summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/showexpl/showexpl.dtx
blob: 6219774f7b463fe152798c2ef99e2af094fa0a82 (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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
% \iffalse meta-comment
%
% Copyright (C) 2004 -- by Rolf Niepraschk <Rolf.Niepraschk@gmx.de>
% --------------------------------------------------------------------
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.2
% of this license or (at your option) any later version.
% The latest version of this license is in:
%
%    http://www.latex-project.org/lppl.txt
%
% and version 1.2 or later is part of all distributions of LaTeX
% version 1999/12/01 or later.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{showexpl.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{showexpl}
%<*package>
    [2020/05/26 v0.3q Typesetting example code (RN)]
%</package>
%
%<*driver>
\documentclass[a4paper]{ltxdoc}
\usepackage{showexpl}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{showexpl.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{889}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v0.1a}{2004/11/14}{Initial version}
%
% \GetFileInfo{showexpl.dtx}
%
% \DoNotIndex{\newcommand,\renewcommand,\newenvironment,\renewenvironment}
% \DoNotIndex{\usepackage,\documentclass,\nofiles,\bibliogaphy}
% \DoNotIndex{\tiny,\tableofcontens}
% \DoNotIndex{\newif,\newcounter,\linewidth,\listfiles}
% \DoNotIndex{\providecommand,\def,\edef,\let,\gdef,\xdef,\global,\newtoks}
% \DoNotIndex{\RequirePackage,\DeclareOption,\ProcessOptions,\ExecuteOptions}
% \DoNotIndex{\@nameuse,\value,\input,\InputIfFileExists}
% \DoNotIndex{\@ifdefinable,\@ifundefined,\@percentchar}
% \DoNotIndex{\AtBeginDocument,\AtEndOfPackage,\PassOptionsToPackage}
% \DoNotIndex{\CurrentOption,\jobname}
% \DoNotIndex{\PackageError,\PackageWarning,\PackageWarningNoLine,\PackageInfo}
% \DoNotIndex{\MessageBreak,\typeout}
% \DoNotIndex{\z@,\z@skip,\p@,\@ne,\tw@,\thr@@,\@iv,\two@fourteen,\strip@pt}
% \DoNotIndex{\the,\if,\else,\or,\fi,\ifnum,\ifdim,\ifcase,\ifodd}
% \DoNotIndex{\advance,\multiply,\divide,\ht,\dp,\wd,\catcode}
% \DoNotIndex{\@tfor,\do,\bgroup,\egroup,\ifx,\iftrue,\iffalse}
% \DoNotIndex{\csname,\endcsname,\begingroup,\endgroup}
% \DoNotIndex{\expandafter,\afterassignment,\noexpand}
% \DoNotIndex{\@tempdima,\@tempdimb,\@tempdimc,\@tempcnta,\@tempcntb}
% \DoNotIndex{\@halfwidth,\@wholewidth,\unitlength}
% \DoNotIndex{\@clnwd,\@clnht,\@ovdx,\@ovdy,\@ovro,\@ovri,\@ovxx,\@ovyy}
% \DoNotIndex{\@xarg,\@xdim,\@yarg,\@ydim,\@linelen,\@dashdim,\dimen@}
% \DoNotIndex{\reserved@a,\relax,\protect,\long,\space}
% \DoNotIndex{\if@tempswa,\@tempswatrue,\@tempswafalse,\@tempa}
% \DoNotIndex{\@tempboxa,\@tempboxb,\show}
% \DoNotIndex{\@empty,\%,\typeout,\vspace,\vskip,\the,\hbox,\par}
% \DoNotIndex{\minipage,\endminipage,\trivlist,\endtrivlist}
% \DoNotIndex{\parbox,\setbox,\setlength,\hfill,\item,\number}
% \DoNotIndex{\x}
% \DoNotIndex{\SX@put@a,\SX@put@b,\SX@put@l,\SX@put@r,\SX@put@o,\SX@put@i}
%
% ^^A\DoNotIndex{\usepackage,\documentclass,\tableofcontens,\printindex}
%
% \title{The \textsf{showexpl} package\thanks{This document
%   corresponds to \textsf{showexpl}~\fileversion, dated \filedate.}}
% \author{Rolf Niepraschk (\texttt{Rolf.Niepraschk@gmx.de})}
% \date{\filedate}
%
% \maketitle
%
% \section{Introduction}
%
% The documentation of a \LaTeX{} package is by far more readable if there
% are examples of the commands' and environments' usage. The best way to do
% that is to give a comparison of the \LaTeX{} code and the formatted
% output. \textsf{showexpl} is a package for doing that comparison, it is
% based on the package \textsf{listings} which provides a good typesetted
% source code with emphasised keywords and so on.
%
% \section{Usage}
%
% You can use \textsf{showexpl} like every other package by putting the
% line
% \begin{verbatim}
% \usepackage{showexpl}
% \end{verbatim}
% \vspace{-\baselineskip}
% in your source code. \textsf{showexpl} doesn't know any options by
% itself, but all options for the underlying packages
% (\textsf{listings} and \textsf{graphicx}) will be passed to the
% respective packages.
%
% \textsf{showexpl} provides one command and one environment:
% \begin{itemize}
% \item |\LTXinputExample| and
% \item \textsf{LTXexample}
% \end{itemize}
%
% \DescribeMacro{\LTXinputExample}
% The syntax of |\LTXinputExample| is given by
% \begin{quote}
% |\LTXinputExample[|\meta{key val list}|]{|\meta{file}|}|
% \end{quote}
%
% \DescribeEnv{LTXexample}
% The syntax of the environment \textsf{LTXexample} is given by
% \begin{quote}
% |\begin{LTXexample}[|\meta{key val list}|]|\ldots|\end{LTXexample}|
% \end{quote}
%
% The set of options represented by \meta{key val list} is the same for
% both the command and the environment, the options are described in the
% following:
% \begin{description}
%   \item[attachfile] Boolean valued key, default value: false. If set to
%   true the sourcecode will be attached to the \textsf{.pdf}
%   file---presumed that the document is processed by |pdflatex|.
%   \item[codefile] Name of the (temporary) file that contains the code
%   which will be formatted as source code. The default value is |\jobname.tmp|.
%   \item[explpreset] A \meta{key val list} which serves for presetting the
%   properties of the formatting of the source code, for values see the
%   documentation of the \textsf{listings} package. The default value is
%
%   \item[graphic] Name of a (graphic) file. This file---if present---will
%   be included and displayed instead of the formatted code. The default value is empty.
%   \item[hsep] Defines the horizontal distance between the source code and the
%   formatted text.
%   \item[justification] Defines the justification of the formatted text:
%   reasonable values are |\raggedleft|, |\raggedright|, |\centering|. The
%   default value is |\raggedright|.
%   \item[overhang] A \textit{dimen}-value that defines the amount by which
%   the formatted text and the source code can overlap the print space. The
%   default value is 0\,pt.
%   \item[pos:] Defines the relative position of the formatted text
%   relating to the source code. Allowed values are |t|, |b|, |l|, |r|,
%   |o|, and |i| for top, bottom, left, right, outer, and inner. The last
%   values give sense only for two-sided printing, where there are outer
%   and inner margins of a page. The default value is |l|.
%   \item[preset] Any \TeX\ code executed before the sample code but
%   not visible in the listings area.
%   \item[rangeaccept] Boolean valued key, default value is false. If set
%   to  true, one can define ranges of lines that will be excerpted from
%   the source code.
%   \item[rframe] Defines the form of the frame around the formatted
%   text. With a non-empty value (e.\,g. ``single'') a simple frame
%   will be drawn. In the future more kinds of frames will be supported.
%   The default value is empty (no frame).
%   \item[varwidth] Boolean valued key, default value is false. If set to
%   true, the formatted text is set with its ``natural'' width instead of a
%   fixed width as given by the value of the option |width|.
%   \item[vsep] Defines the vertical distance between the source code and the
%   formatted text.
%   \item[wide] Boolean valued key, default value is false. If set to
%   true, the source code and the formatted text overlap the print space
%   and the margin area.
%   \item[width] A \meta{dimen} value that defines the width of the
%   formatted text. The default value depends of the relative positions of
%   the source code and the formatted text.
%   \item[scaled] Without a value the formatted text will be scaled to fit
%   the given width of the result area. With a number as value the formatted
%   text will be scaled by this number.
% \end{description}
% In addition to these options the kind of the result box (default: |\fbox|)
% can be changed. For example:
% \begin{verbatim}
%   \renewcommand\ResultBox{\fcolorbox{green}{lightgray}}
%   \setlength\ResultBoxSep{5mm}%  default: \fboxsep
%   \setlength\ResultBoxRule{2mm}% default: \fboxrule
% \end{verbatim}
%
% \StopEventually{}
%
% \section{Implementation}
% \changes{v0.1e}{2004/11/24}{Loading of |showexpl.cfg| added (RN).}
% \changes{v0.3p}{2020/05/04}{Remove package `calc' (RN).}
%    \begin{macrocode}
\DeclareOption{final}{%
  \PassOptionsToPackage{\CurrentOption}{graphicx}%
  \PassOptionsToPackage{\CurrentOption}{listings}%
}%
\DeclareOption{draft}{%
  \PassOptionsToPackage{\CurrentOption}{graphicx}%
  \PassOptionsToPackage{\CurrentOption}{listings}%
}%
%    \end{macrocode}
% \changes{v0.3h}{2007/02/03}{New Option `attachfiles' (RN).}
%    \begin{macrocode}
\DeclareOption{attachfiles}{%
  \AtBeginDocument{\IfFileExists{attachfile.sty}%
    {\RequirePackage{attachfile}}{\def\SX@attachfile{}}}
}%
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{listings}}
\ProcessOptions\relax
\RequirePackage{refcount,listings,graphicx,varwidth,float}
%    \end{macrocode}
% We must aktivate code from package \textsf{listings} for writing files.
%    \begin{macrocode}
\lst@RequireAspects{writefile}
%    \end{macrocode}
% \begin{macro}{\SX@defaultWD}
% Parameter |#2| is a length or a number. Parameter |#1| is a
% macro. After a call of \cmd{\SX@defaultWD} this macro contains the value
% of the length or the value of the number multiplied by \cmd{\linewidth}.
%    \begin{macrocode}
\newcommand*\SX@defaultWD[2]{%
  \afterassignment\SX@def@WD\dimen@#2\linewidth\relax{#1}}
\newcommand*\SX@def@WD{}
\def\SX@def@WD#1\relax#2{\edef#2{\the\dimen@}}
%    \end{macrocode}
% \end{macro}
% Additional keys.
% \changes{v0.1a}{2004/11/14}{``hpos'' and ``vpos'' added,
% ``pos'' removed (RN).}
% \changes{v0.1f}{2004/12/06}{``lstpreset'' added. (RN).}
% \changes{v0.1h}{2005/06/03}{``codefile'' added. (RN).}
%    \begin{macrocode}
\lst@Key{pos}\relax{\def\SX@pos{#1}}
\lst@Key{width}\relax{\def\SX@width{#1}}
\lst@Key{hsep}\relax{\@tempdima=#1\relax\edef\SX@hsep{\the\@tempdima}}
\lst@Key{vsep}\relax{\@tempdima=#1\relax\edef\SX@vsep{\the\@tempdima}}
\lst@Key{overhang}\relax{\def\SX@overhang{#1}}
\lst@Key{wide}f[t]{\lstKV@SetIf{#1}\if@SX@wide}
\lst@Key{rframe}\relax{\def\SX@rframe{#1}}
\lst@Key{preset}\relax{\def\SX@preset{#1}}
\newcommand*\SX@scaled{}
\lst@Key{scaled}{?}[!]{\def\SX@scaled{#1}}
%    \end{macrocode}
% \changes{v0.1h}{2005/06/03}{``lstpreset'' renamed to ``explpreset''
% (RN).}
% \changes{v0.3l}{2014/01/19}{Option ``scaled'' and \cmd{\SX@scaled}
% added (RN).}
%    \begin{macrocode}
\lst@Key{explpreset}\relax{\def\SX@explpreset{#1}}
\lst@Key{codefile}\relax{\def\SX@codefile{#1}}
\newif\if@SX@rangeaccept \@SX@rangeacceptfalse
\newif\if@SX@varwidth \@SX@varwidthfalse
\newif\if@SX@wide \@SX@widefalse
\newif\if@SX@attachfile \@SX@attachfilefalse
%    \end{macrocode}
% \changes{v0.1j}{2005/06/12}{``rangeaccept'' added (RN).}
% \changes{v0.1k}{2005/06/13}{Some bug corrections (RN).}
%    \begin{macrocode}
\lst@Key{rangeaccept}f[t]{\lstKV@SetIf{#1}\if@SX@rangeaccept}
%    \end{macrocode}
% \changes{v0.2a}{2005/07/16}{``varwidth'' and ``justification'' added (RN).}
% \changes{v0.1l}{2005/06/19}{``graphic'' added (RN).}
% \changes{v0.3a}{2005/08/20}{``attachfile'' added (RN).}
% \changes{v0.3n}{2016/11/20}{Define \cmd{\ResultBox} etc.}
%    \begin{macrocode}
\lst@Key{varwidth}f[t]{\lstKV@SetIf{#1}\if@SX@varwidth}
\lst@Key{justification}\relax{\def\SX@justification{#1}}
\lst@Key{attachfile}f[t]{\lstKV@SetIf{#1}\if@SX@attachfile}
\newcommand*\SX@graphicname{}%
\newcommand*\SX@graphicparam{}%
\lst@Key{graphic}{}[]{%
  \lstKV@OptArg[width=\linewidth]{#1}{%
    \edef\SX@graphicparam{##1}\edef\SX@graphicname{##2}%
  }%
}%
\newbox\SX@ResBox
\newcommand\ResultBox{} \let\ResultBox=\fbox
\newdimen\ResultBoxSep  \ResultBoxSep=\fboxsep
\newdimen\ResultBoxRule \ResultBoxRule=\fboxrule
\newcommand*\SX@pos{}
\newcommand*\SX@width{}
\newcommand*\SX@hsep{}
\newcommand*\SX@vsep{}
\newcommand*\SX@overhang{}
\newcommand*\SX@rframe{}
\newcommand\SX@preset{}
\newcommand*\SX@explpreset{}
%    \end{macrocode}
% \changes{v0.3g}{2006/09/05}{Missing \cmd{\newcommand} for
%   \cmd{\SX@@explpreset} added (RN).}
%    \begin{macrocode}
\newcommand*\SX@@explpreset{}
\newcommand*\SX@codefile{}\edef\SX@codefile{\jobname.tmp}
\newcommand*\SX@justification{\raggedright}
%    \end{macrocode}
% \begin{macro}{\SX@@preset}
% Contains some redefinitions of \LaTeX\ macros and environments to do
% nothing. \cmd{\SX@@preset} will be called just before
% typesetting the result of the example code. More can be added with
% the user key ``|preset=...|''.
% \changes{v0.3e}{2006/04/05}{More redefinitions added (RN).}
% \changes{v0.3p}{2020/05/04}{Remove extra treatment of `figure'/`table' (RN).}
%    \begin{macrocode}
\newcommand*\SX@@preset{%
  \renewcommand\documentclass[2][]{\SX@eat@version}%
  \renewcommand\usepackage[2][]{\SX@eat@version}%
  \renewenvironment{document}{}{}%
  \renewcommand\cite[1][]{}%
  \let\tableofcontens\relax \let\listoffigures\relax
  \let\listoftables\relax \let\printindex\relax
  \let\listfiles\relax \let\nofiles\relax
  \let\index\@gobble \let\label\@gobble
  \let\refstepcounter=\@gobble
  \let\bibliography\@gobble
  \let\pagestyle\@gobble \let\thispagestyle\@gobble
  %%\let\immediate\relax \let\write\@gobbletwo
  %%\let\closeout\@gobble \let\@@input\@gobble
  \renewcommand\marginpar[2][]{}%
  \renewcommand\footnote[2][]{}%
  \let\@footnotetext\@gobble
  %%\abovedisplayskip=\z@
  %%\abovedisplayshortskip=\z@
}
\newcommand*\SX@eat@version[1][]{}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\isSX@odd}
%  Parameter |#1| is executed on odd pages, parameter |#2| on
%  even pages.
%  \changes{v0.3p}{2020/05/04}{Replace \cmd{\isodd} with
%  \cmd{\ifodd}\cmd{\getpagerefnumber} (remove package `ifthen') (RN).}
%    \begin{macrocode}
\newif\ifSX@wasodd
\if@twoside
  \newcommand*\isSX@odd{%
    \begingroup
      \ifodd\getpagerefnumber{\SX@IDENT}%
        \aftergroup\SX@wasoddtrue
      \else
        \aftergroup\SX@wasoddfalse
      \fi
    \endgroup
    \ifSX@wasodd
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
  }
\else
  \SX@wasoddtrue
  \newcommand*\isSX@odd[2]{#1}
\fi
%    \end{macrocode}
%  The call of \cmd{\isSX@odd} sets also \cmd{\ifSX@wasodd} to true or
%  false. If it's clear that no page break occurs, \cmd{\ifSX@wasodd}
%  can be used.
%    \begin{macrocode}
\newcounter{ltxexample}
\newcommand*{\SX@IDENT}{SX@\number\value{ltxexample}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SX@attachfile}
% \changes{v0.3a}{2005/08/20}{Attach file functionality (with pdf\TeX)
%   added (RN).}
%    \begin{macrocode}
\newcommand*\SX@attachfile{%
  \if@SX@attachfile
    \attachfile[mimetype=text/plain,subject={example \theltxexample}]%
      {\SX@codefile}{}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SX@put@t/b/l/r/o/i}
% Six macros for positioning |#2| (result) and |#3| (code). The result
% can be
% \underline{a}bove, \underline{b}elow, \underline{l}eft or
% \underline{r}ight of the code area or on the \underline{o}uter or
% \underline{i}nner side. Parameter |#1| is the width of the result.
% \changes{v0.1b}{2004/11/18}{Positioning the captions more independend
%   of the result and code area (RN).}
% \changes{v0.1c}{2004/11/19}{Commands \cmd{\SX@KillAboveCaptionskip} and
%   \cmd{\SX@KillBelowCaptionskip} added (RN).}
% \changes{v0.1k}{2005/06/13}{Change [a]bove to [t]op (RN).}
%    \begin{macrocode}
\newcommand*\SX@put@t[3]{%
  \SX@ResultArea{\linewidth}{#2}\endgraf\pagebreak[2]%
  \@tempdima=\dimexpr\SX@vsep\vskip\@tempdima
  \SX@CodeArea{\linewidth}{#3}%
}
\newcommand*\SX@put@b[3]{%
  \SX@CodeArea{\linewidth}{#3}\endgraf\pagebreak[2]%
  \@tempdima=\dimexpr\SX@vsep\vskip\@tempdima
  \SX@ResultArea{\linewidth}{#2}%
}
\newcommand*\SX@put@l[3]{%
  \@tempdimc=\dimexpr\linewidth-#1-\SX@hsep % 
  \SX@ResultArea{#1}{#2}\hfill\SX@CodeArea{\@tempdimc}{#3}%
}
\newcommand*\SX@put@r[3]{%
  \@tempdimc=\dimexpr\linewidth-#1-\SX@hsep %
  \SX@CodeArea{\@tempdimc}{#3}\hfill\SX@ResultArea{#1}{#2}%
}
\newcommand*\SX@put@o[3]{%
  \@nameuse{SX@put@\ifSX@wasodd r\else l\fi}{#1}{#2}{#3}%
}
\newcommand*\SX@put@i[3]{%
  \@nameuse{SX@put@\ifSX@wasodd l\else r\fi}{#1}{#2}{#3}%
}
\newcommand\SX@ResultArea[2]{%
  \SX@justification\@tempdima=\dimexpr #1 %
  \parbox\@tempdima{#2}%
}
\newcommand\SX@CodeArea[2]{%
  \@tempdima=\dimexpr #1 %
  \sbox\@tempboxa{\parbox\@tempdima{#2}}%
  \@tempdima=\dp\@tempboxa\usebox\@tempboxa
  \rlap{\raisebox{-\@tempdima}[0pt][0pt]{\SX@attachfile}}%
}
\newcommand*\SX@KillAboveCaptionskip{%
  \ifx\lst@caption\@empty\else
    \lst@IfSubstring t\lst@captionpos
      {\vskip-\abovecaptionskip}{}%
  \fi
}
\newcommand*\SX@KillBelowCaptionskip{%
  \ifx\lst@caption\@empty\else
    \lst@IfSubstring b\lst@captionpos
      {\vskip-\belowcaptionskip}{}%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \changes{v0.1h}{2005/06/03}{Renamed from ``example'' to ``LTXexample' (RN).}
% \changes{v0.3n}{2016/11/20}{Prevent utf8 encoding errors}
% \begin{environment}{LTXexample}
%    \begin{macrocode}
\lstnewenvironment{LTXexample}[1][]{%
  \@temptokena{#1}%
  \begingroup
%    \end{macrocode}
% For "codefile=..."/"graphic=..." if \cmd{\theltxexample} or
% \cmd{\thelstlisting} is part of the filename.
%    \begin{macrocode}
  \advance\c@ltxexample\@ne \advance\c@lstlisting\@ne
  \expandafter\lstset\expandafter{\SX@explpreset,#1}%
  \edef\x{\endgroup
    \def\noexpand\SX@codefile{\SX@codefile}%
    \def\noexpand\SX@graphicname{\SX@graphicname}%
    \def\noexpand\SX@graphicparam{\SX@graphicparam}}%
  \x
  \xdef\SX@@explpreset{\the\@temptokena,codefile=\SX@codefile,%
    graphic={[\SX@graphicparam]{\SX@graphicname}}}%
  \setbox\@tempboxa=\hbox\bgroup
  \lst@BeginWriteFile{\SX@codefile}%
}
{%
  \lst@EndWriteFile\egroup
  \SX@put@code@result
}
%    \end{macrocode}
% \end{environment}
% \changes{v0.1i}{2005/06/07}{Better caption positioning and
%   correct distance between the parts (RN).}
% \changes{v0.1m}{2005/06/25}{Problem related to \cmd{\label}/\cmd{\ref}
%   solved (RN).}
% \changes{v0.2a}{2005/07/16}{``varwidth'' package used (RN).}
% \changes{v0.2b}{2005/07/30}{Check if \cmd{\SX@put@}\texttt{?}
%   is defined (RN).}
% \begin{macro}{\SX@put@code@result}
%    \begin{macrocode}
\newcommand*\SX@put@code@result{%
  \begingroup
    \expandafter\lstset\expandafter{\SX@explpreset}%
    \expandafter\lstset\expandafter{\SX@@explpreset}%
%    \end{macrocode}
% Use listings floating procedure if necessary.
%    \begin{macrocode}
    \ifx\lst@float\relax\else
      \edef\@tempa{\noexpand\lst@beginfloat{lstlisting}[\lst@float]}
      \expandafter\@tempa
    \fi
    \ifx\lst@caption\@empty
      \lstset{nolol=true}%
    \fi
    \if@SX@wide\def\SX@overhang{\marginparwidth+\marginparsep}\fi
    \trivlist\item\relax
      \stepcounter{ltxexample}\label{\SX@IDENT}%
%    \end{macrocode}
% Make \cmd{\SX@width} a real dimension if the unit is missing.
%    \begin{macrocode}
      \SX@defaultWD\SX@width{\SX@width}%
%    \end{macrocode}
% Set the default width if necessary.
%    \begin{macrocode}
      \ifdim\SX@width<\z@
        \@tempswatrue
        \def\@tempa{t}%
        \ifx\@tempa\SX@pos\@tempswafalse\fi
        \def\@tempa{b}%
        \ifx\@tempa\SX@pos\@tempswafalse\fi
        \@tempdima=\dimexpr\linewidth+\SX@overhang %
        \if@tempswa\@tempdima=.5\@tempdima\fi%
        \edef\SX@width{\the\@tempdima}%
      \fi
%    \end{macrocode}
% Correct \cmd{\SX@width} if a frame is requested.
% \changes{v0.3n}{2016/11/20}{Use \cmd{\ResultBox}}
%    \begin{macrocode}
      \ifx\SX@rframe\@empty
        \long\def\SX@frame##1{##1}%
      \else
        \let\SX@frame\ResultBox
        \@tempdima=\dimexpr\SX@width-2\ResultBoxSep-2\ResultBoxRule %
        \edef\SX@width{\the\@tempdima}%
      \fi
      \isSX@odd{\def\@tempa{l}}{\def\@tempa{r}}%
      \makebox[\linewidth][\@tempa]{%
        \parbox{\dimexpr\linewidth+\SX@overhang}{%
%    \end{macrocode}
% \cmd{\SX@codefile} (|\jobname.tmp|) is not nessesary for the filelist.
% \changes{v0.3m}{2016/08/05}{Wrong assignement for \cmd{\lst@belowskip} (RN).}
%    \begin{macrocode}
          \let\@addtofilelist\@gobble
          \let\lst@ifdisplaystyle=\iftrue
          \SX@KillAboveCaptionskip\lst@MakeCaption{t}%
%    \end{macrocode}
% Use the ``natural'' width of the result code if ``varwidth'' is
% true.
% \changes{v0.3k}{2013/03/21}{Setting \cmd{\lst@MakeCaption} to \cmd{\@gobble}
%    again (prevent multiply defined labels; label key)}.
% \changes{v0.3p}{2020/05/04}{Let's leave \cmd{\lst@MakeCaption} untouched (RN).}
% \changes{v0.3j}{2012/09/22}{Setting \cmd{\lst@MakeCaption} to
%    was a bad idea for hyperlinks. Group added to
%   varwidth environment. (Suggestions by Ulrike Fischer.).}
%    \begin{macrocode}
          \setbox\SX@ResBox\hbox{%
            \fboxsep=\ResultBoxSep
            \fboxrule=\ResultBoxRule
            \SX@frame{%
              \@nameuse{\if@SX@varwidth varwidth\else minipage\fi}%
                  \SX@width\relax
                \begingroup
                  \SX@resultInput
                \endgroup
              \@nameuse{end\if@SX@varwidth varwidth\else minipage\fi}}}%
          \edef\SX@width{\the\wd\SX@ResBox}%
          \@ifundefined{SX@put@\SX@pos}%
            {\@latex@error{Parameter `\SX@pos' undefined}\@ehd}%
          {\@nameuse{SX@put@\SX@pos}%
            {\SX@width}{\box\SX@ResBox}{\SX@codeInput}}%
          \lst@MakeCaption{b}\SX@KillBelowCaptionskip
        }%
      }%
    \endtrivlist
    \ifx\lst@float\relax\else\expandafter\lst@endfloat\fi
    \gdef\SX@@explpreset{}%
  \endgroup
}
%    \end{macrocode}
% \end{macro}
% \changes{v0.3g}{2006/09/05}{\cmd{\SX@ProcessResult} is now working
% correctly
% using \cmd{\readline} and \cmd{\scantokens}. Thanks to Ulrich Diez
% for help (RN).}
%    \begin{macrocode}
\newcommand\SX@SkipToFirst{%
  \ifeof\@inputcheck\else
    \ifnum \lst@lineno=\lst@firstline\else
      \readline\@inputcheck to\SX@tempa
      \typeout{IGNORE (\the\lst@lineno)}%
      \global\advance\lst@lineno\@ne
      \SX@SkipToFirst
    \fi
  \fi
}
\newcommand\SX@ProcessResult{%
  \ifeof\@inputcheck
    \let\SX@tempb\relax
  \else
    \let\SX@tempb\SX@ProcessResult
    \ifnum \lst@lineno>\lst@lastline\relax
      \ifx\lst@linerange\@empty
        \let\SX@tempb\relax
      \else
        \lst@GetLineInterval
        \SX@SkipToFirst
      \fi
    \else
      \readline\@inputcheck to\SX@tempa
      \typeout{READ (\the\lst@lineno)}%
      \expandafter\g@addto@macro
        \expandafter\SX@lines\expandafter{\SX@tempa^^J}%
      \global\advance\lst@lineno\@ne
    \fi
  \fi
  \SX@tempb
}
%    \end{macrocode}
% \begin{macro}{\SX@input}
% \changes{v0.1j}{2005/06/12}{For ranges of lines (RN).}
%    \begin{macrocode}
\newcommand\SX@input[1]{%
  \begingroup
    \IfFileExists{#1}{}%
    {%
      \filename@parse{#1}%
      \ifx\filename@ext\relax \def\filename@ext{tex}\fi
      \@latexerr{File
        `\filename@area\filename@base.\filename@ext' not found.^^J^^J}\@ehd%
    }%
    \openin\@inputcheck#1
    \lsthk@PreSet\let\lst@linerange\@empty\global\lst@lineno\@ne
    \expandafter\lstset\expandafter{\SX@@explpreset}%
    \ifx\lst@linerange\@empty
      \edef\lst@linerange{{\lst@firstline}-{\lst@lastline},}%
    \fi
    \lst@GetLineInterval
    \SX@Info
    \newlinechar=`\^^J\relax
    \SX@SkipToFirst\let\SX@lines\@empty
    \SX@ProcessResult
    \closein\@inputcheck
    \scantokens\expandafter{\SX@lines}%
  \endgroup
}
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
\newcommand*\SX@Info{%
  \typeout{--------------------------------}%
  \typeout{pos=\SX@pos}%
  \typeout{width=\SX@width}%
  \typeout{hsep=\SX@hsep}%
  \typeout{vsep=\SX@vsep}%
  \typeout{overhang=\SX@overhang}%
  \typeout{rframe=\SX@rframe}%
  \typeout{codefile=\SX@codefile}%
  \@ifundefined{lst@firstline}{}%
    {\typeout{\string\lst@firstline=\lst@firstline}}%
  \@ifundefined{lst@lastline}{}%
    {\typeout{\string\lst@lastline=\lst@lastline}}%
  \@ifundefined{lst@linerange}{}%
    {\typeout{\string\lst@linerange=\lst@linerange}}%
  \typeout{\string\if@SX@wide=\if@SX@wide TRUE\else FALSE\fi}%
  \typeout{\string\if@SX@rangeaccept=\if@SX@rangeaccept TRUE\else FALSE\fi}%
  \typeout{\string\if@SX@varwidth=\if@SX@varwidth TRUE\else FALSE\fi}%
  \typeout{graphicfile=\SX@graphicname, graphicparameter=[\SX@graphicparam]}%
  \typeout{--------------------------------}%
}
\providecommand*\MakePercentIgnore{\catcode`\%9\relax}
\providecommand*\MakePercentComment{\catcode`\%14\relax}
%    \end{macrocode}
% \begin{macro}{\SX@resultInput}
% \changes{v0.3b}{2005/10/15}{Input of result code now inside a group;
% \cmd{\makeatother} added (RN).}
% \changes{v0.3c}{2005/10/15}{Wrong catcode for newline char corrected (RN).}
% \changes{v0.3d}{2005/12/06}{Missing \cs{par} added (RN).}
% \changes{v0.3l}{2014/01/19}{Code for ``scaled'' option (RN).}
%    \begin{macrocode}
\newcommand*\SX@resultInput{%
  \ifx\SX@graphicname\@empty
    \begingroup
      \MakePercentComment\makeatother\catcode`\^^M=5\relax
      \SX@@preset\SX@preset
      \if@SX@rangeaccept
       \let\SX@tempa=\SX@input
      \else
       \let\SX@tempa=\input
      \fi
      \if\SX@scaled ?%
        \let\SX@tempb=\@firstofone
      \else
        \if\SX@scaled !%
          \def\SX@tempb##1{\resizebox{\SX@width}{!}{##1}}%
        \else
          \def\SX@tempb##1{\scalebox{\SX@scaled}{##1}}%
        \fi
      \fi
      \let\SX@lst@Init=\lst@Init
%    \end{macrocode}
%  \changes{v0.3p}{2020/05/04}{Better handling of floats (RN).}
%  \changes{v0.3q}{2020/05/06}{Floats should always be numbered 0 (RN).}      
%  Prevents float environments from floating. This is not enough for floating
%  listing environments! Why?
%    \begin{macrocode}
      \def\@xfloat##1[##2]{%
        \def\@captype{##1}% 
        \@namedef{the\@captype}{0}%
        \@float@HH{##1}[H]}%
%    \end{macrocode}
%  Special handling of floating listing environments.
%    \begin{macrocode}
      \def\lst@Init{%
        \let\lst@float=\relax
        \setcounter\@captype{-1}%
        \SX@lst@Init
      }
%    \end{macrocode}
%  Typeset the Code.
%    \begin{macrocode}
      \SX@tempb{\SX@tempa{\SX@codefile}}\par
%    \end{macrocode}
%  Restore the regular numbering of floats outside of `LTXexample'. 
%    \begin{macrocode}
    \endgroup
  \else
    \expandafter\includegraphics\expandafter[\SX@graphicparam]%
      {\SX@graphicname}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SX@codeInput}
%    \begin{macrocode}
\newcommand*\SX@codeInput{%
%    \end{macrocode}
% Without a caption entry the command \cmd{\lstinputlisting} adds the
% filename to the ``list of listings'' (lol). This should be avoided.
% \changes{v0.1d}{2004/11/20}{\cmd{\lstset}|{nolol}| and
%   \cmd{\lstset}|{float=false}| added (RN).}
% \changes{v0.1h}{2005/06/03}{\cmd{\lstset}|{float=false}| removed (RN).}
%    \begin{macrocode}
  \begingroup
%    \end{macrocode}
% The default parameters for all examples.
%    \begin{macrocode}
  \expandafter\lstset\expandafter{\SX@explpreset}%
%    \end{macrocode}
% If "numbers=none" then margin dimensions should be zero.
%    \begin{macrocode}
    \expandafter\lstset\expandafter{\SX@@explpreset}%
    \ifx\lst@PlaceNumber\@empty
      \g@addto@macro\SX@@explpreset{,xleftmargin=0pt,xrightmargin=0pt}%
    \fi
    \SX@Info
    \expandafter\lstinputlisting\expandafter[\SX@@explpreset,nolol=true,%
      caption={},belowskip=\z@,aboveskip=\z@,float=false]{\SX@codefile}%
  \endgroup
}%
%    \end{macrocode}
% \end{macro}
% \changes{v0.1h}{2005/06/03}{New macro \cmd{\LTXinputExample} (RN).}
%    \begin{macrocode}
\newcommand*\LTXinputExample[2][]{%
  \g@addto@macro\SX@@explpreset{float=false,#1,codefile=#2}%
  \SX@put@code@result}%
%    \end{macrocode}
% All the default values.
%    \begin{macrocode}
\lstset{explpreset={numbers=left,numberstyle=\tiny,numbersep=.3em,
%    \end{macrocode}
% Negative width means defaults.
%    \begin{macrocode}
  xleftmargin=1em,columns=flexible,language=[LaTeX]TEX},pos=l,width=-99pt,
  overhang=0pt,hsep=\columnsep,vsep=\bigskipamount,rframe=single}
%    \end{macrocode}
% \changes{v0.3k}{2013/03/21}{Definition for ``hyperref''
%   (suggested by Heiko Oberdiek)}.
%    \begin{macrocode}
\AtBeginDocument{%
  \def\theHlstnumber{\thelstlisting.\arabic{lstnumber}.\lst@neglisting}%
}
%    \end{macrocode}
% \changes{v0.1g}{2004/12/10}{Loading of |showexpl.cfg| now at end of
%   package (RN).}
% Changing the defaults possible in |showexpl.cfg|.
%    \begin{macrocode}
\InputIfFileExists{showexpl.cfg}{}{}
%    \end{macrocode}
%
% \Finale
\endinput