summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/easyfloats/doc/preamble/examplecode.tex
blob: 79d3c7f4247d6f013d53f8b43f4b149580bae2ba (plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
% !TeX root = ../easyfloats.tex

% Copyright © 2020 E. Zöllner
% Alternatively to the terms of the LPPL, at your choice,
% you can redistribute and/or modify this file under the
% terms of the Do What The Fuck You Want To Public License, Version 2,
% as published by Sam Hocevar. See http://www.wtfpl.net/about/.

% Defines an environment to typeset LaTeX code:
%     \begin{examplecode}
%         \let\do=\@makeother
%         \dospecials
%     \end{examplecode}
% @ is treated as letter.
%
% A starred version takes one argument, TeX code to be executed inside of the group.
% It can be used to change catcodes or to define escape characters.
%   \begin{examplecode*}{\ExamplecodeEscapeinside $ $}
%       a long$\\$long line
%   \end{examplecode*}
%
% \ExamplecodeEscapeinside takes two arguments
% characters between which the code is executed normally.
%
% \ExamplecodeNoBox
% disables the tcolorbox
%
% \ExamplecodeLinenumbers
% enables line numbers
%
% \\ is redefined to cause a linebreak marked with \ExamplecodePostBreakText.



\makeatletter
% ---------- settings ---------

\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary{breakable,skins}
\usepackage{latexsym}
\usepackage{textcomp}


% taken from /usr/lib/python3/dist-packages/pygments/styles/default.py
\definecolor{math}{RGB}{"BA,"21,"21}
\definecolor{aligntab}{RGB}{"00,"80,"00}
\definecolor{parameter}{RGB}{"19,"17,"7C}
\colorlet{superscript}{black}
\colorlet{subscript}{superscript}
\colorlet{brace}{aligntab}
\colorlet{controlsequence}{aligntab}
\definecolor{comment}{RGB}{"40,"80,"80}
\definecolor{codebackground}{rgb}{0.9,0.9,1}
\colorlet{codeborder}{codebackground!50!black}
\colorlet{codelinkcolor}{white}

\newtcolorbox{examplecodebox}{breakable, skin=enhanced,
	colback = codebackground,
	colframe = codeborder,
	size = fbox, % normal|title|small|fbox|tight|minimal
	%left = 0pt, right = 0pt,
	%top = 0pt, bottom = 0pt,
	%boxrule = 1pt,
	%arc = 3pt,
}
\newcommand{\ExamplecodeNoBox}{%
	\let\examplecodebox=\par
	\let\endexamplecodebox=\par
}

\newcommand{\NewExamplecodeFormatMandatoryArgument}[2]{% #1: control sequence, #2: replacement text where ##1 is the unprocessed argument
	\expandafter\newcommand\csname @examplecode@formatarg\detokenize{#1}\endcsname[1]{\ExamplecodeProcessBeginGroup{\{}#2\ExamplecodeProcessEndGroup{\}}\ExamplecodeAllowLinebreakBeforeNext\@examplecode@loop}%
}
\newcommand{\NewExamplecodeFormatOptionalArgument}[4]{% #1: control sequence, #2: bracket open, #3: bracket close, #4: replacement text where ##1 is the unprocessed argument
	\expandafter\def\csname @examplecode@formatoptarg\detokenize{#1}#2\endcsname##1#3{\ExamplecodeProcessBracket{#2}#4\ExamplecodeProcessBracket{#3}\ExamplecodeAllowLinebreakBeforeNext\@examplecode@loop}%
}
\NewExamplecodeFormatMandatoryArgument\begin{\ExamplecodeProcessEnv{#1}\ExamplecodeSetCmd{#1}}
\NewExamplecodeFormatMandatoryArgument\end{\ExamplecodeProcessEnv{#1}\ExamplecodeNoCmd}
\NewExamplecodeFormatMandatoryArgument\usepackage{\ExamplecodeProcessPkg{#1}\ExamplecodeNoCmd}
\NewExamplecodeFormatMandatoryArgument{object}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
\NewExamplecodeFormatMandatoryArgument{tableobject}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
\NewExamplecodeFormatMandatoryArgument{figureobject}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
\NewExamplecodeFormatMandatoryArgument{subobject}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
\NewExamplecodeFormatMandatoryArgument\objectset{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
\NewExamplecodeFormatOptionalArgument\includegraphicobject[]{\ExamplecodeParseKeys{}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
\NewExamplecodeFormatOptionalArgument\includegraphicsubobject[]{\ExamplecodeParseKeys{}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
\NewExamplecodeFormatMandatoryArgument\NewObjectStyle{\ExamplecodeParse\relax\relax\ExamplecodeEndParseKeys#1\ExamplecodeEndParseKeys\ExamplecodeSetCmd{\NewObjectStyle2}}
\NewExamplecodeFormatMandatoryArgument{\NewObjectStyle2}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}

% custom styles defined in example
\NewExamplecodeFormatMandatoryArgument{tikzobject}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}
\NewExamplecodeFormatMandatoryArgument{tikz3dobject}{\ExamplecodeParseKeys{/object}#1\ExamplecodeEndParseKeys\ExamplecodeNoCmd}

\newcommand{\ExamplecodeFormatMeta}[1]{{\normalfont\meta{#1}}}

\newcommand{\ExamplecodePrint}[1]{#1}

\newcommand{\ExamplecodePostBreakSymbol}{\footnotesize$\hookrightarrow$\quad}
\newcommand{\ExamplecodeIndentation}{}% don't change this, it is overwritten later to always contain the indentation of the current line
\newcommand{\ExamplecodePostBreakText}{\hbox{\ExamplecodeIndentation\ExamplecodePostBreakSymbol}}
\newcommand{\ExamplecodeAllowLinebreak}{\discretionary{}{\ExamplecodePostBreakText}{}}
\newcommand{\ExamplecodeAllowLinebreakBeforeNext}{\@examplecode@allowlinebreakbeforenexttrue}
\newcommand{\ExamplecodeLinebreak}{\newline\ExamplecodePostBreakText}

\newcommand{\ExamplecodeFormatCmd}[1]{{\leavevmode\color{controlsequence}\LinkUnformattedKeyword{cs}{\detokenize{#1}}{\string#1}}\ExamplecodeAllowLinebreakBeforeNext}  % catcode 0/16
\newcommand{\ExamplecodeFormatEnv}[1]{\LinkUnformattedKeyword{env}{#1}{#1}}
\newcommand{\ExamplecodeFormatPkg}[1]{\LinkUnformattedKeyword{pkg}{#1}{#1}}

\newcommand{\ExamplecodeFormatBeginGroup}[1]{\textcolor{brace}{#1}}  % catcode 1
\newcommand{\ExamplecodeFormatEndGroup}[1]{\textcolor{brace}{#1}\ExamplecodeAllowLinebreakBeforeNext}  % catcode 2
\newcommand{\ExamplecodeFormatMathShift}[1]{\textup{#1}}  % catcode 3, see \ExamplecodeFormatMath
\newcommand{\ExamplecodeFormatAlignmentTab}[1]{\textcolor{aligntab}{#1}}  % catcode 4
% catcode 5: \ExamplecodeEndOfLine
\newcommand{\ExamplecodeFormatParameter}[1]{\textcolor{parameter}{#1}}  % catcode 6
\newcommand{\ExamplecodeFormatSuperscript}[1]{\textcolor{superscript}{#1}}  % catcode 7
\newcommand{\ExamplecodeFormatSubscript}[1]{\textcolor{subscript}{#1}}  % catcode 8
% catcode 9: ignored
% catcode 10: \ExamplecodeSpace, \ExamplecodeTab
\newcommand{\ExamplecodeFormatText}[1]{%
	\ifx`#1%
		\textasciigrave
	\else\ifx'#1%
		\textquotesingle
	\else
		#1%
	\fi\fi
}  % catcode 11, 12
\newcommand{\ExamplecodeFormatBracket}[1]{#1}  % catcode 11
% catcode 13: active (uses \ExamplecodeFormatCmd, too)

% in contrast to the other format macros this does
% *not* take an argument. Instead it applies the
% format for the rest of the group.
\newcommand{\ExamplecodeFormatComment}{%
	\leavevmode
	\color{comment}%
	\itshape
	\let\color=\@gobble
}  % catcode 14
\newcommand{\ExamplecodeFormatMath}{%
	\leavevmode
	\color{math}%
	\itshape
	\let\color=\@gobble
	\ExamplecodeForbidLinebreaks
}
\newcommand{\ExamplecodeForbidLinebreaks}{%
	\let\discretionary=\@thirdofthree
}  % catcode 3

% catcode 15: invalid

\newcommand{\ExamplecodeSpaceSymbol}{\space}
\newcommand{\ExamplecodeTabSymbol}{\qquad}
\newcommand{\ExamplecodeIndentationSpace}{\EnsureHMode\nobreak\space}
\newcommand{\ExamplecodeIndentationTab}{\EnsureHMode\nobreak\qquad}
\newcommand{\ExamplecodeNormalSpace}{\discretionary{}{\ExamplecodePostBreakText}{\hbox{\ExamplecodeSpaceSymbol}}}
\newcommand{\ExamplecodeNormalTab}{\discretionary{}{\ExamplecodePostBreakText}{\hbox{\ExamplecodeTabSymbol}}}
\newcommand{\ExamplecodeEndOfLine}{\EnsureHMode\par}


\newcommand{\ExamplecodeTabsize}{4}


\newcommand{\ExamplecodeAllowLinebreakAfter}[1]{\expandafter\def\csname @examplecode@breakafter@#1\endcsname{\ExamplecodeAllowLinebreakBeforeNext}}
\ExamplecodeAllowLinebreakAfter]


% ---------- process ---------

\newif\ifExamplecodeInAlign

\newcommand{\ExamplecodeProcessCmd}[1]{\ExamplecodePrint{\ExamplecodeFormatCmd{#1}}}  % catcode 0/16
\newcommand{\ExamplecodeProcessEnv}[1]{\ExamplecodePrint{\ExamplecodeFormatEnv{#1}}}
\newcommand{\ExamplecodeProcessPkg}[1]{\ExamplecodePrint{\ExamplecodeFormatPkg{#1}}}
\newcommand{\ExamplecodeProcessBeginGroup}[1]{\ExamplecodePrint{\ExamplecodeFormatBeginGroup{#1}}}  % catcode 1
\newcommand{\ExamplecodeProcessEndGroup}[1]{\ExamplecodePrint{\ExamplecodeFormatEndGroup{#1}}}  % catcode 2
\newcommand{\ExamplecodeProcessMathShift}[1]{\ExamplecodePrint{\ExamplecodeFormatMathShift{#1}}}  % catcode 3
\newcommand{\ExamplecodeProcessBeginMath}{\ExamplecodePrint{\begingroup\ExamplecodeFormatMath}}  % catcode 3
\newcommand{\ExamplecodeProcessEndMath}{\ExamplecodePrint{\endgroup}}
\newcommand{\ExamplecodeProcessAlignmentTab}[1]{\ExamplecodePrint{%
	\ifExamplecodeInAlign
		\endgroup
	\fi
	\ExamplecodeAllowLinebreak
	\begingroup
		\ExamplecodeInAligntrue
		\ExamplecodeForbidLinebreaks
		\ExamplecodeFormatAlignmentTab{#1}%
}}  % catcode 4
\newcommand{\ExamplecodeProcessEndOfLine}{\ExamplecodePrint{%
	\ifExamplecodeInAlign
		\endgroup
	\fi
	\ExamplecodeEndOfLine
}}  % catcode 5
\newcommand{\ExamplecodeProcessParameter}[1]{\ExamplecodePrint{\ExamplecodeFormatParameter{#1}}}  % catcode 6
\newcommand{\ExamplecodeProcessSuperscript}[1]{\ExamplecodePrint{\ExamplecodeFormatSuperscript{#1}}}  % catcode 7
\newcommand{\ExamplecodeProcessSubscript}[1]{\ExamplecodePrint{\ExamplecodeFormatSubscript{#1}}}  % catcode 8
% catcode 9: ignored
\newcommand{\ExamplecodeProcessSpace}{\ExamplecodePrint{\ExamplecodeSpace}\ExamplecodeAppendIndentation{\ExamplecodeSpace}}  % catcode 10
\newcommand{\ExamplecodeProcessTab}{\ExamplecodePrint{\ExamplecodeTab}\ExamplecodeAppendIndentation{\ExamplecodeTab}}  % catcode 10
\newcommand{\ExamplecodeProcessText}[1]{\ExamplecodePrint{\ExamplecodeFormatText{#1}}}  % catcode 11, 12
\newcommand{\ExamplecodeProcessBracket}[1]{\ExamplecodePrint{\ExamplecodeFormatBracket{#1}}}  % catcode 11
% catcode 13: active (uses \ExamplecodeProcessCmd, too)
\newcommand{\ExamplecodeProcessBeginComment}{\ExamplecodePrint{\begingroup\ExamplecodeFormatComment}}  % catcode 14
\newcommand{\ExamplecodeProcessEndComment}{\ExamplecodePrint{\endgroup}}
% catcode 15: invalid

\newcommand{\ExamplecodeProcessCleanup}{\ExamplecodePrint{%
	\ifExamplecodeInAlign
		\endgroup
	\fi
}}

\newcommand{\ExamplecodeProcessMeta}[1]{\ExamplecodePrint{\ExamplecodeFormatMeta{#1}}}


% ---------- key=value options ---------

\newcommand{\ExamplecodeParseKeys}[1]{% #1: key path
	\ExamplecodeParse
		{\@examplecode@prepare@parsekeys{#1}}%
		{\@examplecode@cleanup@parsekeys}%
		\ExamplecodeEndParseKeys
}
\newcommand{\@examplecode@prepare@parsekeys}[1]{% #1: key path
	\let\@examplecode@print@original=\ExamplecodePrint
	\ExamplecodeNoCmd
	\def\@examplecode@key@cmd{\key@withopt@unformatted}%
	\def\@examplecode@keypath{#1}%
	\let\@examplecode@pathsep=\@empty
	\def\ExamplecodeProcessText##1{% ##1: current token
		\ifx,##1%
			\ifExamplecodeInAlign
				\endgroup
			\fi
			\@examplecode@endkey
			\let\@examplecode@beginkey=\@examplecode@beginkey@bak
			\@examplecode@print@original{\ExamplecodeFormatText{##1}}%
		\else\ifx=##1%
			\@examplecode@endkey
			\let\@examplecode@beginkey=\relax
			\@examplecode@print@original{\ExamplecodeFormatText{##1}}%
		\else\ifx/##1%
			\@examplecode@beginkey
			\@examplecode@appendpathsep{##1}%
		\else\ifx.##1%
			\unless \ifx \@examplecode@pathsep \@empty
				\@examplecode@endkey
				\expandafter\@examplecode@print@original\expandafter{\expandafter\ExamplecodeFormatText\expandafter{\@examplecode@pathsep}}%
			\fi
			\@examplecode@beginkey
			\ifx \@examplecode@key \@empty
				\def\@examplecode@key@cmd[####1]####2{\LinkUnformattedKeyword{handler}{####2}{####2}}%
			\fi
			\@examplecode@appendkey{##1}%
		\else
			\@examplecode@beginkey
			\@examplecode@appendkey{##1}%
		\fi \fi \fi \fi
	}%
	\def\@examplecode@appendkey##1{%
		\ExamplecodePrint{\ExamplecodeFormatText{##1}}%
	}%
	\def\@examplecode@appendpathsep##1{%
		\ExamplecodePrint{\ExamplecodeFormatText{##1}}%
	}%
	\def\ExamplecodeProcessBeginComment{%
		\@examplecode@endkey
		\begingroup
		\let\@examplecode@beginkey=\relax
		\let\@examplecode@beginkey@bak=\relax
		\@examplecode@print@original{\begingroup\ExamplecodeFormatComment}%
		\let\ExamplecodePrint=\@examplecode@print@original
	}%
	\def\ExamplecodeProcessEndComment{%
		\@examplecode@print@original{\endgroup}%
		\endgroup
	}%
	\def\ExamplecodeProcessBeginGroup##1{%
		\begingroup
		\let\@examplecode@beginkey=\relax
		\let\@examplecode@beginkey@bak=\relax
		\ExamplecodePrint{\ExamplecodeFormatBeginGroup{##1}}%
	}%
	\def\ExamplecodeProcessEndGroup##1{%
		\ExamplecodePrint{\ExamplecodeFormatEndGroup{##1}}%
		\endgroup
	}%
}
\newcommand{\@examplecode@cleanup@parsekeys}{%
	\@examplecode@endkey
	\ExamplecodeProcessCleanup
}
\newcommand{\@examplecode@beginkey}{%
	\begingroup
	\def\@examplecode@key{}%
	\let\@examplecode@keyspace=\@empty
	\def\@examplecode@appendkey##1{%
		\unless \ifx \@examplecode@keyspace \@empty
			\appto\@examplecode@key{ }%
			\let\@examplecode@keyspace=\@empty
		\fi
		\@examplecode@applypathsep
		\appto\@examplecode@key{##1}%
	}%
	\def\@examplecode@appendpathsep##1{%
		\unless \ifx \@examplecode@keyspace \@empty
			\appto\@examplecode@key{ }%
			\let\@examplecode@keyspace=\@empty
		\fi
		\appto\@examplecode@pathsep{##1}%
	}%
	\def\@examplecode@applypathsep{%
		\unless \ifx \@examplecode@pathsep \@empty
			\eappto\@examplecode@key{\@examplecode@pathsep}%
			\let\@examplecode@pathsep=\@empty
		\fi
	}%
	\def\ExamplecodePrint##1{%
		\@examplecode@endkey
		\@examplecode@print@original{##1}%
	}%
	\def\ExamplecodeProcessSpace{%
		\@examplecode@applypathsep
		\appto\@examplecode@keyspace{\ExamplecodeSpace}%
	}%
	\let\@examplecode@beginkey=\relax
	\def\@examplecode@endkey{%
		\edef\@examplecode@do{\noexpand\@examplecode@print@original{%
			\expandafter\noexpand\@examplecode@key@cmd[\@examplecode@keypath]{\@examplecode@key}%
			\expandonce{\@examplecode@pathsep}%
			\expandonce{\@examplecode@keyspace}%
		}}%
		\@examplecode@do
		\endgroup
	}%
}
\let\@examplecode@beginkey@bak=\@examplecode@beginkey
\newcommand{\@examplecode@endkey}{}

\newcommand{\ExamplecodeEndParseKeys}{\PackageError{examplecode}{This should never be expanded}{}}


% ---------- parsing command ---------

\newcommand{\ExamplecodeParse}[3]{% #1: prepare, #2: cleanup, #3: end token
	\begingroup
	#1%
	\let\@examplecode@endtoken=#3%
	\def\@examplecode@checkend##1{#2\@examplecode@beforeline\endgroup}%
	\@examplecode@loop
}

% WARNING: reads end token before changing catcodes
% compares end token with \ifx in case it is a control sequence
% => does not find end token if it's catcode is changed
% => \xc`...` does not work directly but inside of macro
\newcommand{\@xc@prepare}{%
	\def\ExamplecodeNormalSpace{\ExamplecodeSpaceSymbol}%
	\def\ExamplecodeNormalTab{\ExamplecodeTabSymbol}%
	\@examplecode@setcatcodes
	\@examplecode@state@normal
	\let\ExamplecodeAllowLinebreak=\relax
	\gdef\@xc@code{}%
	\def\ExamplecodePrint{\gappto\@xc@code}%
}%
\newcommand{\@xc@afterend}{%
	\formatcode{\@xc@code}%
}
\newcommand{\xc}{%
	\ExamplecodeParse{%
		\@xc@prepare
	}{%
		\ExamplecodeProcessCleanup
		\@xc@afterend
	}%
}
\newcommand{\xckey}{%
	\ExamplecodeParse{%
		\@xc@prepare
		\@examplecode@prepare@parsekeys{}%
	}{%
		\@examplecode@cleanup@parsekeys
		\@xc@afterend
	}%
}


% ---------- escape inside ---------

\newcommand{\ExamplecodeEscapeinside}[2]{% #1: begin token, #2: end token
	\def\@examplecode@escapeinside#1##1#2{##1\@examplecode@loop}%
	\let\@examplecode@escapeinside@startchar=#1%
}


% ---------- line numbers ---------

\newcounter{ExamplecodeLinecounter}
\newcommand{\ExamplecodeLinenumbers}{%
	\setcounter{ExamplecodeLinecounter}{1}%
	\let\@examplecode@beforeline@hook=\@examplecode@insertlinenumber
}
\newcommand\@examplecode@insertlinenumber{%
	\makebox[0pt][r]{{\ExamplecodeFormatLinenumber{\theExamplecodeLinecounter}}\quad}%
	\stepcounter{ExamplecodeLinecounter}%
}
\newcommand\ExamplecodeFormatLinenumber[1]{%
	\normalfont
	\footnotesize
	#1%
}


% ---------- parsing environment ---------

\newenvironment{examplecode}{%
	\csname examplecode*\endcsname{}%
}{%
	\csname endexamplecode*\endcsname{}%
}
\newenvironment{examplecode*}[1]{%
	\let\@examplecode@envname=\@currenvir
	\@examplecode@setcatcodes
	\let\@examplecode@endtoken=\end
	#1%
	\begin{examplecodebox}%
	\colorlet{linkcolor}{codelinkcolor}%
	\raggedright
	\ttfamily
	\leavevmode
	\parskip=0pt
	\parindent=0pt\relax
	\let\\=\ExamplecodeLinebreak
	\@examplecode@state@firstindent
	\@examplecode@gobblefirsteol
}{%
	\end{examplecodebox}%
}

\newenvironment{examplecodekey}{%
	\csname examplecodekey*\endcsname{}%
}{%
	\csname endexamplecodekey*\endcsname{}%
}
\newenvironment{examplecodekey*}[1]{%
	\csname examplecode*\endcsname{%
		\@examplecode@prepare@parsekeys{}%
		#1%
	}%
}{%
	\@examplecode@cleanup@parsekeys
	\csname endexamplecode*\endcsname
}


\newcommand{\@examplecode@setcatcodes}{%
	\@makeother\^^M% end of line
	\@makeother\^^I% tab
	\@makeother\ % space
	\@makeother\%%
	\makeatletter
	% changed
	\@makeother*%
	\@makeother`%
}
\begingroup
\@makeother\^^M %
\@makeother\^^I %
\@firstofone{%
\endgroup%
\let\EolOther=^^M%
\let\TabOther=^^I%
}
\begingroup\@makeother\%\@firstofone{\endgroup\let\PercentOther=%}

\newcommand{\@examplecode@error@catcodes@indentation}{\PackageError{examplecode}{failed to change catcodes, indentation will be screwed up}{}}
\newcommand{\@examplecode@error@mixedtabsandspaces}{\PackageWarning{examplecode}{mixed use of tabs and spaces}}

\newcount\@examplecode@count@outerindent
\newcount\@examplecode@count@gobbledindent
\newif\if@examplecode@allowlinebreakbeforenext

\newcommand{\EnsureHMode}{\leavevmode\@examplecode@beforeline\let\EnsureHMode\relax}%
\let\@EnsureHMode@bak=\EnsureHMode


\newcommand{\@examplecode@beforeline}{%
	\@examplecode@beforeline@hook
	\let\@examplecode@beforeline=\relax
}
\let\@examplecode@beforeline@bak=\@examplecode@beforeline
\let\@examplecode@beforeline@hook=\relax

\newcommand{\@examplecode@state@firstindent}{%
	\@examplecode@beforeline
	\let\@examplecode@check@spaces=\@examplecode@check@firstindent
}
\newcommand{\@examplecode@state@gobbleindent}{%
	\let\@examplecode@beforeline=\@examplecode@beforeline@bak
	\let\@examplecode@state@normal=\@examplecode@state@normal@bak
	\ifnum \@examplecode@count@outerindent > 0
		\@examplecode@count@gobbledindent=0
		\let\@examplecode@check@spaces=\@examplecode@check@gobbleindent
	\else
		\@examplecode@state@indent
	\fi
	\let\EnsureHMode=\@EnsureHMode@bak
}
\newcommand{\@examplecode@state@indent}{%
	\let\@examplecode@check@spaces=\@examplecode@check@normalspaces
	\let\ExamplecodeSpace=\ExamplecodeIndentationSpace
	\let\ExamplecodeTab=\ExamplecodeIndentationTab
	\def\ExamplecodeAppendIndentation##1{\eappto\ExamplecodeIndentation{\expandonce{##1}}}%
	\let\ExamplecodeIndentation=\@empty
}
\newcommand{\@examplecode@state@normal}{%
	\@examplecode@beforeline
	\let\@examplecode@check@spaces=\@examplecode@check@normalspaces
	\let\ExamplecodeSpace=\ExamplecodeNormalSpace
	\let\ExamplecodeTab=\ExamplecodeNormalTab
	\let\@examplecode@state@normal=\relax
	\let\ExamplecodeAppendIndentation=\@gobble
}
\let\@examplecode@state@normal@bak=\@examplecode@state@normal


\newcommand{\@examplecode@gobblefirsteol}[1]{%
	\ifx \EolOther #1\relax
	\else
		\PackageError{examplecode}{unexpected code on same line after \string\begin{examplecode}}{}%
		\detokenize{#1}%
	\fi
	\@examplecode@loop
}

\newcommand{\@examplecode@loop}{%
	\futurelet \@examplecode@nexttoken \@examplecode@check
}
\newcommand{\@examplecode@check}{%
	\ifx \@examplecode@nexttoken \@examplecode@endtoken
		\expandafter \@examplecode@checkend
	\else
		\if@examplecode@allowlinebreakbeforenext
			\ExamplecodeAllowLinebreak
			\@examplecode@allowlinebreakbeforenextfalse
		\fi
		\expandafter \@examplecode@check@ii
	\fi
}

\newcommand{\@examplecode@check@ii}{%
	\ifcat \noexpand\@examplecode@nexttoken \relax
		\def\@examplecode@do{\@examplecode@handle@cs}%
	\else
		\def\@examplecode@do{\@examplecode@check@spaces}%
	\fi
	\@examplecode@do
}
\newcommand{\@examplecode@handle@cs}[1]{%
	\ExamplecodeSetCmd{#1}%
	\@examplecode@state@normal
	\ExamplecodeProcessCmd{#1}%
	\@examplecode@loop
}
\newcommand{\ExamplecodeSetCmd}[1]{\edef\@examplecode@cs{\detokenize{#1}}}
\newcommand{\ExamplecodeNoCmd}{\def\@examplecode@cs{}}
\ExamplecodeNoCmd

\newcommand{\@examplecode@check@normalspaces}{%
	\ifcat \noexpand\@examplecode@nexttoken \space
		\def\@examplecode@do##1{\ExamplecodeProcessSpace\@examplecode@loop##1}%
	\else\if \noexpand\@examplecode@nexttoken \space
		\def\@examplecode@do##1{\ExamplecodeProcessSpace\@examplecode@loop}%
	\else\ifx \@examplecode@nexttoken \TabOther
		\def\@examplecode@do##1{\ExamplecodeProcessTab\@examplecode@loop}%
	\else
		\def\@examplecode@do{\@examplecode@check@iv}%
	\fi \fi \fi
	\@examplecode@do
}
% count the indentation of the first line
\newcommand{\@examplecode@check@firstindent}{%
	\ifcat \noexpand\@examplecode@nexttoken \space
		\@examplecode@error@catcodes@indentation
		\advance\@examplecode@count@outerindent by 1
		\def\@examplecode@do##1{\@examplecode@loop##1}%
	\else\if \noexpand\@examplecode@nexttoken \space
		\advance\@examplecode@count@outerindent by 1
		\def\@examplecode@do##1{\@examplecode@loop}%
	\else\ifx \@examplecode@nexttoken \TabOther
		\advance\@examplecode@count@outerindent by \ExamplecodeTabsize
		\def\@examplecode@do##1{\@examplecode@loop}%
	\else
		\def\@examplecode@do{\@examplecode@check@iv}%
	\fi \fi \fi
	\@examplecode@do
}
% gobble indentation as far as the first line was indented
\newcommand{\@examplecode@check@gobbleindent}{%
	\ifcat \noexpand\@examplecode@nexttoken \space
		\@examplecode@error@catcodes@indentation
		\@examplecode@gobbleindent{1}%
		\def\@examplecode@do##1{\@examplecode@loop##1}%
	\else\if \noexpand\@examplecode@nexttoken \space
		\@examplecode@gobbleindent{1}%
		\def\@examplecode@do##1{\@examplecode@loop}%
	\else\ifx \@examplecode@nexttoken \TabOther
		\@examplecode@gobbleindent{\ExamplecodeTabsize}%
		\def\@examplecode@do##1{\@examplecode@loop}%
	\else
		\def\@examplecode@do{\@examplecode@check@iv}%
	\fi \fi \fi
	\@examplecode@do
}
\newcommand{\@examplecode@gobbleindent}[1]{%
	\advance\@examplecode@count@gobbledindent by #1
	\ifnum \@examplecode@count@gobbledindent = \@examplecode@count@outerindent
		\@examplecode@state@indent
	\else\ifnum \@examplecode@count@gobbledindent > \@examplecode@count@outerindent
		\@examplecode@error@mixedtabsandspaces
		\@examplecode@state@indent
	\fi \fi
}
\newcommand{\@examplecode@check@iv}{%
	\def\@examplecode@do{\@examplecode@check@v}%
	\unless\ifx \@examplecode@escapeinside@startchar \relax
		\if \noexpand\@examplecode@nexttoken \noexpand\@examplecode@escapeinside@startchar
			\def\@examplecode@do{\@examplecode@escapeinside}%
		\fi
	\fi
	\@examplecode@do
}
\newcommand{\@examplecode@check@v}{%
	\ifcat \noexpand\@examplecode@nexttoken \bgroup% catcode 1
		\def\@examplecode@do{\@examplecode@handlebgroup}%
	\else\ifcat \noexpand\@examplecode@nexttoken \egroup% catcode 2
		\def\@examplecode@do{\expandafter\@examplecode@handlespecialtoken\expandafter\ExamplecodeProcessEndGroup\string}%
	\else\ifcat \noexpand\@examplecode@nexttoken $% catcode 3
		\def\@examplecode@do{\@examplecode@handlemathshift}%
	\else\ifcat \noexpand\@examplecode@nexttoken &% catcode 4
		\def\@examplecode@do{\@examplecode@handlespecialtoken\ExamplecodeProcessAlignmentTab}%
	\else\ifx \@examplecode@nexttoken \EolOther% catcode 5
		\@examplecode@endcomment
		\def\@examplecode@do##1{%
			\ExamplecodeProcessEndOfLine
			\@examplecode@state@gobbleindent
			\@examplecode@loop
		}%
	\else\ifcat \noexpand\@examplecode@nexttoken ##% catcode 6
		\def\@examplecode@do{\@examplecode@handleparameter}%
	\else\ifcat \noexpand\@examplecode@nexttoken ^% catcode 7
		\def\@examplecode@do{\@examplecode@handlespecialtoken\ExamplecodeProcessSuperscript}%
	\else\ifcat \noexpand\@examplecode@nexttoken _% catcode 8
		\def\@examplecode@do{\@examplecode@handlespecialtoken\ExamplecodeProcessSubscript}%
	% catcode 9: ignored characters
	% catcode 10: space, handled in \@examplecode@check@spaces
	% catcode 11: letter, handled in \else
	% catcode 12: other, handled in \else
	\else\ifx \@examplecode@nexttoken <%
		\def\@examplecode@do{\@examplecode@handlepossiblemeta}%
	% catcode 13: active, handled in \@examplecode@check@ii (after \futurelet it's indistinguishable from a control sequence)
	\else\ifx \@examplecode@nexttoken \PercentOther% catcode 14
		\@examplecode@state@normal
		\ExamplecodeProcessBeginComment
		\def\@examplecode@endcomment{\ExamplecodeProcessEndComment}%
		\def\@examplecode@do{\@examplecode@handlenormaltoken}%
	% catcode 15: invalid characters
	\else% catcode 11, 12
		\def\@examplecode@do{\@examplecode@handlenormaltoken}%
	\fi \fi \fi \fi \fi \fi \fi \fi \fi \fi
	\@examplecode@do
}
\newcommand{\@examplecode@endcomment}{}
\newcommand{\@examplecode@handlebgroup}{%
	\ifcsundef{@examplecode@formatarg\@examplecode@cs}{%	
		\expandafter\@examplecode@handlespecialtoken\expandafter\ExamplecodeProcessBeginGroup\string
	}{%
		\csname @examplecode@formatarg\@examplecode@cs\endcsname
	}%
}
\newcommand{\@examplecode@handlenormaltoken}[1]{%
	\@examplecode@state@normal
	\ifcsundef{@examplecode@formatoptarg\@examplecode@cs#1}{%
		\csname @examplecode@breakafter@#1\endcsname
		\ExamplecodeProcessText{#1}%
		\@examplecode@loop
	}{%
		\csname @examplecode@formatoptarg\@examplecode@cs#1\endcsname
	}%
}
\newcommand{\@examplecode@handlespecialtoken}[2]{%
	\@examplecode@state@normal
	#1{\string#2}%
	\@examplecode@loop
}

\newcommand{\@examplecode@handlemathshift}[1]{%
	\@examplecode@math@begin
	\ExamplecodeProcessMathShift{\string#1}%
	\@examplecode@math@end
	\@examplecode@loop
}
\newcommand{\@examplecode@math@begin}{%
	\@examplecode@state@normal
	\begingroup
	\let\@examplecode@handlemeta=\@examplecode@handlenormaltoken
	\ExamplecodeProcessBeginMath
	\let\@examplecode@math@begin=\relax
	\def\@examplecode@math@end{\def\@examplecode@math@end{\ExamplecodeProcessEndMath\endgroup}}%
}

\newcommand{\@examplecode@handleparameter}[1]{%
	\edef\@examplecode@parameter{\string#1}%
	\@examplecode@parameterloop
}
\newcommand{\@examplecode@parameterloop}{%
	\futurelet \@examplecode@nexttoken \@examplecode@parametercheck
}
\newcommand{\@examplecode@parametercheck}{%
	\ifcat \noexpand\@examplecode@nexttoken ##%
		\def\@examplecode@do##1{%
			\edef\@examplecode@parameter{\@examplecode@parameter\string##1}%
			\@examplecode@parameterloop
		}%
	\else\ifcat \noexpand\@examplecode@nexttoken 1%
		\def\@examplecode@do##1{%
			\edef\@examplecode@parameter{\@examplecode@parameter##1}%
			\expandafter\ExamplecodeProcessParameter\expandafter{\@examplecode@parameter}%
			\@examplecode@loop
		}%
	\else
		\def\@examplecode@do{%
			\expandafter\ExamplecodeProcessParameter\expandafter{\@examplecode@parameter}%
			\@examplecode@check
		}%
	\fi \fi
	\@examplecode@do
}

\newcommand{\@examplecode@handlepossiblemeta}[1]{%
	\def\@examplecode@tmp{#1}%
	\futurelet \@examplecode@nexttoken \@examplecode@checkmeta
}
\newcommand{\@examplecode@checkmeta}{%
	\ifcat \@examplecode@nexttoken a%
		\def\@examplecode@do{\@examplecode@handlemeta}%
	\else
		\def\@examplecode@do{\@examplecode@handlenormaltoken}%
	\fi
	\expandafter\@examplecode@do\@examplecode@tmp
}
\def\@examplecode@handlemeta<#1>{%
	\ExamplecodeProcessMeta{#1}%
	\@examplecode@loop
}

\newcommand{\@examplecode@checkend}[2]{%
	\def\@examplecode@arg{#2}%
	\ifx \@examplecode@arg \@examplecode@envname
		\expandafter \@firstoftwo
	\else
		\expandafter \@secondoftwo
	\fi{%
		\end{#2}%
	}{%
		\@examplecode@state@normal
		\ExamplecodeProcessCmd{#1}%
		\ExamplecodeProcessBeginGroup{\{}%
		\ExamplecodeProcessEnv{#2}%
		\ExamplecodeProcessEndGroup{\}}%
		\@examplecode@loop
	}%
}%
\makeatother