Center Latex lstlisting
Asked Answered
E

3

34

This is driving me crazy.

I want to center a lstlisting in LaTeX.

After 3 hours attempting here's some code:

\lstset{ %
    caption=Descriptive Caption Text,
    label=lst:descr_capti_text,
    basicstyle=\ttfamily\footnotesize\bfseries,
    frame=tb,
    linewidth=0.6\textwidth
 }
\centering\begin{tabular}{c}
\begin{lstlisting}
printf("this should be centered!");
\end{lstlisting}
\end{tabular}

This is putting the lstlisting on the center but not its caption, that goes to the right. If I take out the tabular, then caption gets centered but the code goes to the left! :(

Thank you.

Ecstatic answered 24/6, 2010 at 0:5 Comment(1)
Just thought I'd mention the LaTeX StackExchange proposal since once that takes off, it'll be the best place for questions like this.Carlina
D
39

Instead of using linewidth you should consider to use xleftmargin and xrightmargin (cf. texdoc listings, Chapter 4.10). The following code works without any center or minipage environment:

\lstset{
  caption=Descriptive Caption Text, 
  basicstyle=\footnotesize, frame=tb,
  xleftmargin=.2\textwidth, xrightmargin=.2\textwidth
}
\begin{lstlisting}
    printf("this should be centered!");
\end{lstlisting}
Dambrosio answered 11/10, 2013 at 17:29 Comment(1)
Amazing, this is exactly what I was looking for. In my case, I used a negative value on xrightmarginso I could move the lslisting frame around... Thanks for the hint!Volatile
B
8

Your caption is actually centered over the listing. You are just making the lines that run along the top and bottom of your listing only 0.6\textwidth long. This makes it appear as if the caption was off-center. Also, your \centering doesn't center the listing (visible if you don't shorten the lines below and above).

This should work:

\begin{center}
  \lstset{%
    caption=Descriptive Caption Text,
    basicstyle=\ttfamily\footnotesize\bfseries,
    frame=tb
  }
  \begin{lstlisting}
    printf("this should be centered!");
  \end{lstlisting}
\end{center}

You don't explain why you want the delimiting lines to be 0.6\textwidth long. If you actually wanted to set the width of your listing to be that value, your approach doesn't do what you want. Use something like a minipage to set a width for the whole listing.

\begin{minipage}{0.6\textwidth}
  \begin{center}
    \lstset{%
      caption=Descriptive Caption Text,
      basicstyle=\ttfamily\footnotesize\bfseries,
      frame=tb,
    }
    \begin{lstlisting}
      printf("this should be centered!");
    \end{lstlisting}
  \end{center}
\end{minipage}
Bumptious answered 24/6, 2010 at 0:51 Comment(3)
Although like Akim, I found it worked with the minipage within the center environment.Wiggler
Unfortunately this isn't working for me. The listing won't move.Opalina
Using \begin{center} doesn't have any effect for me centering the listing.Gina
E
6

Actually, what did work for me is the converse: putting the minipage inside the center environment.

Enteron answered 22/1, 2011 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.