
**exppreset**: A \(\langle\)_key val list_\(\rangle\) which serves for presetting the properties of the formatting of the source code, for values see the documentation of the listings package. The default value is
**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.
**hsep**: Defines the horizontal distance between the source code and the formatted text.
**justification**: Defines the justification of the formatted text: reasonable values are \raggedleft, vraggedright, \centering. The default value is \raggedright.
**overhang**: A _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\,\mathrm{pt}\).
**pos:**: Defines the relative position of the formatted text relating to the source code. Allowed values are \(\mathrm{t}\), \(\mathrm{b}\), \(\mathrm{l}\), \(\mathrm{r}\), \(\mathrm{o}\), and \(\mathrm{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 \(1\).
**preset**: Any TeX code executed before the sample code but not visible in the listings area.
**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.
**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).
**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.
**vsep**: Defines the vertical distance between the source code and the formatted text.
**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.
**width**: A \(\langle\)_dimen_\(\rangle\) 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.
**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.

In addition to these options the kind of the result box (default: \fbox) can be changed. For example:

\renewcommand\ResultBox\{fcolorbox{green}\{lightgray}\}

\setlength\ResultBoxSep{5mm}% default: \fboxsep

\setlength\ResultBoxRule{2mm}% default: \fboxrule

## 3 Implementation

```
1\DeclareOption{final}{%
2\PassOptionsToPackage{\CurrentOption}{graphicx}%
3\PassOptionsToPackage{\CurrentOption}{listings}%
4\%
5\DeclareOption{draft}{%
6\PassOptionsToPackage{\CurrentOption}{graphicx}%
7\PassOptionsToPackage{\CurrentOption}{listings}%
8\%
9\DeclareOption{attachfiles}{%
10\AtBeginDocument{\IfFileExists{attachfile.sty}%
11\RequirePackage{attachfile}}{\def\SX@attachfile{}}}
12\%
13\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{listings}}
14\ProcessOptions\relax
15\RequirePackage{refcount,listings,graphicx,varwidth,float}
```

We must aktivate code from package listings for writing files.

```
1\SX@defaultWDparameter#2isap lengthor anumber. Parameter#1isap macro. After a call of \SX@defaultWD this macro contains the value of the lengthor the value of the number multiplied by \linewidth.
17\newcommand*\SX@defaultWD[2]{%
18\afterassignment{SX@def%WD\dimen@#2\linewidth\relax{#1}}
19\newcommand*\SX@def%WD{
20\def\SX@def%WD#1\relax#2{\ndef\{the\dimen}}}
```

Additional keys.

```
2\lst&Key{pos}\relax{def\SX@pos{#1}}
22\lst&Key{width}\relax{\def\SX@width{#1}}
23\lst&Key{shep}\relax{\@templa=#1|relax\odef\SX@shep{\the\@templa}}
24\lst&Key{vsep}\relax{\@templa=#1|relax\odef\SX@vsep{\the\@templa}}
25\lst&Key{overhang}\relax{\def\SX@overhang{#1}}
26\lst&Key{wide}f[t]{\lst&UKV@SetIf{#1}\if@SX@wide}
27\lst&Key{rframe}\relax{\def\SX@rframe{#1}}
28\lst&Key{present}\relax{\def\SX@present{#1}}
29\newcommand*\SX@scaled{}
30\lst&Key{scaled}{?}[!]{\def\SX@scaled{#1}}
31\lst&Key{explpreset}\relax{\def\SX@explpreset{#1}}
32\lst&Key{codefile}\relax{\def\SX@codefile{#1}}
33\newififESX@rangeaccept \@SX@rangeacceptfalse
34\newififESX@varwidth \@SX@varwidthfalse
35\newifif@SX@wide \@SX@widefelse
36\newifififESX@attachfile \@SX@attachfilefalse
37\lst&Key{rangeaccept}f[t]{\lst&UKV@SetIf{#1}\if@SX@rangeaccept}
38\lst&Key{varwidth}f[t]{\lst&UKV@SetIf{#1}\if@SX@varwidth}
39\lst&Key{justification}\relax{\def\SX@justification{#1}}
40\lst&Key{attachfile}f[t]{\lst&UKV@SetIf{#1}\if@SX@attachfile}
41\newcommand*\SX@graphicname{}%
42\newcommand*\SX@graphicparam{}%


90\newf{ifS%@wasodd
91\if@twoside
92\newcommand*\isSX@odd%
93\begingroup
94\ifodd\getpagerefnumber{\SX@IDENT}%
95\aftergroup\SX@wasoddtrue
96\else
97\aftergroup\SX@wasoddfalse
98\fi
99\endgroup
100\ifSX@wasodd
101\expandafter\@firstoftwo
102\else
103\expandafter\@secondoftwo
104\fi
105\
106\else
107\SX@wasoddtrue
108\newcommand*\isSX@odd[2]{#1}
109\fi The call of \isSX@odd sets also \ifSX@wasodd to true or false. If it's clear that no page break occurs, \ifSX@wasodd can be used.
110\newcounter{ltxexample}
111\newcommand*{\SX@IDENT}{SX@\number\value{ltxexample}}
112\newcommand*\SX@attachfile{%
113\if@SX@attachfile
114\attachfile[minetype=text/plain,subject={example \theltexample}]%
115\SX@codefile}%
116\fi
117}

\SX@put@t/b/l/r/o/i Six macros for positioning #2 (result) and #3 (code). The result can be above, below, left or right of the code area or on the outer or inner side. Parameter #1 is the width of the result.

118\newcommand*\SX@put@t[3]{%
119\SX@ResultArea{\linewidth}{#2}\endgraf\pagebreak[2]%
120\@tempdima=\dimexpr\SX@vsep\@tempdima
121\SX@CodeArea{\linewidth}{#3}%
122}
123\newcommand*\SX@put@b[3]{%
124\SX@CodeArea{\linewidth}{#3}\endgraf\pagebreak[2]%
125\@tempdima=\dimexpr\SX@vsep\@texple\@tempdima
126\SX@ResultArea{\linewidth}{#2}%
127}
128\newcommand*\SX@put@l[3]{%
129\@tempdimc=\dimexpr\linewidth-#1-\SX@hsep %
130\SX@ResultArea{#1}{#2}\hfill\SX@CodeArea{\#tempdimc}{#3}%
131}
132\newcommand*\SX@put@r[3]{%
133\@tempdimc=\dimexpr\linewidth-#1-\SX@hsep %
134\SX@CodeArea{\@tempdimc}{#3}\hfill\SX@ResultArea{#1}{#2}%



\SX@width\relax
227 \begingroup
228 \SX@resultInput
229 \endgroup
230 \@nameuse{end\ifGX@varwidth\expsinipage\fij}}%
231 \edef\SX@width{\the\vd\SX@ResBox}%
232 \@ifundefined{SX@put@SX@pos}%
233 {\@latex@error{Parameter '\SX@pos'undefined}\@ehd}%
234 {\@nameuse{SX@put@\SX@pos}%
235 {\SX@width\{box\SX@ResBox}{\SX@codeInput}}%
236 \lst@MakeCaption{b}\SX@KillBelowCaptionskip
237 \%
238 \%
239 \endtrivlist
240 \ifx\lst@float\relax\else\expandafter\lst@endfloat\fi
241 \gdef\SX@explpreset}%
242 \endgroup
243 \
244 \newcommand\SX@SkipToFirst{%
245 \ifeof\@inputcheck\else
246 \ifnum\lst@lineno\lst@firstline\else
247 \readline\@inputcheckto\SX@tempa
248 \typeout{IGNORE (\the\lst@lineno)}%
249 \global\advance\lst@lineno\@ne
250 \SX@SkipToFirst
251 \fi
252 \fi
253 \
254 \newcommand\SX@ProcessResult{%
255 \ifeof\@inputcheck
256 \let\SX@tempb\relax
257 \else
258 \let\SX@tempb\SX@ProcessResult
259 \ifnum\lst@lineno\lst@lastline\relax
260 \ifx\lst@linerange\@empty
261 \let\SX@tempb\relax
262 \else
263 \lst@GetLineInterval
264 \SX@SkipToFirst
265 \fi
266 \else
267 \readline\@inputcheckto\SX@tempa
268 \typeout{READ (\the\lst@lineno)}%
269 \expandafter\g@addto@macro
270 \expandafter\SX@linest\expandafter\SX@tempa~~~JJ%
271 \global\advance\lst@lineno\@ne
272 \fi
273 \fi
274 \SX@tempb
275 \
276 \newcommand\SX@input[1]{%\ggingroup
278 {IfFileExists{#1}{}%
279 {%
280 {filename@parse{#1}%
281 {ifx\filename@ext}relax \def\filename@ext{tex}{if
282 \@latexerr{File
283 '\filename@area\filename@base.\filename@ext'notfound.~~J~~JJ}@ehd%
284 {}%
285 \openin\@inputcheck#1
286 \lsthkePreSet\let\lst@linerange\@empty\global\lst@lineno\@ne
287 \expandafter\lstset\expandafter\(\SX@explpreset}%
288 \ifx\lst@linerange\@empty
289 \edef\lst@linerange{{\lst@firstline}-{\lst@lastline},\%
290 \fi
291 \lst@GetLineInterval
292 \SX@Info
293 \newlinechar='\~~J\relax
294 \SX@SkipToFirst\lst\SX@linest\empty
305 \SX@ProcessResult
306 \closein\@inputcheck
307 \scantokens\expandafter{\SX@lines}%
308 \endgroup
309 \newcommand*\SX@Info{%
301 \typeout{-------------------------------}%
302 \typeout{pos=\SX@pos}%
303 \typeout{width=\SX@width}%
304 \typeout{hsep=\SX@hsep}%
305 \typeout{verb=\SX@verb)%
306 \typeout{verb=\SX@overhang}%
307 \typeout{frframe=\SX@frame}%
308 \typeout{codefile=\SX@codefile}%
309 \@ifundefined{lst@firstline}{%
310 \{typeout{\string}lst@firstline=\lst@firstline}}%
311 \@ifundefined{lst@lastline}{}%
312 \{typeout{\string}lst@lastline=\lst@lastline}}%
313 \@ifundefined{lst@linerange}{}%
314 \typeout{\string}lst@linerange=\lst@linerange}}%
315 \typeout{string}ifGX@wide\ifGX@wide\else FALSE\j\%
316 \typeout{\string}ifGXS@rangeaccept=\ifGXS@rangeaccept TRUE\else FALSE\j\%
317 \typeout{string}ifGXS@varwidth=\ifGXS@varwidth TRUE\else FALSE\j\%
318 \typeout{graphicfile=\SX@graphicname, graphicparameter=[\SX@graphicparam]}%
319 \typeout{-------------------------------}%
320 \providecommand*\MakePercentIgnore{\catcode'\%9\relax}
322 \providecommand*\MakePercentComment{\catcode'\%14\relax}
323 \newcommand*\SX@resultInput{%
324 \ifx\SX@graphicname}@empty
325 \begingroup
326 \MakePercentComment\makeatother\catcode'\~~M=5\relax
327 \SX@preset\SX@preset\if@SX@rangeaccept
329 \let\SX@tempa=\SX@input
330 \else
331 \let\SX@tempa=\input
332 \fi
333 \if\SX@scaled?%
334 \let\SX@tempb=\firstofone
335 \else
336 \if\SX@scaled!%
337 \def\SX@tempb##1{resizebox{\SX@width}{{!}{##1}}%
338 \else
339 \def\SX@tempb##1{scalebox{\SX@scaled}{##1}}%
340 \fi
341 \fi
342 \let\SX@lst@Init=\lst@Init

Prevents float environments from floating. This is not enough for floating listing environments! Why?
343 \def\@xfloat#s1[#s2]\%
344 \def\@captype{#s1}%
345 \@namedef{the}@captype}{0}%
346 \@float@HH{##1}[H]}%

Special handling of floating listing environments.
347 \def\lst@Init{%
348 \let\lst@float=\relax
349 \setcounter\@captype{-1}%
350 \SX@lst@Init
351 }

Typeset the Code.
352 \SX@tempb{\SX@tempa{\SX@codefile}}\par
353 \endgroup
354 \else
355 \expandafter\includegraphics\expandafter[\SX@graphicparam]%
356 \SI@graphicname}%
357 \fi
358 }

\SX@codeInput
359 \newcommand*\SX@codeInput%

Without a caption entry the command \lstinputlisting adds the filename to the "list of listings" (lol). This should be avoided.
360 \begingroup

The default parameters for all examples.
361 \expandafter\listset\expandafter{\SX@explpreset}%

If "numbers=none" then margin dimensions should be zero.
362 \expandafter\listset\expandafter{\SX@@explpreset}%
363 \if\lst@PlaceNumber\@empty
364 \g@addto@macro\SX@@explpreset{,xleftmargin=Opt,xrightmargin=Opt}%
365 \fi
366 \SX@Info
367 \expandafter\listinputlisting\expandafter[\SX@explpreset,nolol=true,%


v0.3b \SA%resultInput: Input of result code now inside a group; \makeatother added (RN)... 9v0.3c \SA%resultInput: Wrong catcode for newline char corrected (RN).. 9v0.3d \SA%resultInput: Missing \par added (RN).. 9v0.3e \SA%@preset: More redefinitions added (RN).. 4v0.3g  General: \SX@ProcessResult is now working correctly using \reading and \scantokens. Thanks to Ulrich Diez for help (RN).. 8 Missing \newcommand for \SX@explpreset added (RN). v0.3h  General: New Option 'attachfiles' (RN).. 3v0.3j \SX@put@code@result: Setting \lst@MakeCaption to was a bad idea for hyperlinks. Group added to varwidth environment. (Suggestions by Ulrike Fischer.).. 7v0.3k \SX@put@code@result: Setting \lst@MakeCaption to \gobble again (prevent multiply defined labels; label key).. 7 General: Definition for "hyperref" (suggested by Heiko Oberdiek) 11 v0.3l \SX@resultInput: Code for "scaled" option (RN).. 9 General: Option "scaled" and \SX@scaled added (RN)... 3v0.3m \SX@put@code@result: Wrong assignement for \lst@belowskip (RN).. 7 v0.3n \SX@put@code@result: Use \ResultBox.. 7 General: Define \ResultBox etc.. 9 Prevent utf8 encoding errors. 6 v0.3p \SX@present: Remove extra treatment of 'figure'/'table' (RN).. 4 \SX@put@code@result: Let's leave \lst@MakeCaption untouched (RN).. 7 \SX@resultInput: Better handling of floats (RN).. 10 \isX@odd: Replace \isodd with \ifodd(getpagerefnumber (remove package 'ifthen') (RN).. 4 General: Remove package 'calc' (RN).. 3 v0.3q \SX@resultInput: Floats should always be numbered 0 (RN).. 10 v0.3r \SX@present: letting \fstepcounter be \stepcounter (RN).. 4 v0.3s \SX@present: Special handling of cleveref's trefstepcounter (RN).. 4

## Index

Numbers written in italic refer to the page where the corresponding entry is described; numbers underlined refer to the code line of the definition; numbers in roman refer to the code lines where the entry is used.

**Symbols**\@SX@widefalse. 35 \@footnotetext.. 85 \%. 321, 322 \@addtofilelist. 218 \@gobble. 71, 74, 77, \@input. 82 \@captype. 344, 345, 349 79, 80, 82, 85, 218 \@SX@attachfilefalse. 36 \@ehd. 233, 283 \@gobbletwo.. 81 \@SX@rangeacceptfalse \@firstoftone. 334 \@inputcheck 245, 247, \SX@firstoftwo. 101 255, 267, 285, 296 \@SX@varwidthfalse. 34 \@float@HH. 346 \@latex@error. 233


