%% A style for typesetting displayed latex code
%% Colours are chosen to match WinShell on the TeX Live! CD
\lstdefinestyle{tex}{%
  language=[AlLaTeX]TeX, %
  basicstyle=\black\small\ttfamily, %
  keywordstyle=\darkblue\rmfamily\textbf, %
  texcsstyle=\blue\textbf, %
  %%   identifierstyle=, %
  commentstyle=\gray\textsl, %
  %%   %%alsoletter=\\, %
}

\ifcolorBG
\newcommand{\ttcolor}{\yellow}
\else
\newcommand{\ttcolor}{}
\fi

%% A style for inline latex keywords
\lstdefinestyle{intex}{%
  language={}, %
  basicstyle=\ttcolor\ttfamily, %
}

%% Use yellow text for typing
\lstdefinestyle{typing}{%
  language={}, %
  basicstyle=\ttcolor\ttfamily, %
}

%% Set the default style to tex
\lstset{style=tex}

\newgray{ExampleFillColour}{0.9}
\newgray{ExampleInsetColour}{0.8}

%% An nevironment to display a block of latex text
%% in a framebox.
\lstnewenvironment{texlisting}[1][]
{\gdef\lst@intname{#1}%
  \lstset{style=tex,gobble=4}%
  \setbox\lst@samplebox=\hbox\bgroup}
{\egroup%
  \psframebox[fillcolor=ExampleFillColour,fillstyle=solid,linestyle=solid,framesep=5pt]{%
    \begin{minipage}{0.98\linewidth}%
      \usebox{\lst@samplebox}%
    \end{minipage}%
  } %
}

%% An enviroment for display latex example code.
%% The formatted output is displayed, and the raw latex
%% command are displayed in an inset box.
%%
%% If the inset box is narrow enough, the output and the latex
%% code are set side-by-side, otherwise set above and below
\lst@RequireAspects{writefile}
\newsavebox{\lst@samplebox}
\lstnewenvironment{Example}[1][]
{\global\let\lst@intname\@empty
  \gdef\lst@sample{}%
  \setbox\lst@samplebox=\hbox\bgroup
  %%\begin{lrbox}{\lst@samplebox}
    \setkeys{lst}{tabsize=4,gobble=4,basewidth=0.51em,%
    language=[AlLaTeX]TeX, %
    basicstyle=\black\ttfamily, %
    keywordstyle=\darkblue\textbf, %
    texcsstyle=\blue\textbf, %
    identifierstyle=, %
    commentstyle=\gray\textsl, %
  }
  \small%
  #1%
  \lst@BeginAlsoWriteFile{\jobname.tmp}}
{\lst@EndWriteFile\egroup
  \ifdim \wd\lst@samplebox>.5\linewidth
  \begin{center}%
    \psframebox[fillcolor=ExampleFillColour,fillstyle=solid,linestyle=solid,framesep=8pt]{%
      \begin{minipage}{0.99\linewidth}%
        \psframebox[fillcolor=ExampleInsetColour,fillstyle=solid,linestyle=solid,framesep=5pt]{%
          \begin{minipage}{0.98\linewidth}%
            \usebox{\lst@samplebox}%
          \end{minipage}%
        } %
        \vspace{1.0ex plus1.5ex minus1.0ex} \\%
        \begin{minipage}{0.98\linewidth}%
          \hspace*{\fill}%
          \begin{minipage}{0.95\linewidth}%
            \black%
            \lst@sampleInput%
          \end{minipage}%
          \hspace*{\fill}%
        \end{minipage}%
      \end{minipage}%
    }%
  \end{center}%
  \else
  \begin{center}%
    \psframebox[fillcolor=ExampleFillColour,fillstyle=solid,linestyle=solid]{%
      \black
      \begin{minipage}{0.48\linewidth}%
        \lst@sampleInput%
      \end{minipage}%
      \hfill%
      \psframebox[fillcolor=ExampleInsetColour,fillstyle=solid,linestyle=solid]{%
        \begin{minipage}{0.48\linewidth}%
          \usebox{\lst@samplebox}%
        \end{minipage}%
      }%
    }%
  \end{center}%
  \fi}

\def\MakePercentIgnore{\catcode`\%9\relax}
\def\MakePercentComment{\catcode`\%14\relax}

\def\lst@sampleInput{%
  \MakePercentComment\catcode`\^^M=10\relax
  \small\lst@sample
  {\setkeys{lst}{SelectCharTable=\lst@ReplaceInput{\^\^I}%
      {\lst@ProcessTabulator}}%
    \leavevmode \input{\jobname.tmp}}\MakePercentIgnore}
