# How to center a lstlisting

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:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\begin{document}

\begin{figure}[thp]
\begin{tabular}{c}
\begin{lstlisting}[numbers=left] % Here lies the problem without the numbers=left it works
My Code
\end{lstlisting}
\end{tabular}
\centering
\caption{Bla}
\end{figure}

\end{document}


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

-
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

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:

\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{fancybox}

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

\begin{document}

\begin{figure}[thp]
\begin{CenteredBox}
\begin{lstlisting}[numbers=left]
My Code
Another line
\end{lstlisting}
\end{CenteredBox}
\caption{Bla}
\end{figure}

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

\end{document}


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.

-
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