13

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} 
\lstloadlanguages{[LaTeX]Tex} 
\lstset{% 
    basicstyle=\ttfamily\small, 
    commentstyle=\itshape\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} 
    \begin{equation} 
       a^2+b^2=\sqrt{\frac{c^2}{1}} 
    \end{equation} 
  \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?

Addendum: This problem has been solved with tcolorbox instead of sowexpl by Thomas F. Sturm in Article example inside beamer frame

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

2 Answers 2

11

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.

3
  • 2
    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
    Commented Nov 26, 2010 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...
    – jub0bs
    Commented May 29, 2013 at 13:28
  • 2
    @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
    Commented May 29, 2013 at 17:08
9

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    =
      {
        % Add list of additions here, e.g.
        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).

3
  • 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
    Commented Aug 17, 2013 at 14:11
  • Nice! Why doesn't it pick up \usepackage[utf8]{inputenc}? I'm trying to write some Norwegian æøå letters...
    – Holene
    Commented Aug 27, 2015 at 20:29
  • 1
    @Holene listings isn't 'UTF-8 safe' as with an 8-bit engine characters are not always one byte. There are some questions on the site about this.
    – Joseph Wright
    Commented Aug 27, 2015 at 20:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .