4

I've been looking around and tried various setups but have yet to found the solution.

I'm trying to define a lstlisting environment that is a float, is centered in the page, and cannot be broken across pages, without using captionof. Using the figure, center, and varwidth environments to encapsulate the lstlistingenvironment works. However, it requires to use captionof from the captionpackage outside the lstlisting, which changes the look (size) and behavior (width of the caption box, perhaps due to the varwidth) of the caption itself. I'd like to use the label and caption of the lstlisting environment.

Any idea?

Note: I may post more details sometime later.

EDIT: Well, I just achieved a partial solution:

\begin{figure}[t]
\begin{center}
\begin{minipage}{0.7\linewidth}
\begin{lstlisting}[label={lst:stuff},
caption={Very large Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption.}]
Stuff
\end{lstlisting}
\end{minipage}
\end{center}
\end{figure}

\ref{lst:stuff}

Now the problem is that large captions will begin break lines and not fill the entire \linewidth since only 70% percent (0.7\linewidth) of it is available (this is required in order to make the listing centered by eye).

Any ideas?

EDIT 2: I guess \captionof{lstlisting}{Caption} will have to do the trick.

EDIT 3: Complete solution:

\documentclass{article}
\usepackage{caption}
\usepackage{listings}

\begin{document}
\begin{figure}[t]
\begin{center}
\begin{minipage}{0.7\linewidth}
\begin{lstlisting}
Stuff
\end{lstlisting}
\end{minipage}
\end{center}
\captionof{lstlisting}{Caption.}
\label={lst:stuff}
\end{figure}

\ref{lst:stuff}
\end{document}
  • \begin{lstlisting}[float,caption={Stuff},label=listofstuff] Stuff \end{lstlisting} is a floating listing that cannot be broken across pages. – Torbjørn T. Jan 23 '14 at 19:40
  • Yes, but it is not centered. Adding this requirement to the mix complicates things, since many solutions (e.g., minipage) are not floatable. – dfernan Jan 23 '14 at 20:26
  • Could you please post some minimal working example (MWE), to get people started on solving your problem? – jub0bs Jan 23 '14 at 20:34
  • @Jubobs I just added more details. – dfernan Jan 23 '14 at 21:15
  • Good. Now please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – jub0bs Jan 23 '14 at 23:13
3

You can use xleftmargin and xrightmargin to give a "centered" looking to your listing.

For example:

\documentclass{article}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[%
  float=htbp,
  caption={Very large Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption.},
  label={lst:stuff},
  xleftmargin=2cm,
  xrightmargin=2cm
  ]
Stuff
\end{lstlisting}

This is my listing no. \ref{lst:stuff}
\end{document} 

Output:

enter image description here

Adjust 2cm to better fit your needs.

  • Much cleaner solution. Didn't know about the margin arguments. Thanks! – dfernan Jan 27 '14 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.