43

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}
0

2 Answers 2

43

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}
7
  • 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.) Nov 23, 2010 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. Nov 23, 2010 at 13:34
  • 1
    Agree, listing is fine, but caption goes crazy.
    – paraa
    Nov 23, 2010 at 16:58
  • 2
    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. Nov 27, 2010 at 17:06
  • @Hendrik -- thank you, I deleted the other answer.
    – Matten
    Nov 27, 2010 at 21:13
19
+50

The lstlisting package only boxes the contents if it starts in horizontal mode (which is the reason why it boxes the contents in a tabular).

A solution is to coerce horizontal mode, but there's the added problem of setting the caption, which can be done with the help of \captionof, persuading LaTeX into thinking this is a listings and not a figure.

In order to have a key-value interface, I set up a new one that intercepts caption and label, while it passes every unknown option to \lstset. The only change is that a short caption should be specified as a value to shortcaption and not with the

caption=[short caption]{long caption}

format of listings.

\documentclass{article}
\usepackage{showframe} % just for testing
\usepackage{listings,caption,xparse}

\ExplSyntaxOn
\tl_new:N \l_listings_boxed_options_tl
\keys_define:nn { listings/boxed }
 {
  caption .tl_set:N = \l_listings_boxed_caption_tl,
  shortcaption .tl_set:N = \l_listings_boxed_shortcaption_tl,
  label .tl_set:N = \l_listings_boxed_label_tl,
  unknown .code:n =
          \tl_put_right:NV \l_listings_boxed_options_tl \l_keys_key_tl
          \tl_put_right:Nn \l_listings_boxed_options_tl { = #1 , },
 }
\box_new:N \l_listings_boxed_box

\lstnewenvironment{blstlisting}[1][]
 {
  \keys_set:nn { listings/boxed } { #1 }
  \exp_args:NV \lstset \l_listings_boxed_options_tl
  \hbox_set:Nw \l_listings_boxed_box
 }
 {
  \hbox_set_end:
  \cs_set_eq:cc {c@figure} {c@lstlisting}
  \tl_set_eq:NN \figurename \lstlistingname
  \tl_if_empty:NF \l_listings_boxed_caption_tl
   {
    \tl_if_empty:NTF \l_listings_boxed_shortcaption_tl
     {
      \captionof{figure}{\l_listings_boxed_caption_tl}
     }
     {
      \captionof{figure}[\l_listings_boxed_shortcaption_tl]{\l_listings_boxed_caption_tl}
     }
    \tl_if_empty:NF \l_listings_boxed_label_tl { \label{\l_listings_boxed_label_tl} }
   }
  \leavevmode\box_use:N \l_listings_boxed_box
 }
\ExplSyntaxOff

\begin{document}
\begin{center}
\begin{blstlisting}[
  caption=Ausgabe des C-Programms,
  shortcaption=Ausgabe, % just for showing the syntax, it can be omitted
  label=ggt_c_ausgabe,
  basicstyle=\ttfamily, % just for testing the option is correctly passed
]
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{blstlisting}
\end{center}
\end{document}

enter image description here

8
  • 1
    It would seem that nobody but you and me cares about this answer :) May 23, 2015 at 15:49
  • 7
    @SeanAllred You are wrong ;) May 25, 2015 at 9:23
  • What would need to be changed if the caption should be placed below the listing? Jan 10, 2016 at 2:31
  • 1
    @cryingshadow Move the \leavevmode line just after \hbox_set_end:
    – egreg
    Jan 10, 2016 at 9:30
  • 1
    @TorbjørnT. To be honest, I don't remember.
    – egreg
    Jan 4, 2017 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .