-
Notifications
You must be signed in to change notification settings - Fork 3
/
ilatex.sty
203 lines (174 loc) 路 7.03 KB
/
ilatex.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
\ProvidesPackage{ilatex}[2020/04/16 iLaTeX]
\RequirePackage{etoolbox}
\RequirePackage{environ}
\RequirePackage[abspath]{currfile}
\RequirePackage{pdfcomment}
\RequirePackage{graphicx}
\RequirePackage{amsmath}
% Create an output file named '.ilatex-mappings'
% to write information about certain commands
% (e.g. path of the source file, line number)
% when they are processed the LaTeX engine
\newwrite\Output@ilatex
\immediate\openout\Output@ilatex=\jobname.ilatex-mappings
% Create a counter to generate identifiers
% that are used to link a piece of code with a PDF annotation
\newcounter{Annotation@ilatex}
\setcounter{Annotation@ilatex}{1}
% Define a command to convert and print a length
% From https://tex.stackexchange.com/a/8337
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
% Define a command to write a new mapping between
% a line in a source file and an identifier
% in the output file created above
\newcommand{\WriteMapToCurrentLine@ilatex}[1]{%
% Important values about the mapping
\immediate\write\Output@ilatex{type #1}%
\immediate\write\Output@ilatex{path \currfileabspath}%
\immediate\write\Output@ilatex{line \the\inputlineno}%
\immediate\write\Output@ilatex{id \theAnnotation@ilatex}%
% Context data which might be useful for some transitionals
% (e.g. current values of certain dimension macros to convert relative lengths)
\immediate\write\Output@ilatex{graphicspath \graphicspath@ilatex}%
\immediate\write\Output@ilatex{length_macro_paperwidth \convertto{pt}{\paperwidth}}%
\immediate\write\Output@ilatex{length_macro_paperheight \convertto{pt}{\paperheight}}%
\immediate\write\Output@ilatex{length_macro_textwidth \convertto{pt}{\textwidth}}%
\immediate\write\Output@ilatex{length_macro_textheight \convertto{pt}{\textheight}}%
\immediate\write\Output@ilatex{length_macro_linewidth \convertto{pt}{\linewidth}}%
\immediate\write\Output@ilatex{length_macro_columnwidth \convertto{pt}{\columnwidth}}%
\immediate\write\Output@ilatex{length_macro_baselineskip \convertto{pt}{\baselineskip}}%
\immediate\write\Output@ilatex{length_macro_parskip \convertto{pt}{\parskip}}%
\immediate\write\Output@ilatex{length_macro_hsize \convertto{pt}{\hsize}}%
\immediate\write\Output@ilatex{length_macro_vsize \convertto{pt}{\vsize}}%
\immediate\write\Output@ilatex{length_macro_rowheight \convertto{pt}{\rowheight}}%
\immediate\write\Output@ilatex{length_macro_cellwidth \convertto{pt}{\cellwidth}}%
\immediate\write\Output@ilatex{length_unit_em \convertto{pt}{1em}}%
\immediate\write\Output@ilatex{length_unit_ex \convertto{pt}{1ex}}%
% End of the mapping
\immediate\write\Output@ilatex{---}%
}
% Define a command to declare an interactive annotation
% It expects two arguments:
% 1. the type of content to map;
% 2. the code of the object to annotate.
%
% This command creates a PDF annotation of the same size
% than the element generated by the given code,
% and it writes the mapping between the unique identifier
% used in the annotation and the current location in the code
% in the dedicated output file (see \WriteMapToCurrentLine@ilatex).
\newcommand{\ilatex}[2]{%
\WriteMapToCurrentLine@ilatex{#1}%
\pdftooltip{{#2}}{ilatex-code-mapping-id-\theAnnotation@ilatex}%
\stepcounter{Annotation@ilatex}%
}
% Define a custom includegraphics-like command
% which can be manipulated through ilatex.
\newcommand{\iincludegraphics}[2][]{%
\ilatex{includegraphics}{\includegraphics[#1]{#2}}%
}
% Patch the \graphicspath command provided by the graphicx package
% to save the list of directories provided by the user (if any)
% into the \graphicspath@ilatex macro (defined below).
% By default, the macro points to the relative current directory.
\def\graphicspath@ilatex{.}
\apptocmd{\graphicspath}{\def\graphicspath@ilatex{#1}}
% Define a custom tabular-like environement
% which can be manipulated through ilatex.
\NewEnviron{itabular}[2][c]
{%
% The \ilatex command seems to fail here,
% so the PDF annotation is directly performed in here.
%
% The current value of the annotation counter is stored as text
% beforehand to fix an expansion problem which seems to occur
% when \theAnnotation@ilatex is used directly inside
% the second argument of \pdftooltip.
\let\CurrentAnnotationValue@ilatex\theAnnotation@ilatex%
\pdftooltip{{%
\begin{tabular}[#1]{#2}%
\BODY
\end{tabular}%
}}{ilatex-code-mapping-id-\CurrentAnnotationValue@ilatex}%
\stepcounter{Annotation@ilatex}%
}
{}
% Since the environ \NewEnviron command reads the full body
% of the environement before executing env. start code,
% the mapping to the beginning of the environement
% must be written in a different way to get the correct line number.
% Using a hook provided by the etoolbox package seems to work well!
\AtBeginEnvironment{itabular}{%
\WriteMapToCurrentLine@ilatex{tabular}%
}
% Define a custom equation-like environement
% which can be manipulated through ilatex.
\NewEnviron{imaths}
{%
\let\CurrentAnnotationValue@ilatex\theAnnotation@ilatex%
\par\noindent
\vspace{\abovedisplayskip}
\pdftooltip{%
\begin{minipage}{\linewidth}%
\begin{align*}%
\BODY
\end{align*}%
\end{minipage}%
}{ilatex-code-mapping-id-\CurrentAnnotationValue@ilatex}%
\stepcounter{Annotation@ilatex}%
\vspace{\belowdisplayskip}%
\par\noindent
}
{}
\AtBeginEnvironment{imaths}{%
\WriteMapToCurrentLine@ilatex{mathematics}%
}
% Provide the gridlayout, row and cell environments.
% Note: these environments used to be provided by the 'gridlayout' package.
\newlength{\gridlayout@gridwidth}
\newlength{\gridlayout@gridheight}
\newlength{\rowheight}
\newlength{\cellwidth}
\newlength{\gridlayout@savedfboxsep}
\setlength{\gridlayout@savedfboxsep}{\fboxsep}
% The gridlayout environements seems to suffer from the same limitations
% than the itabular environement regarding the usage of the \ilatex command.
% For this reason, the writing of a new mapping, the PDF annotation
% and the counter increment are directly performed inside gridlayout's definition.
% Read the comments about the definition of itabular for more details.
\NewEnviron{gridlayout}[2]
{%
\setlength{\fboxsep}{0pt}%
\setlength{\gridlayout@gridwidth}{#1}%
\setlength{\gridlayout@gridheight}{#2}%
\par
\pdftooltip{{\begin{minipage}[b]{\gridlayout@gridwidth}%
\BODY%
\end{minipage}}}{ilatex-code-mapping-id-\CurrentAnnotationValue@ilatex}%
\stepcounter{Annotation@ilatex}%
\setlength{\fboxsep}{\gridlayout@savedfboxsep}
\par
}
{}
\AtBeginEnvironment{gridlayout}{%
\WriteMapToCurrentLine@ilatex{gridlayout}%
\let\CurrentAnnotationValue@ilatex\theAnnotation@ilatex%
}
\NewEnviron{row}[1]
{%
\setlength{\rowheight}{#1\gridlayout@gridheight}%
\makebox{\begin{minipage}[b]{\gridlayout@gridwidth}%
\BODY%
\end{minipage}}%
\par
}
{}
\NewEnviron{cell}[1]
{%
\setlength{\cellwidth}{#1\gridlayout@gridwidth}%
\makebox{\begin{minipage}[b][\rowheight][t]{\cellwidth}%
\BODY%
\end{minipage}}%
}
{}
\endinput