Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to get some console output in my homework. I put it in a listings environment but I want it to be centered horizontally on the page. The only thing I get centered is the caption ;-/

\begin{minipage}{1\textwidth}
\begin{center}
\begin{lstlisting}[caption={Ausgabe des C-Programms},label={ggt_c_ausgabe}]
a  |  b  |  q  |  u  |  s  |  v  |  t  
----------------------------------------
78 |  21 |   1 |     |   1 |   1 |  -1 
21 |  15 |   3 |   1 |  -3 |  -1 |   4 
15 |   6 |   1 |  -3 |   4 |   4 |  -5 
 6 |   3 |   2 |   4 | -11 |  -5 |  14 
 3 |     |   2 | -11 |  26 |  14 | -33 
----------------------------------------
\end{lstlisting}
\end{center}
\end{minipage}
share|improve this question
1  
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 at 18:14

1 Answer 1

up vote 19 down vote accepted

I know some strange solutions involving saving the content temporary to a box and then determing the text width.

The simplest solution is to use a tabular environment:

\begin{center}
\begin{tabular}{c}
\begin{lstlisting}[...]
...
\end{lstlisting}
\end{tabular}
\end{center}

However, this doesn't center the caption. Making the caption centering correctly above the listing requires another solution. We have to take the caption out of the listings environment; this is done by a figure (or another floating environment).

Here's a way to center both your listing and the caption:

\documentclass{article}
\usepackage{listings}
\renewcommand{\figurename}{Listing}
                    % replace figurename with the text that should preceed the caption
\begin{document}

\begin{figure}[thp] % the figure provides the caption
\centering          % which should be centered
\caption{Ausgabe des C-Programms}
\begin{tabular}{c}  % the tabular makes the listing as small as possible and centers it
\begin{lstlisting}[label={gtt_c_ausgabe}]
a  |  b  |  q  |  u  |  s  |  v  |  t
----------------------------------------
78 |  21 |   1 |     |   1 |   1 |  -1
21 |  15 |   3 |   1 |  -3 |  -1 |   4
15 |   6 |   1 |  -3 |   4 |   4 |  -5
 6 |   3 |   2 |   4 | -11 |  -5 |  14
 3 |     |   2 | -11 |  26 |  14 | -33
----------------------------------------
\end{lstlisting}
\end{tabular}
\end{figure}

\end{document}
share|improve this answer
2  
Good answer. You'd think if tabular can centre it then the center environment should be able to as well, wouldn't you. (N.B. that fancyvrb users can use center around a BVerbatim environment without the tabular.) –  Will Robertson Nov 23 '10 at 9:26
    
@Will, @Matten: I can't get this solution to work properly. The listing is centered, yes, but the caption then is completely off-center. –  Hendrik Vogt Nov 23 '10 at 13:34
    
Agree, listing is fine, but caption goes crazy. –  paraa Nov 23 '10 at 16:58
1  
As this answer has already 10 upvotes (although it didn't really answer the question so far), I took the freedom to add your nice solution from your other answer, inserted it here and made it closer to the code of the OP. Hope you don't mind. I'd suggest you remove that other answer. –  Hendrik Vogt Nov 27 '10 at 17:06
    
@Hendrik -- thank you, I deleted the other answer. –  Matten Nov 27 '10 at 21:13

Your Answer

 
discard

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.