9

I am using listings to put java code in my beamer slides.

Sometimes, I want to frame the code but I do not want the frame to occupy the whole width of the page. I want the frame to occupy only the width of my code and no more.

Is there a way to do that?

7

Here is a solution with fancyvrb and output with listings:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{beramono}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{fancyvrb}
\usepackage{listings}
\makeatletter
\newenvironment{SmallListing}[1][]
  {\lstset{#1}\VerbatimEnvironment\begin{VerbatimOut}{VerbEnv.tmp}}
  {\end{VerbatimOut}\settowidth\@tempdima{%
    \lstinputlisting{VerbEnv.tmp}}
  \minipage{\@tempdima}\lstinputlisting{VerbEnv.tmp}\endminipage}    
\makeatother
\begin{document}

\blindtext

\begin{center}
\begin{SmallListing}
  [language=Java,frame=single,columns=fixed,
   basicstyle=\small\ttfamily,
   keywordstyle=\bfseries]
static void main () {
  int foo;
  string baz;
}
\end{SmallListing}
\end{center}

\end{document}

enter image description here

  • Wow, that's awesome! Is it advisable/possible to use \write18 (or something else) to clean up VerbEnv.tmp? – Werner Aug 29 '11 at 17:58
  • @Werner: you are right, that should be \jobname.tmp, then it is easier to delete. It makes no real sense here to delete that file because we need -shell-escape – user2478 Aug 29 '11 at 18:47
  • Nice tidy approach! – Niel de Beaudrap Aug 30 '11 at 0:03
3

The varwidth package provides the varwidth environment which is similar to minipage. However, it fits it's content in a box of "natural" width if the contents is up to a specified maximum:

\usepackage{varwidth}% http://ctan.org/pkg/varwidth
...
\begin{varwidth}{0.5\textwidth}
   ...
\end{varwidth}

The problem with using the listings package is that, as @Neil mentioned, it wants to take up the entire \textwidth for the lstlisting. There are other alternatives, like the algorithmicx package, for example:

\documentclass{article}
\usepackage{varwidth}% http://ctan.org/pkg/varwidth
\usepackage{algorithmicx}% http://ctan.org/pkg/algorithmicx
\begin{document}
\centering
  \fbox{\begin{varwidth}{0.7\textwidth}
    \begin{algorithmic}[1]
      \State Here is some code
      \State Some lines or very long compared to others
      \State Some are short.
    \end{algorithmic}
  \end{varwidth}}
\end{document}

Framed algorithmic environment

0

Consider putting the lstlisting environment in a minipage:

\begin{center}% if you want
  \begin{minipage}{0.5\textwidth}
    \begin{lstlisting}[...]
but don't indent your source code
unless you want extra white-space
on the left side of your listing!
    \end{lstlisting}
  \end{minipage}
\end{center}



Edited to add: using fancyvrb, I have found a fantastically cludgy solution, but one which gives exactly the desired output.

\documentclass{article}
\usepackage{fancyvrb,listings}
\begin{document}

% The following saves the code in a separate file.
\begin{VerbatimOut}{\jobname.listing}
\begin{lstlisting}[frame=single]
but don't indent your source code
unless you want extra white-space
on the left side of your listing!
\end{lstlisting}
\end{VerbatimOut}

% The following somehow manages to find the natural width of the code!
\newlength\codewidth
\settowidth\codewidth{\input{\jobname.listing}}

% Make a minipage with the natural width of the code.
\noindent
\begin{minipage}{\codewidth}%
\input{\jobname.listing}%
\end{minipage}

\end{document}

Can we tidy this up to allow this mechanism to be captured in a wrapper environment for lstlisting?

  • Thanks for your reply. But, if I use minipage, I have to choose the width (for example, 0.5\textwidth). What I would like to get is a frame that occupies just the width of my code without having to specify this width. – user7137 Aug 29 '11 at 15:25
  • @user7137: Indeed. I am interested myself in whether there is a way to do this. However, the lstlisting environment seems to want to occupy as much horizontal space as it thinks is allowed. – Niel de Beaudrap Aug 29 '11 at 15:41
  • @user7137: see my revised answer above for something ugly, but which works automatically. – Niel de Beaudrap Aug 29 '11 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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