forked from probcomp/tutorial_highlighter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlighter.sty
37 lines (32 loc) · 1.75 KB
/
highlighter.sty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
% This file from: https://tex.stackexchange.com/a/19716
\RequirePackage{atbegshi}
\RequirePackage{forloop}
\RequirePackage{ifthen}
\RequirePackage{tikz}
\RequirePackage{zref-abspage}
\RequirePackage{zref-abspos}
% customizable by package user
\tikzset{highlighter/.style = {yellow, line width = \baselineskip}}
% anchor placement, with @highlight counting upward to generate unique names
\newcounter{@highlight}
\newcommand{\@HighlightAnchor}[1]{\ensuremath{\vcenter{\zsavepos{highlight-#1}}}}
\newcommand{\HighlightFrom}[0]{\stepcounter{@highlight}\@HighlightAnchor{begin-\the@highlight}}
\newcommand{\HighlightTo}[0]{\@HighlightAnchor{end-\the@highlight}}
% highlight painting, with @@highlight counting upward to consider all defined highlights
\newcounter{@@highlight}
\newcommand{\@HighlightPage}[1]{\zref@extract{highlight-#1-\the@@highlight}{abspage}}
\newcommand{\@HighlightCoords}[2]{(#1\zposx{highlight-#2-\the@@highlight}sp, #1\zposy{highlight-#2-\the@@highlight}sp)}
\AtBeginShipout{
\AtBeginShipoutUpperLeft{
% consider every highlight until reaching one that is undefined
\forloop{@@highlight}{1}{\@HighlightPage{begin} > 0}{
% page highlight if it begins and ends on the current page
\ifthenelse{\@HighlightPage{begin} = \value{abspage}}{
\ifthenelse{\@HighlightPage{end} = \value{abspage}}{
% drop an anchor here so we compute the proper (x, y) offsets
\zsavepos{highlight-draw-\the@@highlight}%
\tikz[overlay, shift={\@HighlightCoords{-}{draw}}]{
\draw [highlighter] \@HighlightCoords{}{begin} -- \@HighlightCoords{}{end};
}}
{\PackageWarning{highlighter}{highlight \protect#\the@@highlight\space crosses from page \@HighlightPage{begin} to page \@HighlightPage{end}}}}
{}}}}