Debian Codegraph @

texmf-dist/doc/latex/xpeekahead/xpeekahead.tex

raw
1 %%%==============================================================================
2 % WinEdt pragmas
3 % !Mode:: "TeX:EN"
4 % Default Compile engines:
5 % !TEX program = pdflatex
6 % !PDFTeXify ext = --enable-etex --restrict-write18
7 % !PDFLaTeX ext = --enable-etex --restrict-write18
8 % !BIB program = biber
9 %%%==============================================================================
10 %% Copyright 2025-present by Alceu Frigeri
11 %%
12 %% This work may be distributed and/or modified under the conditions of
13 %%
14 %% * The [LaTeX Project Public License](http://www.latex-project.org/lppl.txt),
15 %% version 1.3c (or later), and/or
16 %% * The [GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.html),
17 %% version 3 (or later)
18 %%
19 %% This work has the LPPL maintenance status *maintained*.
20 %%
21 %% The Current Maintainer of this work is Alceu Frigeri
22 %%
23 %% This is version {1.3a} {2025/11/01}
24 %%
25 %% The list of files that compose this work can be found in the README.md file at
26 %% https://ctan.org/pkg/xpeekahead
27 %%
28 %%%==============================================================================
29 \documentclass[10pt]{article}
30 \RequirePackage[verbose,a4paper,marginparwidth=27.5mm,top=2.5cm,bottom=1.5cm,hmargin={40mm,20mm},marginparsep=2.5mm,columnsep=10mm,asymmetric]{geometry}
31 \usepackage[T1]{fontenc}
32 \usepackage[utf8]{inputenc}
33 \usepackage{lmodern}
34 \usepackage[infograb]{codedescribe}
35 \RequirePackage[inline]{enumitem}
36 \SetEnumitemKey{miditemsep}{parsep=0ex,itemsep=0.4ex}
37
38 \usepackage{xpeekahead}
39
40 %% if needed
41 \RequirePackage[backend=biber]{biblatex}
42 \addbibresource{xpeekahead.bib}
43
44
45 \RequirePackage[hidelinks,hypertexnames=false]{hyperref}
46
47 \begin{document}
48 \tstitle{
49 author={Alceu Frigeri\footnote{\tsverb{https://github.com/alceu-frigeri/xpeekahead}}},
50 date={\tsdate},
51 title={The xpeekahead Package\break Version \PkgInfo{xpeekahead}{version}}
52 }
53
54
55 \begin{typesetabstract}
56
57 This package offers a few commands aiming at peeking ahead environments and commands in simple cases. It's based on \tsobj[pkg]{expl3} and a question at stackexchange \cite{stackexchange}.
58
59 \end{typesetabstract}
60
61 \tableofcontents
62
63 \section{Introduction}
64
65 \tsobj[pkg]{expl3} offers a solid base for programmatically peeking ahead (with the many \tsobj{\peek_} commands), nevertheless some constructions might be extensive and, at times, tricky. This package is focused in two cases:
66 \begin{itemize}
67 \item detecting the first token (perhaps a command) past the end of the current one, ignoring all spaces, blanks, new lines.
68 \item same, but past the end of an environment (the tricky part)
69 \end{itemize}
70 This should be enough in most cases where one wants to fine tune formatting, e.g. spacing, based on what follows.
71
72 Two sets of commands are defined, one to be used in a \tsobj[pkg]{expl3} package (see \ref{expl3-cmds}), and another for use in a \LaTeXe\ code régime (see \ref{2e-cmds}).
73 \begin{tsremark}
74 In fact, given \tsobj{\peek_regex:} flexibility, it is possible to construct a regular expression that will look past a single/few tokens, in which case one is probably best served with the \tsobj[pkg]{expl3} \tsobj{\peek_} functions.
75 \end{tsremark}
76 \begin{tsremark}
77 The \LaTeXe\ commands at \ref{2e-cmds} are just aliases to some of the \tsobj[pkg]{expl3} commands at \ref{expl3-cmds}.
78 \end{tsremark}
79
80
81 \newpage
82 \section{Expl3 Commands}\label{expl3-cmds}
83
84 When peeking ahead, a regular expression is needed to match against, the commands in \ref{peek-expl3} have the option to express the regular expression directly, \tsobj[marg]{regex}, with a pre-compiled regex variable, \tsobj[marg]{pre-regex}, or through a pre-defined command, \ref{cmd-def-expl3}.
85 \begin{tsremark}[\color{red}Note:]
86 All conditionals, \tsobj{T,F,TF} are defined, it's possible to generate variants with, for instance, \tsobj{\prg_generate_conditional_variant:} None of them is expandable.
87 \end{tsremark}
88
89
90 \subsection{Defining Action Commands}\label{cmd-def-expl3}
91
92 \begin{codedescribe}[code,update=2025/10/07]{\xpeekahead_set:NnTF, \xpeekahead_gset:NnTF,\xpeekahead_set:NNTF, \xpeekahead_gset:NNTF}
93 \begin{codesyntax}%
94 \tsmacro{\xpeekahead_set:NnTF}{cmd,regex,if-true,if-false}
95 \tsmacro{\xpeekahead_gset:NnTF}{cmd,regex,if-true,if-false}
96 \tsmacro{\xpeekahead_set:NNTF}{cmd,pre-regex,if-true,if-false}
97 \tsmacro{\xpeekahead_gset:NNTF}{cmd,pre-regex,if-true,if-false}
98 \end{codesyntax}
99 These will create a new command \tsobj[marg]{cmd}, the peeked ahead token(s) will be compared with \tsobj[marg]{regex} and \tsobj[marg,sep=or]{if-true,if-false} will be left on the input stream, before the peeked ahead token(s). For instance, \tsobj[xverb]{\c{begin}\cB{envx}} will match \tsobj[xverb]{\begin{envx}}.
100
101 The \tsobj{\xpeekahead_set:} will create the new command in the current group, whilst \tsobj{\xpeekahead_gset:} will create it grobally
102 \end{codedescribe}
103 \begin{tsremark}
104 \tsobj[marg]{regex} can be any valid Regular Expression, as described in \cite{expl3}, in particular take a look on the section \emph{Matching exact tokens}.
105 \end{tsremark}
106 \begin{tsremark}[\color{red}Warning:]
107 These commands won't check if \tsobj[marg]{cmd} is already defined, and will overwrite any previous definition.
108 \end{tsremark}
109
110
111 \subsection{Peeking Ahead}\label{peek-expl3}
112
113 \begin{codedescribe}[code,update=2025/10/07]{\xpeekahead_cmd_peek:N, \xpeekahead_cmd_peek:nTF, \xpeekahead_cmd_peek:NTF}
114 \begin{codesyntax}%
115 \tsmacro{\xpeekahead_cmd_peek:N}{cmd}
116 \tsmacro{\xpeekahead_cmd_peek:nTF}{regex,if-true,if-false}
117 \tsmacro{\xpeekahead_cmd_peek:NTF}{pre-regex,if-true,if-false}
118 \end{codesyntax}
119 These are for the most simple cases, where \tsobj[code,sep=or]{\xpeekahead_cmd_peek:} will be placed at the very end of a command definition. \tsobj[marg]{regex,if-true,if-false} are the same as defined in \ref{cmd-def-expl3} and \tsobj[marg]{cmd} is any command defined using \tsobj[code,sep=or]{\xpeekahead_(g)set:}.
120 \end{codedescribe}
121
122
123 \begin{codedescribe}[code,update=2025/10/07]{\xpeekahead_env_set:nN, \xpeekahead_env_set:nnTF, \xpeekahead_env_set:nNTF}
124 \begin{codesyntax}%
125 \tsmacro{\xpeekahead_env_set:nN}{env-name,cmd}
126 \tsmacro{\xpeekahead_env_set:nnTF}{env-name,regex,if-true,if-false}
127 \tsmacro{\xpeekahead_env_set:nNTF}{env-name,pre-regex,if-true,if-false}
128 \end{codesyntax}
129 These are for the cases where one wants to detect what comes after the end of an environment. In this case, those commands are to be placed in the ``begin part'' of an environment definition. Note that the peek ahead command will be injected past the end of the environment, meaning any local assignment made inside of the environment won't be accessible.
130
131 \tsobj[marg]{regex,if-true,if-false} are the same as defined in \ref{cmd-def-expl3} and \tsobj[marg]{cmd} is any command defined using \tsobj[code,sep=or]{\xpeekahead_(g)set:}.
132 \end{codedescribe}
133 \begin{tsremark}[\color{red}Important:]
134 The \tsobj[marg]{env-name} HAS TO BE the name of the environment being defined. It will be used by the injected function to evaluate if it should peek ahead or not (in case it's called in the context of another (inner) environment).
135 \end{tsremark}
136 \begin{tsremark}
137 These commands are reentrant safe, meaning, the resulting environment can be nested as needed.
138 \end{tsremark}
139 \begin{tsremark}[\color{red}Warning:]
140 The peek ahead command injection assumes that the macro ``end '' (with a space at the end of it) doesn't change (thanks David, \cite{stackexchange}). Since the macro capture occurs at the outermost call to \tsobj{\xpeekahead_env_set:} it should be safe.
141 \end{tsremark}
142
143
144
145 \section{LaTeX2e Commands}\label{2e-cmds}
146 When peeking ahead, a regular expression is needed to match against, the commands in \ref{peek-2e}
147 have the option to express the regular expression directly, \tsobj[marg]{regex} or through a pre-defined command.
148
149
150 \subsection{Defining Action Commands}\label{cmd-def-2e}
151 \begin{codedescribe}{\xpeekSetCmd, \xpeekSetCmdGlobal}
152 \begin{codesyntax}%
153 \tsmacro{\xpeekSetCmd}{cmd,regex,if-true,if-false}
154 \tsmacro{\xpeekSetCmdGlobal}{cmd,regex,if-true,if-false}
155 \end{codesyntax}
156 These will create a new command \tsobj[marg]{cmd}, the peeked ahead token(s) will be compared with \tsobj[marg]{regex} and \tsobj[marg,sep=or]{if-true,if-false} will be left on the input stream, before the peeked ahead token(s). For instance, \tsobj{\c{begin}\cB{envx}} will match \tsobj{\begin{envx}}.
157
158 The \tsobj{\xpeekSetCmd} will create the new command in the current group, whilst \tsobj{\xpeekSetCmdGlobal} will create it grobally
159 \end{codedescribe}
160 \begin{tsremark}
161 \tsobj[marg]{regex} can be any valid Regular Expression, as described in \cite{expl3}, in particular take a look on the section \emph{Matching exact tokens}.
162 \end{tsremark}
163 \begin{tsremark}[\color{red}Warning:]
164 These commands won't check if \tsobj[marg]{cmd} is already defined, and will overwrite any previous definition.
165 \end{tsremark}
166
167
168 \subsection{Peeking Ahead}\label{peek-2e}
169
170 \begin{codedescribe}{\xpeekTokCmd, \xpeekTok}
171 \begin{codesyntax}%
172 \tsmacro{\xpeekTokCmd}{cmd}
173 \tsmacro{\xpeekTok}{regex,if-true,if-false}
174 \end{codesyntax}
175 These are for the most simple cases, where \tsobj[code,sep=or]{\xpeekTokCmd,\xpeekTok} will be placed at the very end of a command definition. \tsobj[marg]{regex,if-true,if-false} are the same as defined in \ref{cmd-def-2e} and \tsobj[marg]{cmd} is any command defined using \tsobj[code,sep=or]{\xpeekSetCmd, \xpeekSetCmdGlobal}.
176 \end{codedescribe}
177
178
179 \begin{codedescribe}{\xpeekEnvCmd, \xpeekEnv}
180 \begin{codesyntax}%
181 \tsmacro{\xpeekEnvCmd}{env-name,cmd}
182 \tsmacro{\xpeekEnv}{env-name,regex,if-true,if-false}
183 \end{codesyntax}
184 These are for the cases where one wants to detect what comes after the end of an environment. In this case, those commands are to be placed in the ``begin part'' of an environment definition. Note that the peek ahead command will be injected past the end of the environment, meaning any local assignment made inside of the environment won't be accessible.
185
186 \tsobj[marg]{regex,if-true,if-false} are the same as defined in \ref{cmd-def-2e} and \tsobj[marg]{cmd} is any command defined using \tsobj[code,sep=or]{\xpeekSetCmd, \xpeekSetCmdGlobal}.
187 \end{codedescribe}
188 \begin{tsremark}[\color{red}Important:]
189 The \tsobj[marg]{env-name} HAS TO BE the name of the environment being defined. It will be used by the injected function to evaluate if it should peek ahead or not (in case it's called in the context of another (inner) environment).
190 \end{tsremark}
191 \begin{tsremark}
192 These commands are reentrant safe, meaning, the resulting environment can be nested as needed.
193 \end{tsremark}
194 \begin{tsremark}[\color{red}Warning:]
195 The peek ahead command injection assumes that the macro ``end '' (with a space at the end of it) doesn't change (thanks David, \cite{stackexchange}). Since the macro capture occurs at the outermost call to \tsobj[code,sep=or]{\xpeekEnv,\xpeekEnvCmd} it should be safe.
196
197 \end{tsremark}
198
199 \newpage
200
201 \section{Examples}
202
203 To keep things simple, in the examples below \LaTeXe\ syntax will be used, since they are just aliases to their \tsobj[pkg]{expl3} counter parts.
204
205 \subsection{Peeking Ahead Simple Commands}
206 Note that, in this first example, the \tsobj{\xpeekTok,\xpeekTokCmd} were the last command on both \tsobj{\cmdA,\cmdB}.
207
208 Of course, in a more real case, one will (instead of adding some conditional text as in those examples) perhaps adjust vertical spacing and/or set auxiliary variables, etc.
209
210 \setnewcodekey{xpeekdemo}{codeprefix={},resultprefix={},letter={@,_},
211 keywd2={
212 xpeekahead_set:NnTF, xpeekahead_gset:NnTF,
213 xpeekahead_set:NNTF, xpeekahead_gset:NNTF,
214 xpeekahead_cmd_peek:N, xpeekahead_cmd_peek:nTF, xpeekahead_cmd_peek:NTF,
215 xpeekahead_env_set:nN, xpeekahead_env_set:nnTF, xpeekahead_env_set:nNTF,
216 xpeekSetCmd,xpeekSetCmdGlobal, xpeekTokCmd, xpeekTok, xpeekEnvCmd, xpeekEnv
217 },
218 emph2={cmdA,cmdB,cmdC,cmdD,envA,envB,envC,envD}
219 }
220
221
222 \begin{codestore}[cmd-defs]
223 %% This will match either \cmdA or \cmdC
224 \xpeekSetCmd{\detectAC}
225 {\c{cmdA} | \c{cmdC}}
226 {\hspace{5mm} A or C will be next\par}
227 {\hspace{5mm} something else\par}
228
229 %% This will match only \cmdB
230 \xpeekSetCmd{\detectB}
231 {\c{cmdB}}
232 {\hspace{5mm} B will be next\par}
233 {\hspace{5mm} something else\par}
234 \end{codestore}
235 %\tscode*[xpeekdemo]{cmd-defs}
236 \tsexec{cmd-defs}
237
238
239 \begin{codestore}[cmd-defs]
240 % using the pre-defined \detectAC
241 \NewDocumentCommand{\cmdA}{m}
242 {\par .. command A (#1).. \par\xpeekTokCmd{\detectAC}}
243
244 % given the matching regular expression directly
245 \NewDocumentCommand{\cmdB}{m}
246 {\par .. command B (#1).. \par\xpeekTok{\c{cmdC}}{\hspace{3mm}C will be next\par}{\hspace{3mm}something else\par}}
247
248 \NewDocumentCommand{\cmdC}{m}
249 {\par .. command C (#1).. \par}
250 \end{codestore}
251
252 \tsexec{cmd-defs}[2]
253
254
255 \begin{codestore}[demoA]
256 \cmdA{some par for A}
257
258 \cmdA{some par for A, again}
259
260 \cmdB{some for B}
261
262 \cmdC{some for C}
263 \end{codestore}
264
265 \tscode*[xpeekdemo]{cmd-defs}
266 \tscode*[xpeekdemo]{cmd-defs}[2]
267 \tsdemo[xpeekdemo]{demoA}
268 %\tscode
269 %\tsdemo*[emph={draw,none},emph2={path},emph3={name},basicstyle={\scriptsize\ttfamily},numbers=left]{demoA}
270 %\tsresult
271
272 \subsection{Peeking Ahead Environments}
273
274 Note that all one needs to detect an environment is to recognise the sequence \tsobj{\c{begin}} followed by whatever environment one wants to detect:
275
276 \begin{codestore}[cmd-defs]
277 %% This will match the begin of two environments: \begin{envA} or \begin{envC}
278 \xpeekSetCmd{\detectEnvAC}
279 {\c{begin}(\cB{envA}|\cB{envC})}
280 {\hspace{5mm} envA or envC will be next\par}
281 {\hspace{5mm} something else\par}
282
283 %% This will match only \begin{envB}
284 \xpeekSetCmd{\detectEnvB}
285 {\c{begin}\cB{envB}}
286 {\hspace{5mm} B will be next\par}
287 {\hspace{5mm} something else\par}
288 \end{codestore}
289 \tscode*[xpeekdemo]{cmd-defs}[3]
290 \tsexec{cmd-defs}[3]
291
292
293 \begin{codestore}[cmd-defs]
294 \NewDocumentCommand{\cmdD}{m}
295 {
296 \par .. command D (#1) .. \par
297 \xpeekTok{\c{begin}\cB{envA}}
298 {\hspace{3mm}env A will be next\par}
299 {\hspace{3mm}something else\par}
300 }
301
302 \NewDocumentEnvironment{envA}{}
303 {
304 \par beginning of environment A\par
305
306 %this can be put anywhere in the beginning block... no need to be the last command.
307 \xpeekEnvCmd{envA}
308 {\detectEnvAC}
309 }
310 {
311 \par end of environment A\par
312 }
313
314 \NewDocumentEnvironment{envB}{}
315 {
316 \par beginning of environment B\par
317
318 %this can be put anywhere in the beginning block... no need to be the last command.
319 \xpeekEnv{envB}
320 {\c{begin}\cB{envC}}
321 {\hspace{3mm}env C will be next\par}
322 {\hspace{3mm}something else\par}
323 }
324 {
325 \par end of environment B\par
326 }
327
328
329 \NewDocumentEnvironment{envC}{}
330 {
331 \par beginning of environment C\par
332 }
333 {
334 \par end of environment C\par
335 }
336
337 \end{codestore}
338
339
340
341 \begin{codestore}[demoB]
342 \cmdD{some par for D}
343
344 \begin{envA}
345 some text
346 \end{envA}
347
348 \begin{envC}
349 some text
350 \end{envC}
351
352
353 \begin{envB}
354 some text
355 \end{envB}
356
357 \begin{envC}
358 some text
359 \end{envC}
360 \end{codestore}
361
362 \tsexec{cmd-defs}[4]
363
364 \tscode*[xpeekdemo]{cmd-defs}[4]
365
366 \tsdemo[xpeekdemo]{demoB}
367
368
369
370 %% if needed
371 \printbibliography
372
373 \end{document}