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 have the following problem. I want to center a listing, I tried this approach: How to center a listing?

It works but when I try to set frame or numbers specifically for each listing I get errors. Here´s the M(Not)WE:


  \begin{lstlisting}[numbers=left] % Here lies the problem without the numbers=left it works
   My Code


Is there another way to center the listings and captions or how could I fix this approach?

share|improve this question
The listing environment inside the tabular looks odd to me. I'm not sure if this is the best option, but I would go for a minipage solution, along with the caption package or something like that. – guillem Dec 4 '12 at 15:46
@guillem : It sounded weird to me as well, but that's indeed the way suggested by the linked answer. The minipage being the first attempt of the asker of the linked question. – T. Verron Dec 4 '12 at 15:48
I think that the OP in the linked question did not get the listing centered because the width of the minipage used is equal to textwidth. The problem with minipage, of course, is to automatically determine the horizontal size of the listing. – guillem Dec 4 '12 at 15:53
up vote 10 down vote accepted

The solution involving a table does not work here probably because lstlisting uses itself a table or other kind of tabular to put the numbers on the listing.

So the required solution for this case involves to save the content of the listing into a box, and then use the width of that box to size a \parbox or minipage, and put the original box inside. This new box can be centered by usual methods.

The problem is that lstlisting environment is a "verbatim-like" environment, and putting this kind of environment inside a box causes some problems. Fortunately fancybox package provides the environment Sbox devised for this kind of cases.

So my proposed solution is:


\begin{Sbox}}{% Save the content in a box
\end{Sbox}\centerline{\parbox{\wd\@Sbox}{\TheSbox}}}% And output it centered


  My Code
  Another line

\noindent X\hrulefill X\par % This is to see the page width



Note that numbers are not part of the box (they are typeset with some kind of \llap), so they are not taken into account to center it.

share|improve this answer
Perfect, only drawback is you still have to use figure to keep the caption in line, but I can work with that, hehe, thank you very very much. I also would like to mention tex.stackexchange is really a great place, always excellent an fast answers to any question you have on your mind you guys are great! – chris Dec 4 '12 at 17:03
You are welcome! I dont understand your problem with the caption. Currently it is also centered. Did you wanted it left-aligned with the listing? – JLDiaz Dec 4 '12 at 17:05
What I meant was it would have been optimal if you could use the listings caption like this \begin{lstlisting}[numbers=left,caption=Bla] inside the CenteredBox instead of the figure caption, but no problem it´s just a few lines more this way, as long as the output looks good I´m happy. – chris Dec 4 '12 at 17:21

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.