6

I am working on the sheets of a course on LaTeX and would like to have several sheets in which you see on the left an example of a source document and on the right its output. I have this working now with listings and minipage, but I also want a frame around the page of a fixed width and height.

what I have now is:

\documentclass{beamer}
\usepackage{listings}

\begin{document}

\begin{frame}[fragile]{The \LaTeX\ language}

% Source code:
\begin{columns}[T]
\column{0.45\textwidth}
\begin{center}
\LaTeX\ code:
%\begin{lrbox}\mylistingbox
\begin{minipage}[t][0.7\textheight]{\textwidth}
\begin{lstlisting}
An equation:
\[
  1 + 1 = 2
\]
\end{lstlisting}
\end{minipage}
%\end{lrbox}
%\fbox{\usebox\mylistingbox}
\end{center}

% Output
\column{0.45\textwidth}
\begin{center}
Output:
\fbox{
\begin{minipage}[t][0.7\textheight]{1\textwidth}
An equation:
\[
  1 + 1 = 2
\]
\end{minipage}}
\end{center}
\end{columns}
\end{frame}
\end{document}

This works, but only puts a frame around the right side (the output). If I try the same with \fbox{} on the left side it does not work.

How can I put a frame around the minipage with the listiting?

1

2 Answers 2

5

While this does not address you specific question, I would highly recommend that you consider using the showexpl pacakge which makes use of the listings package. This eliminates the duplication of the LaTeX code, so it is not as error prone.

To control the font use basicstyle=... to set it for the listings. The output font will be as per the rest of the document. If you want to adjust it just for the output of LTXexample you can use preset=... to specify commands to be executed before the sample code is typeset.

enter image description here

\documentclass{beamer}
\usepackage{xcolor}
\usepackage{showexpl}% already includes listings package

\usepackage{adjustbox}

\lstdefinestyle{demoLatexStyle}{
    basicstyle=\small\ttfamily,% control font of code
    preset=\small,% adjust font size of output
    numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
    frame=tlbr,
    pos=r,% want output on right
    backgroundcolor=\color{yellow!30},
    basicstyle=\ttfamily,
    width=0.50\linewidth,
}
\lstloadlanguages{[LaTeX]TeX}

\begin{document}

\begin{frame}[fragile]{The \LaTeX\ language}
\makebox[0.50\linewidth][c]{\LaTeX\ code:}%  Center titles over
\makebox[0.50\linewidth][c]{Output:}%        half the \linewidth

\begin{LTXexample}[style=demoLatexStyle]
An equation:
\[
  1 + 1 = 2
\]
\end{LTXexample}
\end{frame}
\end{document}
2
  • Cool, thanks. Is there a way to make the font in the output abit smaller? Nov 6, 2011 at 4:17
  • Use \preset to specify commands to be executed prior to typesetting the code. See updated example. Nov 6, 2011 at 15:39
7

You can't have verbatim material inside a macro argument. You need to box the content first as you already did in your commented-out code. I wrote the realboxes package to provide variant of most \XXXbox macros which box their "argument". In your case simply load realboxes and use \Fbox instead of \fbox. There should be also a % after the { to avoid getting a space there from the following newline.

\usepackage{realboxes}
%...

\Fbox{%
\begin{minipage}[t][0.7\textheight]{1\textwidth}
An equation:
\[
  1 + 1 = 2
\]
\end{minipage}}

There is also a Fbox environment.

You could also use my adjustbox package:

\usepackage{adjustbox}
% ...

\begin{adjustbox}{minipage=[t][0.7\textheight]{1\textwidth},fbox}
An equation:
\[
  1 + 1 = 2
\]
\end{adjustbox}

There are many other keys (options) you can add, e.g. center to center the content etc.

Finally, there are also the framed and (better) mdframed packages which provide an environment for frames.

You must log in to answer this question.

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