Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have frames of the following format (code followed by an itemize):

\begin{frame}[fragile]
    \frametitle{Blah}
    \begin{lstlisting}
    Five to seven lines of code here...
    \end{lstlisting}
    \begin{itemize}
        \item explanation
        \item explanation
        \item explanation
    \end{itemize}
\end{frame}

What I want to accomplish is for the lstlisting to automatically resize the font to take up as much space as possible without causing horizontal and vertical overflows.

share|improve this question
    
Please always add full compilable but minimal working example (MWE). Here it would be very good to have real code included, not a line saying "5-7 lines". –  Martin Scharrer May 11 '11 at 11:37

1 Answer 1

up vote 10 down vote accepted

First of all: beamer does not provide any automatic scaling of text or diagrams like MS Power Point, because its author thinks that it leads to bad presentations. If the material doesn't fit on one slide unscaled simply make two or more slides out of it.

Adjusting the font or scaling some listings a little bit might be still OK, but I wouldn't do it automatically. Just because the whole slide is filled out doesn't make it automatically look good.

You should manually adjust the size either by adjusting the font size using basicstyle=<some font size command> (e.g. \small, \scriptsize, ...) in the optional argument of the lstlisting environment or by scaling the whole listing. Some people don't like scaling text freely, but I personally would say it is OK in this case. The normal scaling macro \scalebox{<factor>}{...} from the graphics package doesn't work with verbatim content. You need to either store it in a box register e.g. using the lrbox environment or use the adjustbox package which provides some extended box macros and environment on top of the ones provided by graphicx. The adjustbox environment accepts verbatim content and takes all options of \includegraphics including scale and height,width,keepsaspectratio.

I would adjust the height manually. You could try to adjust the solutions to How to define a figure size so that is consume the rest of a page? so that the amount is calculated automatically. However with an figure or listing in the middle of the text it is much more difficult than with one at the end.

\documentclass{beamer}

\usepackage{listings}
\usepackage{adjustbox}

\begin{document}

\newlength\someheight
\setlength\someheight{3cm}

\begin{frame}[fragile]
    \frametitle{Blah}
    \begin{adjustbox}{width=\textwidth,height=\someheight,keepaspectratio}
    \begin{lstlisting}
        function example () {
            if (example) {
                print "Example";
            }
            else {
                print "Automatic Scaling in Presentations is evil anyway";
            }
        }
    \end{lstlisting}
    \end{adjustbox}
    \begin{itemize}
        \item explanation
        \item explanation
        \item explanation
    \end{itemize}
\end{frame}

\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.