TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Thanks a lot for your solutions ! It really helps ! – user7137 Sep 21 '11 at 14:29
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – Jubobs Mar 1 '14 at 22:29

Here is a solution with fancyvrb and output with listings:



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


enter image description here

share|improve this answer
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 – Herbert 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

share|improve this answer

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?

share|improve this answer
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 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.