# How to use showexpl with an external class?

The showexpl package defines LTXexample environment which allows to define a latex code and shows its result side by side. The problem is that it uses the same document class where the LaTeX code is defined and I would like to define which class to use for examples. Do you know if it's possible?

Here you have an examples. It's a beamer presentation with some LTXexamples.

\documentclass{beamer}
\usepackage{showexpl}
\lstset{%
basicstyle=\ttfamily\small,
showspaces=false,
showstringspaces=false,
breaklines=true,
breakautoindent=true,
captionpos=t
}

\begin{document}
\frame[containsverbatim]{
\frametitle{First slide}
\begin{LTXexample}
\begin{itemize}
\item a
\item b
\item c
\end{itemize}
\end{LTXexample}

\begin{LTXexample}
$$a^2+b^2=\sqrt{\frac{c^2}{1}}$$
\end{LTXexample}
}

\frame[containsverbatim]{
\frametitle{Second slide}
\begin{LTXexample}
\documentclass{article}
\begin{document}
\section{Section 1}
Text for section 1
\end{document}
\end{LTXexample}
}
\end{document}


Second slide doesn't show 'section title' because beamer has its own section behaviour, so it seems that \documentclass{article} inside the example doesn't have any effect. The resulting font is sanserif because beamer uses it.

I know I can use several external documents and insert code and results, but with LTXexample or a similar environment would be easier. Do you know a better solution?

-
+1 for introducing me to showexpl, I spent hours this week more or less doing this by hand... Sigh. –  Seamus Nov 26 '10 at 15:35

The code must be compiled externally. Loading the code of article.cls and all sorts of packages in the beamer run would lead to chaos. But theoretically if --shell-escape is active it is possible to trigger this external run on-the-fly during the compilation of the beamer run and then to include the resulting pdf with the graphic-option of showexpl. The main problem I see is that you probably don't want to include a complete page but only a part of it (e.g. to show the section). I think it would be difficult to write a good and flexible interface to choose this part.

-
The LaTeX team had to do something like this for The LaTeX Companion, as a lot of the example packages cannot be mixed. I believe the way it was done was using some special markers in the example .tex files ('start included preamble', 'start included body', etc.), plus a Makefile set up. There is some detail toward the end of the book. –  Joseph Wright Nov 26 '10 at 22:27
@JosephWright I know this question is three years old, but could you elaborate on your comment, perhaps post your own answer, please? I find Ulrike's answer a bit too handwavy and I'm not sure what to do... –  Jubobs May 29 '13 at 13:28
@Jubobs: You can find some information about the process mentioned by Joseph in CTAN. All LC2 examples and classes used to make them are there. –  Ignasi May 29 '13 at 17:08
@Ignasi Thanks. I will look into that. –  Jubobs May 30 '13 at 8:59

This is not a 'fully worked up' solution for all cases, but I hope is enough of an answer to show how things can be done in a reasonably flexible way. The code in showexpl is pretty long, and so breaking into it to add the ability to use an external compilation is a bit tricky. Instead, I'm using some code I've adapted from the listings sources in my own packages, and adding an additional ability to that. As a result, you don't get all of the bells and whistles of showexpl out of the box, although you could add them:

\documentclass{beamer}
\usepackage{ifplatform}
\usepackage{listings}
\makeatletter
\lst@RequireAspects{writefile}
\newbox\LaTeXdemo@box
\newbox\LaTeXdemo@graphics
\lstnewenvironment{LaTeXdemo}[1][code-and-example]{%
\global\let\lst@intname\@empty
\ifcsname LaTeXdemo@#1@end\endcsname
\expandafter\let\expandafter\LaTeXdemo@end
\csname LaTeXdemo@#1@end\endcsname
\csname LaTeXdemo@#1\expandafter\endcsname
\else
\ERROR
\fi
}{%
\LaTeXdemo@end
}
\newcommand*{\LaTeXdemo@common}{%
\setkeys{lst}{%
basewidth    = 0.51 em,
basicstyle   = \small\ttfamily ,
gobble       = 2,
keywordstyle = \color{blue},
language     = [LaTeX]{TeX},
moretexcs    =
{
email
}
}%
}
\newcommand*{\LaTeXdemo@input}{%
\catcode\^^M=10\relax
\small
\begingroup
\leavevmode
\input{\jobname.tmp}%
\endgroup
}

\@namedef{LaTeXdemo@code-and-example}{%
\setbox\LaTeXdemo@box=\hbox\bgroup
\lst@BeginAlsoWriteFile{\jobname.tmp}%
\LaTeXdemo@common
}
\@namedef{LaTeXdemo@code-and-example@end}{%
\lst@EndWriteFile
\egroup
\begin{center}
\ifdim\wd\LaTeXdemo@box>0.48\linewidth\relax
\hbox to\linewidth{\box\LaTeXdemo@box\hss}%
\begin{minipage}{\linewidth}
\LaTeXdemo@input
\end{minipage}
\else
\begin{minipage}{0.48\linewidth}
\LaTeXdemo@input
\end{minipage}
\hfill
\begin{minipage}{0.48\linewidth}
\hbox to\linewidth{\box\LaTeXdemo@box\hss}%
\end{minipage}
\fi
\end{center}
}

\expandafter\let\csname LaTeXdemo@code-and-external\expandafter\endcsname
\csname LaTeXdemo@code-and-example\endcsname
\@namedef{LaTeXdemo@code-and-external@end}{%
\lst@EndWriteFile
\egroup
\immediate\write18{pdflatex -jobname \jobname-demo
"\unexpanded\expandafter{\LaTeXdemo@documentclass@move}
\noexpand\input \jobname.tmp"}%
\begin{center}
\ifdim\wd\LaTeXdemo@box>0.48\linewidth\relax
\hbox to\linewidth{\box\LaTeXdemo@box\hss}%
\begin{minipage}{\linewidth}
\includegraphics{\jobname-demo}%
\end{minipage}
\else
\begin{minipage}{0.48\linewidth}
\includegraphics{\jobname-demo}%
\end{minipage}
\hfill
\begin{minipage}{0.48\linewidth}
\hbox to\linewidth{\box\LaTeXdemo@box\hss}%
\end{minipage}
\fi
\end{center}
\immediate\write18{\ifwindows del\else rm\fi \jobname-demo.pdf}%
}
\begingroup
\catcode\#=12 %
\gdef\LaTeXdemo@documentclass@move{%
\let\LaTeXdemodocumentclass\documentclass
\renewcommand{\documentclass}[2][]{%
\let\documentclass\LaTeXdemodocumentclass
\begingroup
\edef\x{%
\endgroup
\noexpand\documentclass[class=#2,varwidth,#1]{standalone}%
}%
\x
}%
}
\endgroup
\makeatother

\begin{document}
\begin{frame}[fragile]
\frametitle{An example in the beamer class}
\begin{LaTeXdemo}
\section{foobar}
\begin{itemize}
\item foo
\item bar
\end{itemize}
\end{LaTeXdemo}
\end{frame}

\begin{frame}[fragile]
\frametitle{An example in the article class}
\begin{LaTeXdemo}[code-and-external]
\documentclass{article}
\begin{document}
\section{foobar}
\begin{itemize}
\item foo
\item bar
\end{itemize}
\end{document}
\end{LaTeXdemo}
\end{frame}
\end{document}


The idea in the above is to provide different ways of printing examples: I've included only 'code plus demo' and 'code plus externally-compiled demo', but 'code only' and 'code plus floating demo' are easy enough to do: I can add in if there is interest. (I guess also 'code plus floating externally-compiled demo', if really wanted!) As it stands, the code assumes the aim is a 'side by side' presentation but does no scaling if that won't fit, instead placing the output below the code. That can easily be adjusted, but perhaps would be best as a separate (linked) question.

The tricky part is getting a 'tight' page around the output using an external compilation (assuming that is what is required). The method I've used is to really use the standalone class, but to pass the 'required' class as an argument to that using a bit of trickery in the auxiliary compilation.

Note that some of this is a bit low-level (for example the box stuff) and some of that could be moved 'higher up'. Also note that at least restricted \write18 is required, and that I've not added mutli-pass, BibTeX or similar support, but that this could be done with a bit of effort (probably using a keyval approach for the optional argument).

-
Probably the 'correct' solution is of course to ask for a feature to be added to showexpl to do this natively, using one or other of the available approaches. –  Joseph Wright Aug 17 '13 at 14:11