Debian Codegraph @

texmf-dist/source/latex/showexpl/showexpl.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2004 -- by Rolf Niepraschk <[email protected]>
4 % --------------------------------------------------------------------
5 %
6 % This file may be distributed and/or modified under the
7 % conditions of the LaTeX Project Public License, either version 1.2
8 % of this license or (at your option) any later version.
9 % The latest version of this license is in:
10 %
11 % http://www.latex-project.org/lppl.txt
12 %
13 % and version 1.2 or later is part of all distributions of LaTeX
14 % version 1999/12/01 or later.
15 %
16 % \fi
17 %
18 % \iffalse
19 %<*driver>
20 \ProvidesFile{showexpl.dtx}
21 %</driver>
22 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
23 %<package>\ProvidesPackage{showexpl}
24 %<*package>
25 [2021/01/06 v0.3s Typesetting example code (RN)]
26 %</package>
27 %
28 %<*driver>
29 \documentclass[a4paper]{ltxdoc}
30 \usepackage{showexpl}
31 \EnableCrossrefs
32 \CodelineIndex
33 \RecordChanges
34 \begin{document}
35 \DocInput{showexpl.dtx}
36 \PrintChanges
37 \PrintIndex
38 \end{document}
39 %</driver>
40 % \fi
41 %
42 % \CheckSum{902}
43 %
44 % \CharacterTable
45 % {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
46 % 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
47 % Digits \0\1\2\3\4\5\6\7\8\9
48 % Exclamation \! Double quote \" Hash (number) \#
49 % Dollar \$ Percent \% Ampersand \&
50 % Acute accent \' Left paren \( Right paren \)
51 % Asterisk \* Plus \+ Comma \,
52 % Minus \- Point \. Solidus \/
53 % Colon \: Semicolon \; Less than \<
54 % Equals \= Greater than \> Question mark \?
55 % Commercial at \@ Left bracket \[ Backslash \\
56 % Right bracket \] Circumflex \^ Underscore \_
57 % Grave accent \` Left brace \{ Vertical bar \|
58 % Right brace \} Tilde \~}
59 %
60 %
61 % \changes{v0.1a}{2004/11/14}{Initial version}
62 %
63 % \GetFileInfo{showexpl.dtx}
64 %
65 % \DoNotIndex{\newcommand,\renewcommand,\newenvironment,\renewenvironment}
66 % \DoNotIndex{\usepackage,\documentclass,\nofiles,\bibliogaphy}
67 % \DoNotIndex{\tiny,\tableofcontens}
68 % \DoNotIndex{\newif,\newcounter,\linewidth,\listfiles}
69 % \DoNotIndex{\providecommand,\def,\edef,\let,\gdef,\xdef,\global,\newtoks}
70 % \DoNotIndex{\RequirePackage,\DeclareOption,\ProcessOptions,\ExecuteOptions}
71 % \DoNotIndex{\@nameuse,\value,\input,\InputIfFileExists}
72 % \DoNotIndex{\@ifdefinable,\@ifundefined,\@percentchar}
73 % \DoNotIndex{\AtBeginDocument,\AtEndOfPackage,\PassOptionsToPackage}
74 % \DoNotIndex{\CurrentOption,\jobname}
75 % \DoNotIndex{\PackageError,\PackageWarning,\PackageWarningNoLine,\PackageInfo}
76 % \DoNotIndex{\MessageBreak,\typeout}
77 % \DoNotIndex{\z@,\z@skip,\p@,\@ne,\tw@,\thr@@,\@iv,\two@fourteen,\strip@pt}
78 % \DoNotIndex{\the,\if,\else,\or,\fi,\ifnum,\ifdim,\ifcase,\ifodd}
79 % \DoNotIndex{\advance,\multiply,\divide,\ht,\dp,\wd,\catcode}
80 % \DoNotIndex{\@tfor,\do,\bgroup,\egroup,\ifx,\iftrue,\iffalse}
81 % \DoNotIndex{\csname,\endcsname,\begingroup,\endgroup}
82 % \DoNotIndex{\expandafter,\afterassignment,\noexpand}
83 % \DoNotIndex{\@tempdima,\@tempdimb,\@tempdimc,\@tempcnta,\@tempcntb}
84 % \DoNotIndex{\@halfwidth,\@wholewidth,\unitlength}
85 % \DoNotIndex{\@clnwd,\@clnht,\@ovdx,\@ovdy,\@ovro,\@ovri,\@ovxx,\@ovyy}
86 % \DoNotIndex{\@xarg,\@xdim,\@yarg,\@ydim,\@linelen,\@dashdim,\dimen@}
87 % \DoNotIndex{\reserved@a,\relax,\protect,\long,\space}
88 % \DoNotIndex{\if@tempswa,\@tempswatrue,\@tempswafalse,\@tempa}
89 % \DoNotIndex{\@tempboxa,\@tempboxb,\show}
90 % \DoNotIndex{\@empty,\%,\typeout,\vspace,\vskip,\the,\hbox,\par}
91 % \DoNotIndex{\minipage,\endminipage,\trivlist,\endtrivlist}
92 % \DoNotIndex{\parbox,\setbox,\setlength,\hfill,\item,\number}
93 % \DoNotIndex{\x}
94 % \DoNotIndex{\SX@put@a,\SX@put@b,\SX@put@l,\SX@put@r,\SX@put@o,\SX@put@i}
95 %
96 % ^^A\DoNotIndex{\usepackage,\documentclass,\tableofcontens,\printindex}
97 %
98 % \title{The \textsf{showexpl} package\thanks{This document
99 % corresponds to \textsf{showexpl}~\fileversion, dated \filedate.}}
100 % \author{Rolf Niepraschk (\texttt{[email protected]})}
101 % \date{\filedate}
102 %
103 % \maketitle
104 %
105 % \section{Introduction}
106 %
107 % The documentation of a \LaTeX{} package is by far more readable if there
108 % are examples of the commands' and environments' usage. The best way to do
109 % that is to give a comparison of the \LaTeX{} code and the formatted
110 % output. \textsf{showexpl} is a package for doing that comparison, it is
111 % based on the package \textsf{listings} which provides a good typesetted
112 % source code with emphasised keywords and so on.
113 %
114 % \section{Usage}
115 %
116 % You can use \textsf{showexpl} like every other package by putting the
117 % line
118 % \begin{verbatim}
119 % \usepackage{showexpl}
120 % \end{verbatim}
121 % \vspace{-\baselineskip}
122 % in your source code. \textsf{showexpl} doesn't know any options by
123 % itself, but all options for the underlying packages
124 % (\textsf{listings} and \textsf{graphicx}) will be passed to the
125 % respective packages.
126 %
127 % \textsf{showexpl} provides one command and one environment:
128 % \begin{itemize}
129 % \item |\LTXinputExample| and
130 % \item \textsf{LTXexample}
131 % \end{itemize}
132 %
133 % \DescribeMacro{\LTXinputExample}
134 % The syntax of |\LTXinputExample| is given by
135 % \begin{quote}
136 % |\LTXinputExample[|\meta{key val list}|]{|\meta{file}|}|
137 % \end{quote}
138 %
139 % \DescribeEnv{LTXexample}
140 % The syntax of the environment \textsf{LTXexample} is given by
141 % \begin{quote}
142 % |\begin{LTXexample}[|\meta{key val list}|]|\ldots|\end{LTXexample}|
143 % \end{quote}
144 %
145 % The set of options represented by \meta{key val list} is the same for
146 % both the command and the environment, the options are described in the
147 % following:
148 % \begin{description}
149 % \item[attachfile] Boolean valued key, default value: false. If set to
150 % true the sourcecode will be attached to the \textsf{.pdf}
151 % file---presumed that the document is processed by |pdflatex|.
152 % \item[codefile] Name of the (temporary) file that contains the code
153 % which will be formatted as source code. The default value is |\jobname.tmp|.
154 % \item[explpreset] A \meta{key val list} which serves for presetting the
155 % properties of the formatting of the source code, for values see the
156 % documentation of the \textsf{listings} package. The default value is
157 %
158 % \item[graphic] Name of a (graphic) file. This file---if present---will
159 % be included and displayed instead of the formatted code. The default value is empty.
160 % \item[hsep] Defines the horizontal distance between the source code and the
161 % formatted text.
162 % \item[justification] Defines the justification of the formatted text:
163 % reasonable values are |\raggedleft|, |\raggedright|, |\centering|. The
164 % default value is |\raggedright|.
165 % \item[overhang] A \textit{dimen}-value that defines the amount by which
166 % the formatted text and the source code can overlap the print space. The
167 % default value is 0\,pt.
168 % \item[pos:] Defines the relative position of the formatted text
169 % relating to the source code. Allowed values are |t|, |b|, |l|, |r|,
170 % |o|, and |i| for top, bottom, left, right, outer, and inner. The last
171 % values give sense only for two-sided printing, where there are outer
172 % and inner margins of a page. The default value is |l|.
173 % \item[preset] Any \TeX\ code executed before the sample code but
174 % not visible in the listings area.
175 % \item[rangeaccept] Boolean valued key, default value is false. If set
176 % to true, one can define ranges of lines that will be excerpted from
177 % the source code.
178 % \item[rframe] Defines the form of the frame around the formatted
179 % text. With a non-empty value (e.\,g. ``single'') a simple frame
180 % will be drawn. In the future more kinds of frames will be supported.
181 % The default value is empty (no frame).
182 % \item[varwidth] Boolean valued key, default value is false. If set to
183 % true, the formatted text is set with its ``natural'' width instead of a
184 % fixed width as given by the value of the option |width|.
185 % \item[vsep] Defines the vertical distance between the source code and the
186 % formatted text.
187 % \item[wide] Boolean valued key, default value is false. If set to
188 % true, the source code and the formatted text overlap the print space
189 % and the margin area.
190 % \item[width] A \meta{dimen} value that defines the width of the
191 % formatted text. The default value depends of the relative positions of
192 % the source code and the formatted text.
193 % \item[scaled] Without a value the formatted text will be scaled to fit
194 % the given width of the result area. With a number as value the formatted
195 % text will be scaled by this number.
196 % \end{description}
197 % In addition to these options the kind of the result box (default: |\fbox|)
198 % can be changed. For example:
199 % \begin{verbatim}
200 % \renewcommand\ResultBox{\fcolorbox{green}{lightgray}}
201 % \setlength\ResultBoxSep{5mm}% default: \fboxsep
202 % \setlength\ResultBoxRule{2mm}% default: \fboxrule
203 % \end{verbatim}
204 %
205 % \StopEventually{}
206 %
207 % \section{Implementation}
208 % \changes{v0.1e}{2004/11/24}{Loading of |showexpl.cfg| added (RN).}
209 % \changes{v0.3p}{2020/05/04}{Remove package `calc' (RN).}
210 % \begin{macrocode}
211 \DeclareOption{final}{%
212 \PassOptionsToPackage{\CurrentOption}{graphicx}%
213 \PassOptionsToPackage{\CurrentOption}{listings}%
214 }%
215 \DeclareOption{draft}{%
216 \PassOptionsToPackage{\CurrentOption}{graphicx}%
217 \PassOptionsToPackage{\CurrentOption}{listings}%
218 }%
219 % \end{macrocode}
220 % \changes{v0.3h}{2007/02/03}{New Option `attachfiles' (RN).}
221 % \begin{macrocode}
222 \DeclareOption{attachfiles}{%
223 \AtBeginDocument{\IfFileExists{attachfile.sty}%
224 {\RequirePackage{attachfile}}{\def\SX@attachfile{}}}
225 }%
226 \DeclareOption*{\PassOptionsToPackage{\CurrentOption}{listings}}
227 \ProcessOptions\relax
228 \RequirePackage{refcount,listings,graphicx,varwidth,float}
229 % \end{macrocode}
230 % We must aktivate code from package \textsf{listings} for writing files.
231 % \begin{macrocode}
232 \lst@RequireAspects{writefile}
233 % \end{macrocode}
234 % \begin{macro}{\SX@defaultWD}
235 % Parameter |#2| is a length or a number. Parameter |#1| is a
236 % macro. After a call of \cmd{\SX@defaultWD} this macro contains the value
237 % of the length or the value of the number multiplied by \cmd{\linewidth}.
238 % \begin{macrocode}
239 \newcommand*\SX@defaultWD[2]{%
240 \afterassignment\SX@def@WD\dimen@#2\linewidth\relax{#1}}
241 \newcommand*\SX@def@WD{}
242 \def\SX@def@WD#1\relax#2{\edef#2{\the\dimen@}}
243 % \end{macrocode}
244 % \end{macro}
245 % Additional keys.
246 % \changes{v0.1a}{2004/11/14}{``hpos'' and ``vpos'' added,
247 % ``pos'' removed (RN).}
248 % \changes{v0.1f}{2004/12/06}{``lstpreset'' added. (RN).}
249 % \changes{v0.1h}{2005/06/03}{``codefile'' added. (RN).}
250 % \begin{macrocode}
251 \lst@Key{pos}\relax{\def\SX@pos{#1}}
252 \lst@Key{width}\relax{\def\SX@width{#1}}
253 \lst@Key{hsep}\relax{\@tempdima=#1\relax\edef\SX@hsep{\the\@tempdima}}
254 \lst@Key{vsep}\relax{\@tempdima=#1\relax\edef\SX@vsep{\the\@tempdima}}
255 \lst@Key{overhang}\relax{\def\SX@overhang{#1}}
256 \lst@Key{wide}f[t]{\lstKV@SetIf{#1}\if@SX@wide}
257 \lst@Key{rframe}\relax{\def\SX@rframe{#1}}
258 \lst@Key{preset}\relax{\def\SX@preset{#1}}
259 \newcommand*\SX@scaled{}
260 \lst@Key{scaled}{?}[!]{\def\SX@scaled{#1}}
261 % \end{macrocode}
262 % \changes{v0.1h}{2005/06/03}{``lstpreset'' renamed to ``explpreset''
263 % (RN).}
264 % \changes{v0.3l}{2014/01/19}{Option ``scaled'' and \cmd{\SX@scaled}
265 % added (RN).}
266 % \begin{macrocode}
267 \lst@Key{explpreset}\relax{\def\SX@explpreset{#1}}
268 \lst@Key{codefile}\relax{\def\SX@codefile{#1}}
269 \newif\if@SX@rangeaccept \@SX@rangeacceptfalse
270 \newif\if@SX@varwidth \@SX@varwidthfalse
271 \newif\if@SX@wide \@SX@widefalse
272 \newif\if@SX@attachfile \@SX@attachfilefalse
273 % \end{macrocode}
274 % \changes{v0.1j}{2005/06/12}{``rangeaccept'' added (RN).}
275 % \changes{v0.1k}{2005/06/13}{Some bug corrections (RN).}
276 % \begin{macrocode}
277 \lst@Key{rangeaccept}f[t]{\lstKV@SetIf{#1}\if@SX@rangeaccept}
278 % \end{macrocode}
279 % \changes{v0.2a}{2005/07/16}{``varwidth'' and ``justification'' added (RN).}
280 % \changes{v0.1l}{2005/06/19}{``graphic'' added (RN).}
281 % \changes{v0.3a}{2005/08/20}{``attachfile'' added (RN).}
282 % \changes{v0.3n}{2016/11/20}{Define \cmd{\ResultBox} etc.}
283 % \begin{macrocode}
284 \lst@Key{varwidth}f[t]{\lstKV@SetIf{#1}\if@SX@varwidth}
285 \lst@Key{justification}\relax{\def\SX@justification{#1}}
286 \lst@Key{attachfile}f[t]{\lstKV@SetIf{#1}\if@SX@attachfile}
287 \newcommand*\SX@graphicname{}%
288 \newcommand*\SX@graphicparam{}%
289 \lst@Key{graphic}{}[]{%
290 \lstKV@OptArg[width=\linewidth]{#1}{%
291 \edef\SX@graphicparam{##1}\edef\SX@graphicname{##2}%
292 }%
293 }%
294 \newbox\SX@ResBox
295 \newcommand\ResultBox{} \let\ResultBox=\fbox
296 \newdimen\ResultBoxSep \ResultBoxSep=\fboxsep
297 \newdimen\ResultBoxRule \ResultBoxRule=\fboxrule
298 \newcommand*\SX@pos{}
299 \newcommand*\SX@width{}
300 \newcommand*\SX@hsep{}
301 \newcommand*\SX@vsep{}
302 \newcommand*\SX@overhang{}
303 \newcommand*\SX@rframe{}
304 \newcommand\SX@preset{}
305 \newcommand*\SX@explpreset{}
306 % \end{macrocode}
307 % \changes{v0.3g}{2006/09/05}{Missing \cmd{\newcommand} for
308 % \cmd{\SX@@explpreset} added (RN).}
309 % \begin{macrocode}
310 \newcommand*\SX@@explpreset{}
311 \newcommand*\SX@codefile{}\edef\SX@codefile{\jobname.tmp}
312 \newcommand*\SX@justification{\raggedright}
313 % \end{macrocode}
314 % \begin{macro}{\SX@@preset}
315 % Contains some redefinitions of \LaTeX\ macros and environments to do
316 % nothing. \cmd{\SX@@preset} will be called just before
317 % typesetting the result of the example code. More can be added with
318 % the user key ``|preset=...|''.
319 % \changes{v0.3e}{2006/04/05}{More redefinitions added (RN).}
320 % \changes{v0.3p}{2020/05/04}{Remove extra treatment of `figure'/`table' (RN).}
321 % \changes{v0.3r}{2020/10/08}{letting \cmd{\refstepcounter} be
322 % \cmd{\stepcounter} (RN).}
323 % \changes{v0.3s}{2021/01/06}{Special handling of cleveref's
324 % \cmd{\refstepcounter} (RN).}
325 % \begin{macrocode}
326 \newcommand*\SX@@preset{%
327 \renewcommand\documentclass[2][]{\SX@eat@version}%
328 \renewcommand\usepackage[2][]{\SX@eat@version}%
329 \renewenvironment{document}{}{}%
330 \renewcommand\cite[1][]{}%
331 \let\tableofcontens\relax \let\listoffigures\relax
332 \let\listoftables\relax \let\printindex\relax
333 \let\listfiles\relax \let\nofiles\relax
334 \let\index\@gobble
335 \expandafter\ifx\csname [email protected]\endcsname\relax
336 \let\refstepcounter=\stepcounter
337 \let\label\@gobble
338 \else
339 \let\cref@old@refstepcounter=\stepcounter
340 \let\cref@old@label=\@gobble
341 \fi
342 \let\bibliography\@gobble
343 \let\pagestyle\@gobble \let\thispagestyle\@gobble
344 %%\let\immediate\relax \let\write\@gobbletwo
345 %%\let\closeout\@gobble \let\@@input\@gobble
346 \renewcommand\marginpar[2][]{}%
347 \renewcommand\footnote[2][]{}%
348 \let\@footnotetext\@gobble
349 %%\abovedisplayskip=\z@
350 %%\abovedisplayshortskip=\z@
351 }
352 \newcommand*\SX@eat@version[1][]{}
353 % \end{macrocode}
354 % \end{macro}
355 % \begin{macro}{\isSX@odd}
356 % Parameter |#1| is executed on odd pages, parameter |#2| on
357 % even pages.
358 % \changes{v0.3p}{2020/05/04}{Replace \cmd{\isodd} with
359 % \cmd{\ifodd}\cmd{\getpagerefnumber} (remove package `ifthen') (RN).}
360 % \begin{macrocode}
361 \newif\ifSX@wasodd
362 \if@twoside
363 \newcommand*\isSX@odd{%
364 \begingroup
365 \ifodd\getpagerefnumber{\SX@IDENT}%
366 \aftergroup\SX@wasoddtrue
367 \else
368 \aftergroup\SX@wasoddfalse
369 \fi
370 \endgroup
371 \ifSX@wasodd
372 \expandafter\@firstoftwo
373 \else
374 \expandafter\@secondoftwo
375 \fi
376 }
377 \else
378 \SX@wasoddtrue
379 \newcommand*\isSX@odd[2]{#1}
380 \fi
381 % \end{macrocode}
382 % The call of \cmd{\isSX@odd} sets also \cmd{\ifSX@wasodd} to true or
383 % false. If it's clear that no page break occurs, \cmd{\ifSX@wasodd}
384 % can be used.
385 % \begin{macrocode}
386 \newcounter{ltxexample}
387 \newcommand*{\SX@IDENT}{SX@\number\value{ltxexample}}
388 % \end{macrocode}
389 % \end{macro}
390 % \begin{macro}{\SX@attachfile}
391 % \changes{v0.3a}{2005/08/20}{Attach file functionality (with pdf\TeX)
392 % added (RN).}
393 % \begin{macrocode}
394 \newcommand*\SX@attachfile{%
395 \if@SX@attachfile
396 \attachfile[mimetype=text/plain,subject={example \theltxexample}]%
397 {\SX@codefile}{}%
398 \fi
399 }
400 % \end{macrocode}
401 % \end{macro}
402 % \begin{macro}{\SX@put@t/b/l/r/o/i}
403 % Six macros for positioning |#2| (result) and |#3| (code). The result
404 % can be
405 % \underline{a}bove, \underline{b}elow, \underline{l}eft or
406 % \underline{r}ight of the code area or on the \underline{o}uter or
407 % \underline{i}nner side. Parameter |#1| is the width of the result.
408 % \changes{v0.1b}{2004/11/18}{Positioning the captions more independend
409 % of the result and code area (RN).}
410 % \changes{v0.1c}{2004/11/19}{Commands \cmd{\SX@KillAboveCaptionskip} and
411 % \cmd{\SX@KillBelowCaptionskip} added (RN).}
412 % \changes{v0.1k}{2005/06/13}{Change [a]bove to [t]op (RN).}
413 % \begin{macrocode}
414 \newcommand*\SX@put@t[3]{%
415 \SX@ResultArea{\linewidth}{#2}\endgraf\pagebreak[2]%
416 \@tempdima=\dimexpr\SX@vsep\vskip\@tempdima
417 \SX@CodeArea{\linewidth}{#3}%
418 }
419 \newcommand*\SX@put@b[3]{%
420 \SX@CodeArea{\linewidth}{#3}\endgraf\pagebreak[2]%
421 \@tempdima=\dimexpr\SX@vsep\vskip\@tempdima
422 \SX@ResultArea{\linewidth}{#2}%
423 }
424 \newcommand*\SX@put@l[3]{%
425 \@tempdimc=\dimexpr\linewidth-#1-\SX@hsep %
426 \SX@ResultArea{#1}{#2}\hfill\SX@CodeArea{\@tempdimc}{#3}%
427 }
428 \newcommand*\SX@put@r[3]{%
429 \@tempdimc=\dimexpr\linewidth-#1-\SX@hsep %
430 \SX@CodeArea{\@tempdimc}{#3}\hfill\SX@ResultArea{#1}{#2}%
431 }
432 \newcommand*\SX@put@o[3]{%
433 \@nameuse{SX@put@\ifSX@wasodd r\else l\fi}{#1}{#2}{#3}%
434 }
435 \newcommand*\SX@put@i[3]{%
436 \@nameuse{SX@put@\ifSX@wasodd l\else r\fi}{#1}{#2}{#3}%
437 }
438 \newcommand\SX@ResultArea[2]{%
439 \SX@justification\@tempdima=\dimexpr #1 %
440 \parbox\@tempdima{#2}%
441 }
442 \newcommand\SX@CodeArea[2]{%
443 \@tempdima=\dimexpr #1 %
444 \sbox\@tempboxa{\parbox\@tempdima{#2}}%
445 \@tempdima=\dp\@tempboxa\usebox\@tempboxa
446 \rlap{\raisebox{-\@tempdima}[0pt][0pt]{\SX@attachfile}}%
447 }
448 \newcommand*\SX@KillAboveCaptionskip{%
449 \ifx\lst@caption\@empty\else
450 \lst@IfSubstring t\lst@captionpos
451 {\vskip-\abovecaptionskip}{}%
452 \fi
453 }
454 \newcommand*\SX@KillBelowCaptionskip{%
455 \ifx\lst@caption\@empty\else
456 \lst@IfSubstring b\lst@captionpos
457 {\vskip-\belowcaptionskip}{}%
458 \fi
459 }
460 % \end{macrocode}
461 % \end{macro}
462 %
463 % \changes{v0.1h}{2005/06/03}{Renamed from ``example'' to ``LTXexample' (RN).}
464 % \changes{v0.3n}{2016/11/20}{Prevent utf8 encoding errors}
465 % \begin{environment}{LTXexample}
466 % \begin{macrocode}
467 \lstnewenvironment{LTXexample}[1][]{%
468 \@temptokena{#1}%
469 \begingroup
470 % \end{macrocode}
471 % For "codefile=..."/"graphic=..." if \cmd{\theltxexample} or
472 % \cmd{\thelstlisting} is part of the filename.
473 % \begin{macrocode}
474 \advance\c@ltxexample\@ne \advance\c@lstlisting\@ne
475 \expandafter\lstset\expandafter{\SX@explpreset,#1}%
476 \edef\x{\endgroup
477 \def\noexpand\SX@codefile{\SX@codefile}%
478 \def\noexpand\SX@graphicname{\SX@graphicname}%
479 \def\noexpand\SX@graphicparam{\SX@graphicparam}}%
480 \x
481 \xdef\SX@@explpreset{\the\@temptokena,codefile=\SX@codefile,%
482 graphic={[\SX@graphicparam]{\SX@graphicname}}}%
483 \setbox\@tempboxa=\hbox\bgroup
484 \lst@BeginWriteFile{\SX@codefile}%
485 }
486 {%
487 \lst@EndWriteFile\egroup
488 \SX@put@code@result
489 }
490 % \end{macrocode}
491 % \end{environment}
492 % \changes{v0.1i}{2005/06/07}{Better caption positioning and
493 % correct distance between the parts (RN).}
494 % \changes{v0.1m}{2005/06/25}{Problem related to \cmd{\label}/\cmd{\ref}
495 % solved (RN).}
496 % \changes{v0.2a}{2005/07/16}{``varwidth'' package used (RN).}
497 % \changes{v0.2b}{2005/07/30}{Check if \cmd{\SX@put@}\texttt{?}
498 % is defined (RN).}
499 % \begin{macro}{\SX@put@code@result}
500 % \begin{macrocode}
501 \newcommand*\SX@put@code@result{%
502 \begingroup
503 \expandafter\lstset\expandafter{\SX@explpreset}%
504 \expandafter\lstset\expandafter{\SX@@explpreset}%
505 % \end{macrocode}
506 % Use listings floating procedure if necessary.
507 % \begin{macrocode}
508 \ifx\lst@float\relax\else
509 \edef\@tempa{\noexpand\lst@beginfloat{lstlisting}[\lst@float]}
510 \expandafter\@tempa
511 \fi
512 \ifx\lst@caption\@empty
513 \lstset{nolol=true}%
514 \fi
515 \if@SX@wide\def\SX@overhang{\marginparwidth+\marginparsep}\fi
516 \trivlist\item\relax
517 \stepcounter{ltxexample}\label{\SX@IDENT}%
518 % \end{macrocode}
519 % Make \cmd{\SX@width} a real dimension if the unit is missing.
520 % \begin{macrocode}
521 \SX@defaultWD\SX@width{\SX@width}%
522 % \end{macrocode}
523 % Set the default width if necessary.
524 % \begin{macrocode}
525 \ifdim\SX@width<\z@
526 \@tempswatrue
527 \def\@tempa{t}%
528 \ifx\@tempa\SX@pos\@tempswafalse\fi
529 \def\@tempa{b}%
530 \ifx\@tempa\SX@pos\@tempswafalse\fi
531 \@tempdima=\dimexpr\linewidth+\SX@overhang %
532 \if@tempswa\@tempdima=.5\@tempdima\fi%
533 \edef\SX@width{\the\@tempdima}%
534 \fi
535 % \end{macrocode}
536 % Correct \cmd{\SX@width} if a frame is requested.
537 % \changes{v0.3n}{2016/11/20}{Use \cmd{\ResultBox}}
538 % \begin{macrocode}
539 \ifx\SX@rframe\@empty
540 \long\def\SX@frame##1{##1}%
541 \else
542 \let\SX@frame\ResultBox
543 \@tempdima=\dimexpr\SX@width-2\ResultBoxSep-2\ResultBoxRule %
544 \edef\SX@width{\the\@tempdima}%
545 \fi
546 \isSX@odd{\def\@tempa{l}}{\def\@tempa{r}}%
547 \makebox[\linewidth][\@tempa]{%
548 \parbox{\dimexpr\linewidth+\SX@overhang}{%
549 % \end{macrocode}
550 % \cmd{\SX@codefile} (|\jobname.tmp|) is not nessesary for the filelist.
551 % \changes{v0.3m}{2016/08/05}{Wrong assignement for \cmd{\lst@belowskip} (RN).}
552 % \begin{macrocode}
553 \let\@addtofilelist\@gobble
554 \let\lst@ifdisplaystyle=\iftrue
555 \SX@KillAboveCaptionskip\lst@MakeCaption{t}%
556 % \end{macrocode}
557 % Use the ``natural'' width of the result code if ``varwidth'' is
558 % true.
559 % \changes{v0.3k}{2013/03/21}{Setting \cmd{\lst@MakeCaption} to \cmd{\@gobble}
560 % again (prevent multiply defined labels; label key)}.
561 % \changes{v0.3p}{2020/05/04}{Let's leave \cmd{\lst@MakeCaption} untouched (RN).}
562 % \changes{v0.3j}{2012/09/22}{Setting \cmd{\lst@MakeCaption} to
563 % was a bad idea for hyperlinks. Group added to
564 % varwidth environment. (Suggestions by Ulrike Fischer.).}
565 % \begin{macrocode}
566 \setbox\SX@ResBox\hbox{%
567 \fboxsep=\ResultBoxSep
568 \fboxrule=\ResultBoxRule
569 \SX@frame{%
570 \@nameuse{\if@SX@varwidth varwidth\else minipage\fi}%
571 \SX@width\relax
572 \begingroup
573 \SX@resultInput
574 \endgroup
575 \@nameuse{end\if@SX@varwidth varwidth\else minipage\fi}}}%
576 \edef\SX@width{\the\wd\SX@ResBox}%
577 \@ifundefined{SX@put@\SX@pos}%
578 {\@latex@error{Parameter `\SX@pos' undefined}\@ehd}%
579 {\@nameuse{SX@put@\SX@pos}%
580 {\SX@width}{\box\SX@ResBox}{\SX@codeInput}}%
581 \lst@MakeCaption{b}\SX@KillBelowCaptionskip
582 }%
583 }%
584 \endtrivlist
585 \ifx\lst@float\relax\else\expandafter\lst@endfloat\fi
586 \gdef\SX@@explpreset{}%
587 \endgroup
588 }
589 % \end{macrocode}
590 % \end{macro}
591 % \changes{v0.3g}{2006/09/05}{\cmd{\SX@ProcessResult} is now working
592 % correctly
593 % using \cmd{\readline} and \cmd{\scantokens}. Thanks to Ulrich Diez
594 % for help (RN).}
595 % \begin{macrocode}
596 \newcommand\SX@SkipToFirst{%
597 \ifeof\@inputcheck\else
598 \ifnum \lst@lineno=\lst@firstline\else
599 \readline\@inputcheck to\SX@tempa
600 \typeout{IGNORE (\the\lst@lineno)}%
601 \global\advance\lst@lineno\@ne
602 \SX@SkipToFirst
603 \fi
604 \fi
605 }
606 \newcommand\SX@ProcessResult{%
607 \ifeof\@inputcheck
608 \let\SX@tempb\relax
609 \else
610 \let\SX@tempb\SX@ProcessResult
611 \ifnum \lst@lineno>\lst@lastline\relax
612 \ifx\lst@linerange\@empty
613 \let\SX@tempb\relax
614 \else
615 \lst@GetLineInterval
616 \SX@SkipToFirst
617 \fi
618 \else
619 \readline\@inputcheck to\SX@tempa
620 \typeout{READ (\the\lst@lineno)}%
621 \expandafter\g@addto@macro
622 \expandafter\SX@lines\expandafter{\SX@tempa^^J}%
623 \global\advance\lst@lineno\@ne
624 \fi
625 \fi
626 \SX@tempb
627 }
628 % \end{macrocode}
629 % \begin{macro}{\SX@input}
630 % \changes{v0.1j}{2005/06/12}{For ranges of lines (RN).}
631 % \begin{macrocode}
632 \newcommand\SX@input[1]{%
633 \begingroup
634 \IfFileExists{#1}{}%
635 {%
636 \filename@parse{#1}%
637 \ifx\filename@ext\relax \def\filename@ext{tex}\fi
638 \@latexerr{File
639 `\filename@area\filename@base.\filename@ext' not found.^^J^^J}\@ehd%
640 }%
641 \openin\@inputcheck#1
642 \lsthk@PreSet\let\lst@linerange\@empty\global\lst@lineno\@ne
643 \expandafter\lstset\expandafter{\SX@@explpreset}%
644 \ifx\lst@linerange\@empty
645 \edef\lst@linerange{{\lst@firstline}-{\lst@lastline},}%
646 \fi
647 \lst@GetLineInterval
648 \SX@Info
649 \newlinechar=`\^^J\relax
650 \SX@SkipToFirst\let\SX@lines\@empty
651 \SX@ProcessResult
652 \closein\@inputcheck
653 \scantokens\expandafter{\SX@lines}%
654 \endgroup
655 }
656 % \end{macrocode}
657 % \end{macro}
658 % \begin{macrocode}
659 \newcommand*\SX@Info{%
660 \typeout{--------------------------------}%
661 \typeout{pos=\SX@pos}%
662 \typeout{width=\SX@width}%
663 \typeout{hsep=\SX@hsep}%
664 \typeout{vsep=\SX@vsep}%
665 \typeout{overhang=\SX@overhang}%
666 \typeout{rframe=\SX@rframe}%
667 \typeout{codefile=\SX@codefile}%
668 \@ifundefined{lst@firstline}{}%
669 {\typeout{\string\lst@firstline=\lst@firstline}}%
670 \@ifundefined{lst@lastline}{}%
671 {\typeout{\string\lst@lastline=\lst@lastline}}%
672 \@ifundefined{lst@linerange}{}%
673 {\typeout{\string\lst@linerange=\lst@linerange}}%
674 \typeout{\string\if@SX@wide=\if@SX@wide TRUE\else FALSE\fi}%
675 \typeout{\string\if@SX@rangeaccept=\if@SX@rangeaccept TRUE\else FALSE\fi}%
676 \typeout{\string\if@SX@varwidth=\if@SX@varwidth TRUE\else FALSE\fi}%
677 \typeout{graphicfile=\SX@graphicname, graphicparameter=[\SX@graphicparam]}%
678 \typeout{--------------------------------}%
679 }
680 \providecommand*\MakePercentIgnore{\catcode`\%9\relax}
681 \providecommand*\MakePercentComment{\catcode`\%14\relax}
682 % \end{macrocode}
683 % \begin{macro}{\SX@resultInput}
684 % \changes{v0.3b}{2005/10/15}{Input of result code now inside a group;
685 % \cmd{\makeatother} added (RN).}
686 % \changes{v0.3c}{2005/10/15}{Wrong catcode for newline char corrected (RN).}
687 % \changes{v0.3d}{2005/12/06}{Missing \cs{par} added (RN).}
688 % \changes{v0.3l}{2014/01/19}{Code for ``scaled'' option (RN).}
689 % \begin{macrocode}
690 \newcommand*\SX@resultInput{%
691 \ifx\SX@graphicname\@empty
692 \begingroup
693 \MakePercentComment\makeatother\catcode`\^^M=5\relax
694 \SX@@preset\SX@preset
695 \if@SX@rangeaccept
696 \let\SX@tempa=\SX@input
697 \else
698 \let\SX@tempa=\input
699 \fi
700 \if\SX@scaled ?%
701 \let\SX@tempb=\@firstofone
702 \else
703 \if\SX@scaled !%
704 \def\SX@tempb##1{\resizebox{\SX@width}{!}{##1}}%
705 \else
706 \def\SX@tempb##1{\scalebox{\SX@scaled}{##1}}%
707 \fi
708 \fi
709 \let\SX@lst@Init=\lst@Init
710 % \end{macrocode}
711 % \changes{v0.3p}{2020/05/04}{Better handling of floats (RN).}
712 % \changes{v0.3q}{2020/05/06}{Floats should always be numbered 0 (RN).}
713 % Prevents float environments from floating. This is not enough for floating
714 % listing environments! Why?
715 % \begin{macrocode}
716 \def\@xfloat##1[##2]{%
717 \def\@captype{##1}%
718 \@namedef{the\@captype}{0}%
719 \@float@HH{##1}[H]}%
720 % \end{macrocode}
721 % Special handling of floating listing environments.
722 % \begin{macrocode}
723 \def\lst@Init{%
724 \let\lst@float=\relax
725 \setcounter\@captype{-1}%
726 \SX@lst@Init
727 }
728 % \end{macrocode}
729 % Typeset the Code.
730 % \begin{macrocode}
731 \SX@tempb{\SX@tempa{\SX@codefile}}\par
732 \endgroup
733 \else
734 \expandafter\includegraphics\expandafter[\SX@graphicparam]%
735 {\SX@graphicname}%
736 \fi
737 }
738 % \end{macrocode}
739 % \end{macro}
740 % \begin{macro}{\SX@codeInput}
741 % \begin{macrocode}
742 \newcommand*\SX@codeInput{%
743 % \end{macrocode}
744 % Without a caption entry the command \cmd{\lstinputlisting} adds the
745 % filename to the ``list of listings'' (lol). This should be avoided.
746 % \changes{v0.1d}{2004/11/20}{\cmd{\lstset}|{nolol}| and
747 % \cmd{\lstset}|{float=false}| added (RN).}
748 % \changes{v0.1h}{2005/06/03}{\cmd{\lstset}|{float=false}| removed (RN).}
749 % \begin{macrocode}
750 \begingroup
751 % \end{macrocode}
752 % The default parameters for all examples.
753 % \begin{macrocode}
754 \expandafter\lstset\expandafter{\SX@explpreset}%
755 % \end{macrocode}
756 % If "numbers=none" then margin dimensions should be zero.
757 % \begin{macrocode}
758 \expandafter\lstset\expandafter{\SX@@explpreset}%
759 \ifx\lst@PlaceNumber\@empty
760 \g@addto@macro\SX@@explpreset{,xleftmargin=0pt,xrightmargin=0pt}%
761 \fi
762 \SX@Info
763 \expandafter\lstinputlisting\expandafter[\SX@@explpreset,nolol=true,%
764 caption={},belowskip=\z@,aboveskip=\z@,float=false]{\SX@codefile}%
765 \endgroup
766 }%
767 % \end{macrocode}
768 % \end{macro}
769 % \changes{v0.1h}{2005/06/03}{New macro \cmd{\LTXinputExample} (RN).}
770 % \begin{macrocode}
771 \newcommand*\LTXinputExample[2][]{%
772 \g@addto@macro\SX@@explpreset{float=false,#1,codefile=#2}%
773 \SX@put@code@result}%
774 % \end{macrocode}
775 % All the default values.
776 % \begin{macrocode}
777 \lstset{explpreset={numbers=left,numberstyle=\tiny,numbersep=.3em,
778 % \end{macrocode}
779 % Negative width means defaults.
780 % \begin{macrocode}
781 xleftmargin=1em,columns=flexible,language=[LaTeX]TEX},pos=l,width=-99pt,
782 overhang=0pt,hsep=\columnsep,vsep=\bigskipamount,rframe=single}
783 % \end{macrocode}
784 % \changes{v0.3k}{2013/03/21}{Definition for ``hyperref''
785 % (suggested by Heiko Oberdiek)}.
786 % \begin{macrocode}
787 \AtBeginDocument{%
788 \def\theHlstnumber{\thelstlisting.\arabic{lstnumber}.\lst@neglisting}%
789 }
790 % \end{macrocode}
791 % \changes{v0.1g}{2004/12/10}{Loading of |showexpl.cfg| now at end of
792 % package (RN).}
793 % Changing the defaults possible in |showexpl.cfg|.
794 % \begin{macrocode}
795 \InputIfFileExists{showexpl.cfg}{}{}
796 % \end{macrocode}
797 %
798 % \Finale
799 \endinput