Debian Codegraph @

texmf-dist/doc/latex/easyfloats/preamble/examplecode.tex

raw
1 % !TeX root = ../easyfloats.tex
2
3 % Copyright © 2020 E. Zöllner
4 % Alternatively to the terms of the LPPL, at your choice,
5 % you can redistribute and/or modify this file under the
6 % terms of the Do What The Fuck You Want To Public License, Version 2,
7 % as published by Sam Hocevar. See http://www.wtfpl.net/about/.
8
9 % Defines an environment to typeset LaTeX code:
10 % \begin{examplecode}
11 % \let\do=\@makeother
12 % \dospecials
13 % \end{examplecode}
14 % @ is treated as letter.
15 %
16 % A starred version takes one argument, TeX code to be executed inside of the group.
17 % It can be used to change catcodes or to define escape characters.
18 % \begin{examplecode*}{\ExamplecodeEscapeinside $ $}
19 % a long$\\$long line
20 % \end{examplecode*}
21 %
22 % \ExamplecodeEscapeinside takes two arguments
23 % characters between which the code is executed normally.
24 %
25 % \ExamplecodeNoBox
26 % disables the tcolorbox
27 %
28 % \ExamplecodeLinenumbers
29 % enables line numbers
30 %
31 % \ExamplecodeVerbDelimiter|
32 % code between | is treated as verbatim
33 % \ExamplecodeVerbDelimiterOff
34 % no code is treated as verbatim
35 %
36 % \ExampleCodeEscapeInside{...}
37 % can be used to set \ExamplecodeVerbDelimiter in the example code
38 %
39 % \\ is redefined to cause a linebreak marked with \ExamplecodePostBreakText.
40
41
42
43 \makeatletter
44 % ---------- settings ---------
45
46 \usepackage{xcolor}
47 \usepackage{tcolorbox}
48 \tcbuselibrary{breakable,skins}
49 \usepackage{latexsym}
50 \usepackage{textcomp}
51
52
53 % taken from /usr/lib/python3/dist-packages/pygments/styles/default.py
54 \definecolor{math}{RGB}{"BA,"21,"21}
55 \definecolor{aligntab}{RGB}{"00,"80,"00}
56 \definecolor{parameter}{RGB}{"19,"17,"7C}
57 \colorlet{superscript}{black}
58 \colorlet{subscript}{superscript}
59 \colorlet{brace}{aligntab}
60 \colorlet{controlsequence}{aligntab}
61 \definecolor{comment}{RGB}{"40,"80,"80}
62 \colorlet{verb}{black}
63 \definecolor{codebackground}{rgb}{0.9,0.9,1}
64 \colorlet{codeborder}{codebackground!50!black}
65 \colorlet{codelinkcolor}{white}
66
67 \newtcolorbox{examplecodebox}{breakable, skin=enhanced,
68 colback = codebackground,
69 colframe = codeborder,
70 size = fbox, % normal|title|small|fbox|tight|minimal
71 %left = 0pt, right = 0pt,
72 %top = 0pt, bottom = 0pt,
73 %boxrule = 1pt,
74 %arc = 3pt,
75 }
76 \newcommand{\ExamplecodeNoBox}{%
77 \let\examplecodebox=\par
78 \let\endexamplecodebox=\par
79 }
80
81 \newcommand{\NewExamplecodeFormatMandatoryArgument}[2]{% #1: control sequence, #2: replacement text where ##1 is the unprocessed argument
82 \expandafter\newcommand\csname @examplecode@formatarg\detokenize{#1}\endcsname[1]{\ExamplecodeProcessBeginGroup{\{}#2\ExamplecodeProcessEndGroup{\}}\ExamplecodeAllowLinebreakBeforeNext\@examplecode@loop}%
83 }
84 \newcommand{\NewExamplecodeFormatOptionalArgument}[4]{% #1: control sequence, #2: bracket open, #3: bracket close, #4: replacement text where ##1 is the unprocessed argument
85 \expandafter\def\csname @examplecode@formatoptarg\detokenize{#1}#2\endcsname##1#3{\ExamplecodeProcessBracket{#2}#4\ExamplecodeProcessBracket{#3}\ExamplecodeAllowLinebreakBeforeNext\@examplecode@loop}%
86 }
87 \NewExamplecodeFormatMandatoryArgument\begin{\ExamplecodeProcessEnv{#1}\ExamplecodeSetCmd{#1}}
88 \NewExamplecodeFormatMandatoryArgument\end{\ExamplecodeProcessEnv{#1}\ExamplecodeNoCmd}
89 \NewExamplecodeFormatMandatoryArgument\usepackage{\ExamplecodeProcessPkg{#1}\ExamplecodeNoCmd}
90 \NewExamplecodeFormatMandatoryArgument{object}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
91 \NewExamplecodeFormatMandatoryArgument{tableobject}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
92 \NewExamplecodeFormatMandatoryArgument{figureobject}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
93 \NewExamplecodeFormatMandatoryArgument{subobject}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
94 \NewExamplecodeFormatMandatoryArgument\objectset{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
95 \NewExamplecodeFormatOptionalArgument\includegraphicobject[]{\ExamplecodeParseKeys{}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
96 \NewExamplecodeFormatOptionalArgument\includegraphicsubobject[]{\ExamplecodeParseKeys{}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
97 \NewExamplecodeFormatMandatoryArgument\NewObjectStyle{\ExamplecodeParse\relax\relax\ExamplecodeEndParseKeys#1\ExamplecodeEndParseKeys\ExamplecodeSetCmd{\NewObjectStyle2}}
98 \NewExamplecodeFormatMandatoryArgument{\NewObjectStyle2}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
99
100 % custom styles defined in example
101 \NewExamplecodeFormatMandatoryArgument{tikzobject}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
102 \NewExamplecodeFormatMandatoryArgument{tikz3dobject}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
103
104 \newcommand{\ExamplecodeFormatMeta}[1]{{\normalfont\meta{#1}}}
105
106 \newcommand{\ExamplecodePrint}[1]{#1}
107
108 \newcommand{\ExamplecodePostBreakSymbol}{\footnotesize$\hookrightarrow$\quad}
109 \newcommand{\ExamplecodeIndentation}{}% don't change this, it is overwritten later to always contain the indentation of the current line
110 \newcommand{\ExamplecodePostBreakText}{\hbox{\ExamplecodeIndentation\ExamplecodePostBreakSymbol}}
111 \newcommand{\ExamplecodeAllowLinebreak}{\discretionary{}{\ExamplecodePostBreakText}{}}
112 \newcommand{\ExamplecodeAllowLinebreakBeforeNext}{\@examplecode@allowlinebreakbeforenexttrue}
113 \newcommand{\ExamplecodeLinebreak}{\newline\ExamplecodePostBreakText}
114
115 \ExplSyntaxOn
116 \newcommand{\ExamplecodeFormatCmd}[1]{
117 {
118 \leavevmode
119 \color{controlsequence}
120 % do not try to link macros in expl syntax
121 \tl_if_in:nnTF {#1}{\textunderscore}
122 {\string#1}
123 {\LinkUnformattedKeyword{cs}{\detokenize{#1}}{\string#1}}
124 }
125 \ExamplecodeAllowLinebreakBeforeNext
126 } % catcode 0/16
127 \ExplSyntaxOff
128 \newcommand{\ExamplecodeFormatEnv}[1]{\LinkUnformattedKeyword{env}{#1}{#1}}
129 \newcommand{\ExamplecodeFormatPkg}[1]{\LinkUnformattedKeyword{pkg}{#1}{#1}}
130
131 \newcommand{\ExamplecodeFormatBeginGroup}[1]{\textcolor{brace}{#1}} % catcode 1
132 \newcommand{\ExamplecodeFormatEndGroup}[1]{\textcolor{brace}{#1}\ExamplecodeAllowLinebreakBeforeNext} % catcode 2
133 \newcommand{\ExamplecodeFormatMathShift}[1]{\textup{#1}} % catcode 3, see \ExamplecodeFormatMath
134 \newcommand{\ExamplecodeFormatAlignmentTab}[1]{\textcolor{aligntab}{#1}} % catcode 4
135 % catcode 5: \ExamplecodeEndOfLine
136 \newcommand{\ExamplecodeFormatParameter}[1]{\textcolor{parameter}{#1}} % catcode 6
137 \newcommand{\ExamplecodeFormatSuperscript}[1]{\textcolor{superscript}{#1}} % catcode 7
138 \newcommand{\ExamplecodeFormatSubscript}[1]{\textcolor{subscript}{#1}} % catcode 8
139 % catcode 9: ignored
140 % catcode 10: \ExamplecodeSpace, \ExamplecodeTab
141 \newcommand{\ExamplecodeFormatText}[1]{%
142 \ifx`#1%
143 \textasciigrave
144 \else\ifx'#1%
145 \textquotesingle
146 \else
147 #1%
148 \fi\fi
149 } % catcode 11, 12
150 \newcommand{\ExamplecodeFormatBracket}[1]{#1} % catcode 11
151 % catcode 13: active (uses \ExamplecodeFormatCmd, too)
152
153 % in contrast to the other format macros this does
154 % *not* take an argument. Instead it applies the
155 % format for the rest of the group.
156 \newcommand{\ExamplecodeFormatComment}{%
157 \leavevmode
158 \color{comment}%
159 \itshape
160 \let\color=\@gobble
161 } % catcode 14
162 \newcommand{\ExamplecodeFormatMath}{%
163 \leavevmode
164 \color{math}%
165 \itshape
166 \let\color=\@gobble
167 \ExamplecodeForbidLinebreaks
168 }
169 \newcommand{\ExamplecodeForbidLinebreaks}{%
170 \let\discretionary=\@thirdofthree
171 } % catcode 3
172
173 % catcode 15: invalid
174
175 \newcommand{\ExamplecodeSpaceSymbol}{\space}
176 \newcommand{\ExamplecodeTabSymbol}{\qquad}
177 \newcommand{\ExamplecodeIndentationSpace}{\EnsureHMode\nobreak\space}
178 \newcommand{\ExamplecodeIndentationTab}{\EnsureHMode\nobreak\qquad}
179 \newcommand{\ExamplecodeNormalSpace}{\discretionary{}{\ExamplecodePostBreakText}{\hbox{\ExamplecodeSpaceSymbol}}}
180 \newcommand{\ExamplecodeNormalTab}{\discretionary{}{\ExamplecodePostBreakText}{\hbox{\ExamplecodeTabSymbol}}}
181 \newcommand{\ExamplecodeEndOfLine}{\EnsureHMode\par}
182
183
184 \newcommand{\ExamplecodeTabsize}{4}
185
186
187 \newcommand{\ExamplecodeAllowLinebreakAfter}[1]{\expandafter\def\csname @examplecode@breakafter@#1\endcsname{\ExamplecodeAllowLinebreakBeforeNext}}
188 \ExamplecodeAllowLinebreakAfter]
189
190
191 \newcommand\ExampleCodeEscapeInside{\PackageError{examplecode}{\string\ExampleCodeEscapeInside\ should not be called outside of examplecode}{}}
192 \newcommand{\ExamplecodeVerbDelimiter}[1]{%
193 \let\@examplecode@verb@delimiter=#1%
194 }
195 \newcommand{\ExamplecodeVerbDelimiterOff}{%
196 \let\@examplecode@verb@delimiter=\relax
197 }
198 \ExamplecodeVerbDelimiterOff
199
200 \newcommand{\ExamplecodeFormatVerbDelimiter}[1]{\textcolor{brace}{#1}}
201 \newcommand{\ExamplecodeFormatVerb}{\color{verb}}
202
203
204 % ---------- process ---------
205
206 \newif\ifExamplecodeInAlign
207 \newcounter{@examplecode@grouplevel}
208 \gdef\@examplecode@grouplevel@mathenter{-1}
209
210 \newcommand{\ExamplecodeProcessCmd}[1]{\ExamplecodePrint{\ExamplecodeFormatCmd{#1}}} % catcode 0/16
211 \newcommand{\ExamplecodeProcessEnv}[1]{\ExamplecodePrint{\ExamplecodeFormatEnv{#1}}}
212 \newcommand{\ExamplecodeProcessPkg}[1]{\ExamplecodePrint{\ExamplecodeFormatPkg{#1}}}
213 \newcommand{\ExamplecodeProcessBeginGroup}[1]{%
214 \stepcounter{@examplecode@grouplevel}%
215 \ExamplecodePrint{\ExamplecodeFormatBeginGroup{#1}}%
216 } % catcode 1
217 \newcommand{\ExamplecodeProcessEndGroup}[1]{%
218 \ifnum \value{@examplecode@grouplevel} = \@examplecode@grouplevel@mathenter \relax
219 \@examplecode@math@end
220 \fi
221 \addtocounter{@examplecode@grouplevel}{-1}%
222 \ExamplecodePrint{\ExamplecodeFormatEndGroup{#1}}%
223 } % catcode 2
224 \newcommand{\ExamplecodeProcessMathShift}[1]{\ExamplecodePrint{\ExamplecodeFormatMathShift{#1}}} % catcode 3
225 \newcommand{\ExamplecodeProcessBeginMath}{\ExamplecodePrint{\begingroup\ExamplecodeFormatMath}} % catcode 3
226 \newcommand{\ExamplecodeProcessEndMath}{\ExamplecodePrint{\endgroup}}
227 \newcommand{\ExamplecodeProcessAlignmentTab}[1]{\ExamplecodePrint{%
228 \ifExamplecodeInAlign
229 \endgroup
230 \fi
231 \ExamplecodeAllowLinebreak
232 \begingroup
233 \ExamplecodeInAligntrue
234 \ExamplecodeForbidLinebreaks
235 \ExamplecodeFormatAlignmentTab{#1}%
236 }} % catcode 4
237 \newcommand{\ExamplecodeProcessEndOfLine}{\ExamplecodePrint{%
238 \ifExamplecodeInAlign
239 \endgroup
240 \fi
241 \ExamplecodeEndOfLine
242 }} % catcode 5
243 \newcommand{\ExamplecodeProcessParameter}[1]{\ExamplecodePrint{\ExamplecodeFormatParameter{#1}}} % catcode 6
244 \newcommand{\ExamplecodeProcessSuperscript}[1]{\ExamplecodePrint{\ExamplecodeFormatSuperscript{#1}}} % catcode 7
245 \newcommand{\ExamplecodeProcessSubscript}[1]{\ExamplecodePrint{\ExamplecodeFormatSubscript{#1}}} % catcode 8
246 % catcode 9: ignored
247 \newcommand{\ExamplecodeProcessSpace}{\ExamplecodePrint{\ExamplecodeSpace}\ExamplecodeAppendIndentation{\ExamplecodeSpace}} % catcode 10
248 \newcommand{\ExamplecodeProcessTab}{\ExamplecodePrint{\ExamplecodeTab}\ExamplecodeAppendIndentation{\ExamplecodeTab}} % catcode 10
249 \newcommand{\ExamplecodeProcessText}[1]{\ExamplecodePrint{\ExamplecodeFormatText{#1}}} % catcode 11, 12
250 \newcommand{\ExamplecodeProcessBracket}[1]{\ExamplecodePrint{\ExamplecodeFormatBracket{#1}}} % catcode 11
251 % catcode 13: active (uses \ExamplecodeProcessCmd, too)
252 \newcommand{\ExamplecodeProcessBeginComment}{\ExamplecodePrint{\begingroup\ExamplecodeFormatComment}} % catcode 14
253 \newcommand{\ExamplecodeProcessEndComment}{\ExamplecodePrint{\endgroup}}
254 % catcode 15: invalid
255
256 \newcommand{\ExamplecodeProcessCleanup}{\ExamplecodePrint{%
257 \ifExamplecodeInAlign
258 \endgroup
259 \fi
260 }}
261
262 \newcommand{\ExamplecodeProcessMeta}[1]{\ExamplecodePrint{\ExamplecodeFormatMeta{#1}}}
263
264
265 % ---------- key=value options ---------
266
267 \newcommand{\ExamplecodeParseKeys}[1]{% #1: key path
268 \ExamplecodeParse
269 {\@examplecode@prepare@parsekeys{#1}}%
270 {\@examplecode@cleanup@parsekeys}%
271 \ExamplecodeEndParseKeys
272 }
273 \newcommand{\@examplecode@prepare@parsekeys}[1]{% #1: key path
274 \let\@examplecode@print@original=\ExamplecodePrint
275 \ExamplecodeNoCmd
276 \def\@examplecode@key@cmd{\key@withopt@unformatted}%
277 \def\@examplecode@keypath{#1}%
278 \let\@examplecode@pathsep=\@empty
279 \def\ExamplecodeProcessText##1{% ##1: current token
280 \ifx,##1%
281 \ifExamplecodeInAlign
282 \endgroup
283 \fi
284 \@examplecode@endkey
285 \let\@examplecode@beginkey=\@examplecode@beginkey@bak
286 \@examplecode@print@original{\ExamplecodeFormatText{##1}}%
287 \else\ifx=##1%
288 \@examplecode@endkey
289 \let\@examplecode@beginkey=\relax
290 \@examplecode@print@original{\ExamplecodeFormatText{##1}}%
291 \else\ifx/##1%
292 \@examplecode@beginkey
293 \@examplecode@appendpathsep{##1}%
294 \else\ifx.##1%
295 \unless \ifx \@examplecode@pathsep \@empty
296 \@examplecode@endkey
297 \expandafter\@examplecode@print@original\expandafter{\expandafter\ExamplecodeFormatText\expandafter{\@examplecode@pathsep}}%
298 \fi
299 \@examplecode@beginkey
300 \ifx \@examplecode@key \@empty
301 \def\@examplecode@key@cmd[####1]####2{\LinkUnformattedKeyword{handler}{####2}{####2}}%
302 \fi
303 \@examplecode@appendkey{##1}%
304 \else
305 \@examplecode@beginkey
306 \@examplecode@appendkey{##1}%
307 \fi \fi \fi \fi
308 }%
309 \def\@examplecode@appendkey##1{%
310 \ExamplecodePrint{\ExamplecodeFormatText{##1}}%
311 }%
312 \def\@examplecode@appendpathsep##1{%
313 \ExamplecodePrint{\ExamplecodeFormatText{##1}}%
314 }%
315 \def\ExamplecodeProcessBeginComment{%
316 \@examplecode@endkey
317 \begingroup
318 \let\@examplecode@beginkey=\relax
319 \let\@examplecode@beginkey@bak=\relax
320 \@examplecode@print@original{\begingroup\ExamplecodeFormatComment}%
321 \let\ExamplecodePrint=\@examplecode@print@original
322 }%
323 \def\ExamplecodeProcessEndComment{%
324 \@examplecode@print@original{\endgroup}%
325 \endgroup
326 }%
327 \def\ExamplecodeProcessBeginGroup##1{%
328 \begingroup
329 \let\@examplecode@beginkey=\relax
330 \let\@examplecode@beginkey@bak=\relax
331 \ExamplecodePrint{\ExamplecodeFormatBeginGroup{##1}}%
332 }%
333 \def\ExamplecodeProcessEndGroup##1{%
334 \ExamplecodePrint{\ExamplecodeFormatEndGroup{##1}}%
335 \endgroup
336 }%
337 }
338 \newcommand{\@examplecode@cleanup@parsekeys}{%
339 \@examplecode@endkey
340 \ExamplecodeProcessCleanup
341 }
342 \newcommand{\@examplecode@beginkey}{%
343 \begingroup
344 \def\@examplecode@key{}%
345 \let\@examplecode@keyspace=\@empty
346 \def\@examplecode@appendkey##1{%
347 \unless \ifx \@examplecode@keyspace \@empty
348 \appto\@examplecode@key{ }%
349 \let\@examplecode@keyspace=\@empty
350 \fi
351 \@examplecode@applypathsep
352 \appto\@examplecode@key{##1}%
353 }%
354 \def\@examplecode@appendpathsep##1{%
355 \unless \ifx \@examplecode@keyspace \@empty
356 \appto\@examplecode@key{ }%
357 \let\@examplecode@keyspace=\@empty
358 \fi
359 \appto\@examplecode@pathsep{##1}%
360 }%
361 \def\@examplecode@applypathsep{%
362 \unless \ifx \@examplecode@pathsep \@empty
363 \eappto\@examplecode@key{\@examplecode@pathsep}%
364 \let\@examplecode@pathsep=\@empty
365 \fi
366 }%
367 \def\ExamplecodePrint##1{%
368 \@examplecode@endkey
369 \@examplecode@print@original{##1}%
370 }%
371 \def\ExamplecodeProcessSpace{%
372 \@examplecode@applypathsep
373 \appto\@examplecode@keyspace{\ExamplecodeSpace}%
374 }%
375 \let\@examplecode@beginkey=\relax
376 \def\@examplecode@endkey{%
377 \edef\@examplecode@do{\noexpand\@examplecode@print@original{%
378 \expandafter\noexpand\@examplecode@key@cmd[\@examplecode@keypath]{\@examplecode@key}%
379 \expandonce{\@examplecode@pathsep}%
380 \expandonce{\@examplecode@keyspace}%
381 }}%
382 \@examplecode@do
383 \endgroup
384 }%
385 }
386 \let\@examplecode@beginkey@bak=\@examplecode@beginkey
387 \newcommand{\@examplecode@endkey}{}
388
389 \newcommand{\ExamplecodeEndParseKeys}{\PackageError{examplecode}{This should never be expanded}{}}
390
391
392 % ---------- parsing command ---------
393
394 \newcommand{\ExamplecodeParse}[3]{% #1: prepare, #2: cleanup, #3: end token
395 \begingroup
396 #1%
397 \let\@examplecode@endtoken=#3%
398 \def\@examplecode@checkend##1{#2\@examplecode@beforeline\endgroup}%
399 \@examplecode@loop
400 }
401
402 % WARNING: reads end token before changing catcodes
403 % compares end token with \ifx in case it is a control sequence
404 % => does not find end token if it's catcode is changed
405 % => \xc`...` does not work directly but inside of macro
406 \newcommand{\@xc@prepare}{%
407 \def\ExamplecodeNormalSpace{\ExamplecodeSpaceSymbol}%
408 \def\ExamplecodeNormalTab{\ExamplecodeTabSymbol}%
409 \@examplecode@setcatcodes
410 \@examplecode@state@normal
411 \let\ExamplecodeAllowLinebreak=\relax
412 \gdef\@xc@code{}%
413 \def\ExamplecodePrint{\gappto\@xc@code}%
414 }%
415 \newcommand{\@xc@afterend}{%
416 \formatcode{\@xc@code}%
417 }
418 \newcommand{\xc}{%
419 \ExamplecodeParse{%
420 \@xc@prepare
421 }{%
422 \ExamplecodeProcessCleanup
423 \@xc@afterend
424 }%
425 }
426 \newcommand{\xckey}{%
427 \ExamplecodeParse{%
428 \@xc@prepare
429 \@examplecode@prepare@parsekeys{}%
430 }{%
431 \@examplecode@cleanup@parsekeys
432 \@xc@afterend
433 }%
434 }
435
436
437 % ---------- escape inside ---------
438
439 \newcommand{\ExamplecodeEscapeinside}[2]{% #1: begin token, #2: end token
440 \def\@examplecode@escapeinside#1##1#2{##1\@examplecode@loop}%
441 \let\@examplecode@escapeinside@startchar=#1%
442 }
443
444
445 % ---------- line numbers ---------
446
447 \newcounter{ExamplecodeLinecounter}
448 \newcommand{\ExamplecodeLinenumbers}{%
449 \setcounter{ExamplecodeLinecounter}{1}%
450 \let\@examplecode@beforeline@hook=\@examplecode@insertlinenumber
451 }
452 \newcommand\@examplecode@insertlinenumber{%
453 \makebox[0pt][r]{{\ExamplecodeFormatLinenumber{\theExamplecodeLinecounter}}\quad}%
454 \stepcounter{ExamplecodeLinecounter}%
455 }
456 \newcommand\ExamplecodeFormatLinenumber[1]{%
457 \normalfont
458 \footnotesize
459 #1%
460 }
461
462
463 % ---------- parsing environment ---------
464
465 \newenvironment{examplecode}{%
466 \csname examplecode*\endcsname{}%
467 }{%
468 \csname endexamplecode*\endcsname{}%
469 }
470 \newenvironment{examplecode*}[1]{%
471 \let\@examplecode@envname=\@currenvir
472 \@examplecode@setcatcodes
473 \let\@examplecode@endtoken=\end
474 #1%
475 \begin{examplecodebox}%
476 \colorlet{linkcolor}{codelinkcolor}%
477 \raggedright
478 \ttfamily
479 \leavevmode
480 \parskip=0pt
481 \parindent=0pt\relax
482 \let\\=\ExamplecodeLinebreak
483 \setcounter{@examplecode@grouplevel}{0}%
484 \@examplecode@state@firstindent
485 \@examplecode@gobblefirsteol
486 }{%
487 \end{examplecodebox}%
488 }
489
490 \newenvironment{examplecodekey}{%
491 \csname examplecodekey*\endcsname{}%
492 }{%
493 \csname endexamplecodekey*\endcsname{}%
494 }
495 \newenvironment{examplecodekey*}[1]{%
496 \csname examplecode*\endcsname{%
497 \@examplecode@prepare@parsekeys{}%
498 #1%
499 }%
500 }{%
501 \@examplecode@cleanup@parsekeys
502 \csname endexamplecode*\endcsname
503 }
504
505
506 \newcommand{\@examplecode@setcatcodes}{%
507 \@makeother\^^M% end of line
508 \@makeother\^^I% tab
509 \@makeother\ % space
510 \@makeother\%%
511 \makeatletter
512 % changed
513 \@makeother*%
514 \@makeother`%
515 }
516 \begingroup
517 \@makeother\^^M %
518 \@makeother\^^I %
519 \@firstofone{%
520 \endgroup%
521 \let\EolOther=^^M%
522 \let\TabOther=^^I%
523 }
524 \begingroup\@makeother\%\@firstofone{\endgroup\let\PercentOther=%}
525
526 \newcommand{\@examplecode@error@catcodes@indentation}{\PackageError{examplecode}{failed to change catcodes, indentation will be screwed up}{}}
527 \newcommand{\@examplecode@error@mixedtabsandspaces}{\PackageWarning{examplecode}{mixed use of tabs and spaces}}
528
529 \newcount\@examplecode@count@outerindent
530 \newcount\@examplecode@count@gobbledindent
531 \newif\if@examplecode@allowlinebreakbeforenext
532
533 \newcommand{\EnsureHMode}{\leavevmode\@examplecode@beforeline\let\EnsureHMode\relax}%
534 \let\@EnsureHMode@bak=\EnsureHMode
535
536
537 \newcommand{\@examplecode@beforeline}{%
538 \@examplecode@beforeline@hook
539 \let\@examplecode@beforeline=\relax
540 }
541 \let\@examplecode@beforeline@bak=\@examplecode@beforeline
542 \let\@examplecode@beforeline@hook=\relax
543
544 \newcommand{\@examplecode@state@firstindent}{%
545 \@examplecode@beforeline
546 \let\@examplecode@check@spaces=\@examplecode@check@firstindent
547 }
548 \newcommand{\@examplecode@state@gobbleindent}{%
549 \let\@examplecode@beforeline=\@examplecode@beforeline@bak
550 \let\@examplecode@state@normal=\@examplecode@state@normal@bak
551 \ifnum \@examplecode@count@outerindent > 0
552 \@examplecode@count@gobbledindent=0
553 \let\@examplecode@check@spaces=\@examplecode@check@gobbleindent
554 \else
555 \@examplecode@state@indent
556 \fi
557 \let\EnsureHMode=\@EnsureHMode@bak
558 }
559 \newcommand{\@examplecode@state@indent}{%
560 \let\@examplecode@check@spaces=\@examplecode@check@normalspaces
561 \let\ExamplecodeSpace=\ExamplecodeIndentationSpace
562 \let\ExamplecodeTab=\ExamplecodeIndentationTab
563 \def\ExamplecodeAppendIndentation##1{\eappto\ExamplecodeIndentation{\expandonce{##1}}}%
564 \let\ExamplecodeIndentation=\@empty
565 }
566 \newcommand{\@examplecode@state@normal}{%
567 \@examplecode@beforeline
568 \let\@examplecode@check@spaces=\@examplecode@check@normalspaces
569 \let\ExamplecodeSpace=\ExamplecodeNormalSpace
570 \let\ExamplecodeTab=\ExamplecodeNormalTab
571 \let\@examplecode@state@normal=\relax
572 \let\ExamplecodeAppendIndentation=\@gobble
573 }
574 \let\@examplecode@state@normal@bak=\@examplecode@state@normal
575
576
577 \newcommand{\@examplecode@gobblefirsteol}[1]{%
578 \ifx \EolOther #1\relax
579 \else
580 \PackageError{examplecode}{unexpected code on same line after \string\begin{examplecode}}{}%
581 \detokenize{#1}%
582 \fi
583 \@examplecode@loop
584 }
585
586 \newcommand{\@examplecode@loop}{%
587 \futurelet \@examplecode@nexttoken \@examplecode@check
588 }
589 \newcommand{\@examplecode@check}{%
590 \ifx \@examplecode@nexttoken \@examplecode@endtoken
591 \expandafter \@examplecode@checkend
592 \else
593 \if@examplecode@allowlinebreakbeforenext
594 \ExamplecodeAllowLinebreak
595 \@examplecode@allowlinebreakbeforenextfalse
596 \fi
597 \expandafter \@examplecode@check@i
598 \fi
599 }
600
601 \newcommand{\@examplecode@check@i}{%
602 \ifx \@examplecode@nexttoken \ExampleCodeEscapeInside
603 \def\@examplecode@do##1##2{##2\@examplecode@loop}%
604 \expandafter \@gobble
605 \else
606 \expandafter \@firstofone
607 \fi
608 {\ifx \@examplecode@verb@delimiter \relax
609 \def\@examplecode@do{\@examplecode@check@ii}%
610 \expandafter \@gobble
611 \else
612 \expandafter \@firstofone
613 \fi
614 % I cannot nest the if statments because \@examplecode@nexttoken can be an if
615 {\ifx \@examplecode@nexttoken \@examplecode@verb@delimiter
616 \def\@examplecode@do{\@examplecode@handle@verb}%
617 \else
618 \def\@examplecode@do{\@examplecode@check@ii}%
619 \fi}}%
620 \@examplecode@do
621 }
622 \newcommand{\@examplecode@handle@verb}[1]{%
623 \ExamplecodeFormatVerbDelimiter{\string#1}%
624 \begingroup
625 \ExamplecodeFormatVerb
626 \@examplecode@handle@verb@loop
627 }
628 \newcommand{\@examplecode@handle@verb@loop}{%
629 \futurelet \@examplecode@nexttoken \@examplecode@handle@verb@check
630 }
631 \newcommand{\@examplecode@handle@verb@check}{%
632 \ifx \@examplecode@nexttoken \@examplecode@verb@delimiter
633 \def\@examplecode@do{\@examplecode@handle@verb@end}%
634 \else \ifx \@examplecode@nexttoken \bgroup
635 \def\@examplecode@do{\{\afterassignment\@examplecode@handle@verb@loop\let\@examplecode@trash=}%
636 \else \ifx \@examplecode@nexttoken \egroup
637 \def\@examplecode@do{\}\afterassignment\@examplecode@handle@verb@loop\let\@examplecode@trash=}%
638 \else
639 \def\@examplecode@do##1{\string##1\@examplecode@handle@verb@loop}%
640 \fi \fi \fi
641 \@examplecode@do
642 }
643 \newcommand{\@examplecode@handle@verb@end}[1]{%
644 \ExamplecodeFormatVerbDelimiter{\string#1}%
645 \endgroup
646 \@examplecode@loop
647 }
648
649
650 \newcommand{\@examplecode@check@ii}{%
651 \ifcat \noexpand\@examplecode@nexttoken \relax
652 \def\@examplecode@do{\@examplecode@handle@cs}%
653 \else
654 \def\@examplecode@do{\@examplecode@check@spaces}%
655 \fi
656 \@examplecode@do
657 }
658 \newcommand{\@examplecode@handle@cs}[1]{%
659 \ifx\ExplSyntaxOn#1
660 \@examplecode@explsyntaxon
661 \fi
662 \ifx\ExplSyntaxOff#1
663 \@examplecode@explsyntaxoff
664 \fi
665 \ExamplecodeSetCmd{#1}%
666 \@examplecode@state@normal
667 \ExamplecodeProcessCmd{#1}%
668 \@examplecode@loop
669 }
670 \let \@examplecode@handle@cs@normal = \@examplecode@handle@cs
671
672 \newcommand{\@examplecode@explsyntaxon}{%
673 \let \@examplecode@handle@cs = \@examplecode@handle@cs@expl
674 }
675 \newcommand{\@examplecode@explsyntaxoff}{%
676 \let \@examplecode@handle@cs = \@examplecode@handle@cs@normal
677 }
678
679 \newcommand{\@examplecode@handle@cs@expl}[1]{%
680 \def\@examplecode@tmp@csname{#1}%
681 \@examplecode@handle@cs@expl@loop
682 }
683 \newcommand{\@examplecode@handle@cs@expl@loop}{%
684 \futurelet \@examplecode@nexttoken \@examplecode@handle@cs@expl@check
685 }
686 \newcommand{\@examplecode@handle@cs@expl@check}{%
687 \ifx \@examplecode@nexttoken _%
688 \def\@examplecode@do##1{\appto\@examplecode@tmp@csname{\textunderscore}\@examplecode@handle@cs@expl@loop}%
689 \else\ifx \@examplecode@nexttoken :%
690 \def\@examplecode@do##1{\appto\@examplecode@tmp@csname{##1}\@examplecode@handle@cs@expl@loop}%
691 \else\ifcat \noexpand\@examplecode@nexttoken a%
692 \def\@examplecode@do##1{\appto\@examplecode@tmp@csname{##1}\@examplecode@handle@cs@expl@loop}%
693 \else
694 \def\@examplecode@do{\expandafter\@examplecode@handle@cs@normal\expandafter{\@examplecode@tmp@csname}}%
695 \fi\fi\fi
696 \@examplecode@do
697 }
698
699 \newcommand{\ExamplecodeSetCmd}[1]{\edef\@examplecode@cs{\detokenize{#1}}}
700 \newcommand{\ExamplecodeNoCmd}{\def\@examplecode@cs{}}
701 \ExamplecodeNoCmd
702
703 \newcommand{\@examplecode@check@normalspaces}{%
704 \ifcat \noexpand\@examplecode@nexttoken \space
705 \def\@examplecode@do##1{\ExamplecodeProcessSpace\@examplecode@loop##1}%
706 \else\if \noexpand\@examplecode@nexttoken \space
707 \def\@examplecode@do##1{\ExamplecodeProcessSpace\@examplecode@loop}%
708 \else\ifx \@examplecode@nexttoken \TabOther
709 \def\@examplecode@do##1{\ExamplecodeProcessTab\@examplecode@loop}%
710 \else
711 \def\@examplecode@do{\@examplecode@check@iv}%
712 \fi \fi \fi
713 \@examplecode@do
714 }
715 % count the indentation of the first line
716 \newcommand{\@examplecode@check@firstindent}{%
717 \ifcat \noexpand\@examplecode@nexttoken \space
718 \@examplecode@error@catcodes@indentation
719 \advance\@examplecode@count@outerindent by 1
720 \def\@examplecode@do##1{\@examplecode@loop##1}%
721 \else\if \noexpand\@examplecode@nexttoken \space
722 \advance\@examplecode@count@outerindent by 1
723 \def\@examplecode@do##1{\@examplecode@loop}%
724 \else\ifx \@examplecode@nexttoken \TabOther
725 \advance\@examplecode@count@outerindent by \ExamplecodeTabsize
726 \def\@examplecode@do##1{\@examplecode@loop}%
727 \else
728 \def\@examplecode@do{\@examplecode@check@iv}%
729 \fi \fi \fi
730 \@examplecode@do
731 }
732 % gobble indentation as far as the first line was indented
733 \newcommand{\@examplecode@check@gobbleindent}{%
734 \ifcat \noexpand\@examplecode@nexttoken \space
735 \@examplecode@error@catcodes@indentation
736 \@examplecode@gobbleindent{1}%
737 \def\@examplecode@do##1{\@examplecode@loop##1}%
738 \else\if \noexpand\@examplecode@nexttoken \space
739 \@examplecode@gobbleindent{1}%
740 \def\@examplecode@do##1{\@examplecode@loop}%
741 \else\ifx \@examplecode@nexttoken \TabOther
742 \@examplecode@gobbleindent{\ExamplecodeTabsize}%
743 \def\@examplecode@do##1{\@examplecode@loop}%
744 \else
745 \def\@examplecode@do{\@examplecode@check@iv}%
746 \fi \fi \fi
747 \@examplecode@do
748 }
749 \newcommand{\@examplecode@gobbleindent}[1]{%
750 \advance\@examplecode@count@gobbledindent by #1
751 \ifnum \@examplecode@count@gobbledindent = \@examplecode@count@outerindent
752 \@examplecode@state@indent
753 \else\ifnum \@examplecode@count@gobbledindent > \@examplecode@count@outerindent
754 \@examplecode@error@mixedtabsandspaces
755 \@examplecode@state@indent
756 \fi \fi
757 }
758 \newcommand{\@examplecode@check@iv}{%
759 \def\@examplecode@do{\@examplecode@check@v}%
760 \unless\ifx \@examplecode@escapeinside@startchar \relax
761 \if \noexpand\@examplecode@nexttoken \noexpand\@examplecode@escapeinside@startchar
762 \def\@examplecode@do{\@examplecode@escapeinside}%
763 \fi
764 \fi
765 \@examplecode@do
766 }
767 \newcommand{\@examplecode@check@v}{%
768 \ifcat \noexpand\@examplecode@nexttoken \bgroup% catcode 1
769 \def\@examplecode@do{\@examplecode@handlebgroup}%
770 \else\ifcat \noexpand\@examplecode@nexttoken \egroup% catcode 2
771 \def\@examplecode@do{\expandafter\@examplecode@handlespecialtoken\expandafter\ExamplecodeProcessEndGroup\string}%
772 \else\ifcat \noexpand\@examplecode@nexttoken $% catcode 3
773 \def\@examplecode@do{\@examplecode@handlemathshift}%
774 \else\ifcat \noexpand\@examplecode@nexttoken &% catcode 4
775 \def\@examplecode@do{\@examplecode@handlespecialtoken\ExamplecodeProcessAlignmentTab}%
776 \else\ifx \@examplecode@nexttoken \EolOther% catcode 5
777 \@examplecode@endcomment
778 \def\@examplecode@do##1{%
779 \ExamplecodeProcessEndOfLine
780 \@examplecode@state@gobbleindent
781 \@examplecode@loop
782 }%
783 \else\ifcat \noexpand\@examplecode@nexttoken ##% catcode 6
784 \def\@examplecode@do{\@examplecode@handleparameter}%
785 \else\ifcat \noexpand\@examplecode@nexttoken ^% catcode 7
786 \def\@examplecode@do{\@examplecode@handlespecialtoken\ExamplecodeProcessSuperscript}%
787 \else\ifcat \noexpand\@examplecode@nexttoken _% catcode 8
788 \def\@examplecode@do{\@examplecode@handlespecialtoken\ExamplecodeProcessSubscript}%
789 % catcode 9: ignored characters
790 % catcode 10: space, handled in \@examplecode@check@spaces
791 % catcode 11: letter, handled in \else
792 % catcode 12: other, handled in \else
793 \else\ifx \@examplecode@nexttoken <%
794 \def\@examplecode@do{\@examplecode@handlepossiblemeta}%
795 % catcode 13: active, handled in \@examplecode@check@ii (after \futurelet it's indistinguishable from a control sequence)
796 \else\ifx \@examplecode@nexttoken \PercentOther% catcode 14
797 \@examplecode@state@normal
798 \ExamplecodeProcessBeginComment
799 \def\@examplecode@endcomment{\ExamplecodeProcessEndComment}%
800 \def\@examplecode@do{\@examplecode@handlenormaltoken}%
801 % catcode 15: invalid characters
802 \else% catcode 11, 12
803 \def\@examplecode@do{\@examplecode@handlenormaltoken}%
804 \fi \fi \fi \fi \fi \fi \fi \fi \fi \fi
805 \@examplecode@do
806 }
807 \newcommand{\@examplecode@endcomment}{}
808 \newcommand{\@examplecode@handlebgroup}{%
809 \ifcsundef{@examplecode@formatarg\@examplecode@cs}{%
810 \expandafter\@examplecode@handlespecialtoken\expandafter\ExamplecodeProcessBeginGroup\string
811 }{%
812 \csname @examplecode@formatarg\@examplecode@cs\endcsname
813 }%
814 }
815 \newcommand{\@examplecode@handlenormaltoken}[1]{%
816 \@examplecode@state@normal
817 \ifcsundef{@examplecode@formatoptarg\@examplecode@cs#1}{%
818 \csname @examplecode@breakafter@#1\endcsname
819 \ExamplecodeProcessText{#1}%
820 \@examplecode@loop
821 }{%
822 \csname @examplecode@formatoptarg\@examplecode@cs#1\endcsname
823 }%
824 }
825 \newcommand{\@examplecode@handlespecialtoken}[2]{%
826 \@examplecode@state@normal
827 #1{\string#2}%
828 \@examplecode@loop
829 }
830
831 \newcommand{\@examplecode@handlemathshift}[1]{%
832 \@examplecode@math@begin
833 \ExamplecodeProcessMathShift{\string#1}%
834 \@examplecode@math@end
835 \@examplecode@loop
836 }
837 \newcommand{\@examplecode@math@begin}{%
838 \xdef\@examplecode@grouplevel@mathenter{\the@examplecode@grouplevel}%
839 \@examplecode@state@normal
840 \begingroup
841 \let\@examplecode@handlemeta=\@examplecode@handlenormaltoken
842 \ExamplecodeProcessBeginMath
843 \let\@examplecode@math@begin=\relax
844 \def\@examplecode@math@end{\def\@examplecode@math@end{\ExamplecodeProcessEndMath\endgroup\gdef\@examplecode@grouplevel@mathenter{-1}}}%
845 }
846
847 \newcommand{\@examplecode@handleparameter}[1]{%
848 \edef\@examplecode@parameter{\string#1}%
849 \@examplecode@parameterloop
850 }
851 \newcommand{\@examplecode@parameterloop}{%
852 \futurelet \@examplecode@nexttoken \@examplecode@parametercheck
853 }
854 \newcommand{\@examplecode@parametercheck}{%
855 \ifcat \noexpand\@examplecode@nexttoken ##%
856 \def\@examplecode@do##1{%
857 \edef\@examplecode@parameter{\@examplecode@parameter\string##1}%
858 \@examplecode@parameterloop
859 }%
860 \else\ifcat \noexpand\@examplecode@nexttoken 1%
861 \def\@examplecode@do##1{%
862 \edef\@examplecode@parameter{\@examplecode@parameter##1}%
863 \expandafter\ExamplecodeProcessParameter\expandafter{\@examplecode@parameter}%
864 \@examplecode@loop
865 }%
866 \else
867 \def\@examplecode@do{%
868 \expandafter\ExamplecodeProcessParameter\expandafter{\@examplecode@parameter}%
869 \@examplecode@check
870 }%
871 \fi \fi
872 \@examplecode@do
873 }
874
875 \newcommand{\@examplecode@handlepossiblemeta}[1]{%
876 \def\@examplecode@tmp{#1}%
877 \futurelet \@examplecode@nexttoken \@examplecode@checkmeta
878 }
879 \newcommand{\@examplecode@checkmeta}{%
880 \ifcat \@examplecode@nexttoken a%
881 \def\@examplecode@do{\@examplecode@handlemeta}%
882 \else
883 \def\@examplecode@do{\@examplecode@handlenormaltoken}%
884 \fi
885 \expandafter\@examplecode@do\@examplecode@tmp
886 }
887 \def\@examplecode@handlemeta<#1>{%
888 \ExamplecodeProcessMeta{#1}%
889 \@examplecode@loop
890 }
891
892 \newcommand{\@examplecode@checkend}[2]{%
893 \def\@examplecode@arg{#2}%
894 \ifx \@examplecode@arg \@examplecode@envname
895 \expandafter \@firstoftwo
896 \else
897 \expandafter \@secondoftwo
898 \fi{%
899 \end{#2}%
900 }{%
901 \@examplecode@state@normal
902 \ExamplecodeProcessCmd{#1}%
903 \ExamplecodeProcessBeginGroup{\{}%
904 \ExamplecodeProcessEnv{#2}%
905 \ExamplecodeProcessEndGroup{\}}%
906 \@examplecode@loop
907 }%
908 }%
909
910
911 % ---------- input command ---------
912
913 \begingroup\@examplecode@setcatcodes\gdef\@examplecode@magic@comment@prefix{% !}\gdef\@examplecode@empty@line{
914 }\gdef\@examplecode@input@testmarkup{\input{preamble/testmarkup}
915 }\endgroup
916
917 \ExplSyntaxOn
918 \newcommand\inputexamplecode[1]{%
919 \group_begin:
920 \@examplecode@setcatcodes
921 \ior_open:Nn \g_tmpa_ior {#1}
922 \tl_clear:N \l_file_contents_tl
923 \bool_set_false:N \l_tmpa_bool
924 \ior_map_inline:Nn \g_tmpa_ior {
925 \bool_if:NF \l_tmpa_bool {
926 \tl_set:Nx\l_tmpa_tl{\tl_range:nnn{##1}{1}{3}}
927 \tl_if_eq:NNTF \@examplecode@magic@comment@prefix \l_tmpa_tl {
928 % do nothing
929 }{\tl_if_eq:NNTF \@examplecode@empty@line \l_tmpa_tl {
930 % do nothing
931 }{\tl_if_eq:NNTF \@examplecode@input@testmarkup \l_tmpa_tl {
932 % do nothing
933 }{
934 \bool_set_true:N \l_tmpa_bool
935 }}}
936 }
937 \bool_if:NT \l_tmpa_bool {
938 \tl_put_right:Nn \l_file_contents_tl {##1}
939 }
940 }
941 \ior_close:N \g_tmpa_ior
942 \def\l_tmpa_tl{\begin{examplecode}\EolOther}
943 \expandafter \l_tmpa_tl \l_file_contents_tl
944 \end{examplecode}
945 \group_end:
946 }
947 \ExplSyntaxOff
948
949 \makeatother