% \iffalse meta-comment
%
% Copyright (C) 2004 -- 2006 by Rolf Niepraschk <Rolf.Niepraschk@ptb.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>
    [2007/02/03 v0.3h 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{836}
%
% \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@ptb.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 |a|, |b|, |l|, |r|,
%   |o|, and |i| for above, below, 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[hsep] 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.
% \end{description}
%
% \StopEventually{}
%
% \section{Implementation}
% \changes{v0.1e}{2004/11/24}{Loading of |showexpl.cfg| added (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{listings,calc,ifthen,graphicx,varwidth}
%    \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}}
%    \end{macrocode}
% \changes{v0.1h}{2005/06/03}{``lstpreset'' renamed to ``explpreset'' (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).}
%    \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*\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).}
%    \begin{macrocode}  
\newcommand*\SX@@preset{%
  \renewcommand\documentclass[2][]{\SX@eat@version}%
  \renewcommand\usepackage[2][]{\SX@eat@version}%
  \renewenvironment{document}{}{}%
  \renewenvironment{figure}[1][]{\def\@captype{figure}}{}%
  \renewenvironment{table}[1][]{\def\@captype{table}}{}%
  \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\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. 
%    \begin{macrocode}
\newif\ifSX@wasodd
\if@twoside
  \newcommand*{\isSX@odd}[2]{%
    \ifthenelse{\isodd{\pageref{\SX@IDENT}}}%
      {\SX@wasoddtrue #1}{\SX@wasoddfalse #2}}
\else
  \newcommand*{\isSX@odd}[2]{#1}\SX@wasoddtrue
\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]%
  \setlength\@tempdima{\SX@vsep}\vskip\@tempdima
  \SX@CodeArea{\linewidth}{#3}%
}
\newcommand*\SX@put@b[3]{%
  \SX@CodeArea{\linewidth}{#3}\endgraf\pagebreak[2]%
  \setlength\@tempdima{\SX@vsep}\vskip\@tempdima
  \SX@ResultArea{\linewidth}{#2}%
}
\newcommand*\SX@put@l[3]{%
  \setlength\@tempdimc{\linewidth-#1-\SX@hsep}%
  \SX@ResultArea{#1}{#2}\hfill\SX@CodeArea{\@tempdimc}{#3}%
}
\newcommand*\SX@put@r[3]{%
  \setlength\@tempdimc{\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\setlength\@tempdima{#1}%
  %\minipage\@tempdima#2\endminipage
  \parbox\@tempdima{#2}%
}
\newcommand\SX@CodeArea[2]{%
  \setlength\@tempdima{#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}
% 
% \begin{environment}{LTXexample}
% \changes{v0.1h}{2005/06/03}{Renamed from ``example'' to ``LTXexample' (RN).}
%    \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% Warum noetig?
  \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{macrocode}
\newcommand*\SX@put@code@result{%
  \begingroup
    \expandafter\lstset\expandafter{\SX@explpreset}%
    \let\lst@float=\relax\let\SX@float=\relax
%    \end{macrocode}
% Without the following call \cmd{\lst@beginfloat} is undefined.
%    \begin{macrocode}
    \expandafter\lstset\expandafter{\SX@@explpreset}%
    \ifx\lst@float\relax\else
%    \end{macrocode}
% \cmd{\lst@float} must be \cmd{\relax} because the whole ``example'' 
% should float but not the listings part in addition.
%    \begin{macrocode}
      \let\SX@float=\lst@float\let\lst@float=\relax
      \g@addto@macro\SX@@explpreset{,float=false}%
      \edef\@tempa{\noexpand\lst@beginfloat{lstlisting}[\SX@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
        \setlength\@tempdima{\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. 
%    \begin{macrocode}
      \ifx\SX@rframe\@empty
        \long\def\SX@frame##1{##1}%
      \else
        \let\SX@frame\fbox
        \setlength\@tempdima{\SX@width-2\fboxsep-2\fboxrule}%
        \edef\SX@width{\the\@tempdima}%
      \fi
      \isSX@odd{\def\@tempa{l}}{\def\@tempa{r}}%
      \makebox[\linewidth][\@tempa]{%
        \parbox{\linewidth+\SX@overhang}{%
%    \end{macrocode}
% \cmd{\SX@codefile} (|\jobname.tmp|) is not nessesary for the filelist.
%    \begin{macrocode}        
          \let\@addtofilelist\@gobble
          \let\lst@ifdisplaystyle=\iftrue
          \SX@KillAboveCaptionskip\lst@MakeCaption{t}%
          \lst@belowskip=\z@
          \let\SX@MakeCaption\lst@MakeCaption
          \let\lst@MakeCaption\@gobble
%    \end{macrocode}
% Use the ``natural'' width of the result code if ``varwidth'' is 
% true. 
%    \begin{macrocode}
          \setbox\SX@ResBox\hbox{%
            \SX@frame{%
              \@nameuse{\if@SX@varwidth varwidth\else minipage\fi}%
                \SX@width\relax\SX@resultInput%
              \@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}}%
          \let\lst@MakeCaption\SX@MakeCaption
          \lst@MakeCaption{b}\SX@KillBelowCaptionskip
        }%
      }%
    \endtrivlist
    \ifx\SX@float\relax\else\expandafter\lst@endfloat\fi
    \gdef\SX@@explpreset{}%
  \endgroup
}
%    \end{macrocode}
% \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).}
%    \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
      \SX@tempa{\SX@codefile}\par%
    \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={}]{\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{#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.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
