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?


Here is a solution with fancyvrb and output with listings:



static void main () {
  int foo;
  string baz;


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

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

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:

\usepackage{varwidth}% http://ctan.org/pkg/varwidth
\usepackage{algorithmicx}% http://ctan.org/pkg/algorithmicx
      \State Here is some code
      \State Some lines or very long compared to others
      \State Some are short.

Framed algorithmic environment


Consider putting the lstlisting environment in a minipage:

\begin{center}% if you want
but don't indent your source code
unless you want extra white-space
on the left side of your listing!

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


% The following saves the code in a separate file.
but don't indent your source code
unless you want extra white-space
on the left side of your listing!

% The following somehow manages to find the natural width of the code!

% Make a minipage with the natural width of the code.


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.