-
Notifications
You must be signed in to change notification settings - Fork 0
/
tikztag.sty
130 lines (130 loc) · 5.57 KB
/
tikztag.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
%%%%%%%%%%%%%%%%%%%% tikztag.sty %%%%%%%%%%%%%%%%%%%%
% This code is released under the CC0 license (i.e. in the Public Domain
% or under a very permissive license if your country does not recognise
% dedications to the Public Domain.
% Author: Suzanne Soy.
%%%%%%%%%% \documentclass{article}
%%%%%%%%%% \makeatletter
\usepackage{amsmath, tikz, hyperref, lipsum}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parse options into:
% \if@tikztag@star
% \if@tikztag@label@
% \@tikztag@label
% \if@tikztag@labeltext@
% \@tikztag@label
\newif\if@tikztag@star@
\newif\if@tikztag@label@
\newif\if@tikztag@labeltext@
\def\tikztag{\@ifstar\@tikztag@star\@tikztag@nostar}
\def\@tikztag@star{\@tikztag@star@true\expandafter\@tikztag@common}
\def\@tikztag@nostar{\@tikztag@star@false\expandafter\@tikztag@common}
\def\@tikztag@common{\@ifnextchar[{%
\@tikztag@label@true\@tikztag@get@label%
}{%
\@tikztag@label@false\@tikztag%
}}
\def\@tikztag@get@label[#1]{\def\@tikztag@label{#1}\expandafter\@tikztag@get@maybe@labeltext}
\def\@tikztag@get@maybe@labeltext{\@ifnextchar[{%
\@tikztag@labeltext@true\@tikztag@get@labeltext%
}{%
\@tikztag@labeltext@false\@tikztag%
}}
\def\@tikztag@get@labeltext[#1]{\def\@tikztag@labeltext{#1}\expandafter\@tikztag}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Implementation:
\edef\tikztagnumber{0}
\newcommand{\@tikztag}[1]{%
% place the marker:
\pgfmathtruncatemacro{\tikztagnumber}{\tikztagnumber+1}%
\xdef\tikztagnumber{\tikztagnumber}%
\tikz[remember picture]\coordinate(tikztagmarker\tikztagnumber){};%
% dispatch given the options
\expandafter\gdef\csname @tikztag@tag@\tikztagnumber\endcsname{#1}%
\if@tikztag@label@%
\if@tikztag@labeltext@%
\if@tikztag@star@%
\edef\@@tikztag{\noexpand\@@@tikztag{\expandafter\noexpand\@tikztag@label}{\expandafter\noexpand\@tikztag@labeltext}{\expandafter\noexpand\csname @tikztag@tag@\tikztagnumber\endcsname}{\tikztagnumber}{\noexpand\@firstoftwo}}%
\else%
\edef\@@tikztag{\noexpand\@@@tikztag{\expandafter\noexpand\@tikztag@label}{\expandafter\noexpand\@tikztag@labeltext}{(\expandafter\noexpand\csname @tikztag@tag@\tikztagnumber\endcsname)}{\tikztagnumber}{\noexpand\@firstoftwo}}%
\fi%
\else%
\if@tikztag@star@%
\edef\@@tikztag{\noexpand\@@@tikztag{\expandafter\noexpand\@tikztag@label}{\expandafter\noexpand\csname @tikztag@tag@\tikztagnumber\endcsname}{\expandafter\noexpand\csname @tikztag@tag@\tikztagnumber\endcsname}{\tikztagnumber}{\noexpand\@firstoftwo}}%
\else%
\edef\@@tikztag{\noexpand\@@@tikztag{\expandafter\noexpand\@tikztag@label}{\expandafter\noexpand\csname @tikztag@tag@\tikztagnumber\endcsname}{(\expandafter\noexpand\csname @tikztag@tag@\tikztagnumber\endcsname)}{\tikztagnumber}{\noexpand\@firstoftwo}}%
\fi%
\fi%
\else%
\if@tikztag@star@%
\edef\@@tikztag{\noexpand\@@@tikztag{no label}{no label text}{\expandafter\noexpand\csname @tikztag@tag@\tikztagnumber\endcsname}{\tikztagnumber}{\noexpand\@secondoftwo}}%
\else%
\edef\@@tikztag{\noexpand\@@@tikztag{no label}{no label text}{(\expandafter\noexpand\csname @tikztag@tag@\tikztagnumber\endcsname)}{\tikztagnumber}{\noexpand\@secondoftwo}}%
\fi%
\fi%
\@@tikztag%
}
\newcommand{\@@@tikztag}[5]{%
\g@addto@macro\accumulatetikztag{\@@@@tikztag{#1}{#2}{#3}{#4}{#5}}%
}
% #1=label #2=label text #3=text #4=\tikztagnumber #5=\@firstoftwo or \@secondoftwo to use label or not
\newcommand{\@@@@tikztag}[5]{%
\tikz[overlay,remember picture]%
\node[anchor=base east, inner xsep=0pt]%
at (tikztagmarker#4 -| current line right)%
{#3};%
#5{%
\tikz[overlay,remember picture]%
\node[anchor=base east, inner xsep=0pt]%
at (tikztagmarker#4 -| current line left)%
{\phantomsection\edef\@currentlabel{#2}\label{#1}};%
}{}
}
\def\inittikztag{%
\noindent\tikz[remember picture]\coordinate(current line left){};%
\tikz[overlay,remember picture]%
\coordinate[xshift=\linewidth] (current line right) at (current line left){};%
\xdef\accumulatetikztag{}%
}%
\def\posttikztag{\accumulatetikztag}%
\let\@tikztag@old@texMathDisplay\texMathDisplay%
\def\texMathDisplay#1{\inittikztag\@tikztag@old@texMathDisplay{#1\posttikztag}}%
\newenvironment{taggableequationstar}{%
\inittikztag%
\begin{equation*}%
}{%
\end{equation*}%
\posttikztag
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%\makeatother
%%%%%%%%%%\begin{document}
%%%%%%%%%%
%%%%%%%%%%
%%%%%%%%%%\lipsum[2]
%%%%%%%%%%\begin{equation*}
%%%%%%%%%% \begin{array}{@{}r@{}c@{}l@{}}
%%%%%%%%%% a&bbbbb&c \\
%%%%%%%%%% x&y&z \\
%%%%%%%%%% \end{array}
%%%%%%%%%%\end{equation*}
%%%%%%%%%%\lipsum[2]
%%%%%%%%%%\begin{taggableequationstar}
%%%%%%%%%% \begin{array}{@{}r@{}c@{}l@{}}
%%%%%%%%%% a&bbbbb&c \tikztag[eq:foo][abc]{a, b and c} \\
%%%%%%%%%% x&y&z \tikztag[eq:bar]{other stuff}\\
%%%%%%%%%% x&y&z \tikztag{more stuff}\\
%%%%%%%%%% \end{array}
%%%%%%%%%%\end{taggableequationstar}
%%%%%%%%%%Foo: \ref{eq:foo}. Bar: \ref{eq:bar}. Baz: \ref{eq:baz}. Quux: \ref{eq:quux}.
%%%%%%%%%%\lipsum[2]
%%%%%%%%%%\begin{taggableequationstar}
%%%%%%%%%% \begin{array}{@{}r@{}c@{}l@{}}
%%%%%%%%%% a&bbbbb&c \tikztag*[eq:baz][baz]{a, b and c (baz)} \\
%%%%%%%%%% x&y&z \tikztag*[eq:quux]{other stuff 2}\\
%%%%%%%%%% x&y&z \tikztag*{more stuff 2}\\
%%%%%%%%%% \end{array}
%%%%%%%%%%\end{taggableequationstar}
%%%%%%%%%%
%%%%%%%%%%\end{document}
%%%%%%%%%%%%%%%%%%%% end tikztag.sty %%%%%%%%%%%%%%%%%%%%