# Center just one line of code (e.g. comment)

In my lstlisting I want to place a comment in the form of

/*====== some section ====== */


but having it centered and not like the rest of the lines of code which are left aligned. Using the escape char followed by

\begin{center}
...
\end{center}


won't work, because I want to stay in the lstlisting mode and

\centering


is not doing anything at all.

Is there a solution for my requirements?

-

Insert the code you want centred in a \makebox[\linewidth]:

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\begin{document}
\begin{lstlisting}[basicstyle=\ttfamily,mathescape,frame=lr]
Here is some code
$\makebox[\linewidth]{/*~====~Some comment~====~*/}$
Here is some more code
\end{lstlisting}
\end{document}


The default alignment for a fixed-width \makebox is centred.

For obtaining the appropriate comment style associated with your \lstset, you could use

...
$\makeatletter% \makebox[\linewidth]{\lst@commentstyle/*~====~Some comment~====~*/}% \makeatother$
...


This escapes under math but inserts the comment style \lst@commentstyle before printing the actual comment. However, this is not perfect, since escaping seems to remove certain font settings. A more robust (yet cumbersome) approach is to follow Martin's suggestion: storing the content of a comment in a box outside the listing, and then including it again:

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\lstset{language=C,basicstyle=\ttfamily}
\newsavebox{\lstsavebox}
\begin{document}
\begin{lrbox}{\lstsavebox}
\begin{lstlisting}
/* ==== Some comment ==== */
\end{lstlisting}
\end{lrbox}
\begin{lstlisting}[mathescape,frame=lr]
Here is some code
$\makeatletter% \makebox[\linewidth]{\lst@commentstyle/*~====~Some comment~====~*/}% \makeatother$
Here is some code
$\makebox[\linewidth]{\strut\smash{\usebox{\lstsavebox}}}$
\end{lstlisting}
\end{document}


\strut\smash{...} removes any additional height contained within the box before setting it at "regular" height.

-
I assume there is no way to convince latex to format the content of the makebox (in this case the comment) as it was defined in \lstset{...} e.g. coloring it accordingly, since we have excaped to latex mode? –  David Jan 23 '13 at 20:31
@David: listings stores the style of comments in \lst@commentstyle. So you could use this to modify the settings for the centred comment line to match that of the other comments: $\makeatletter\makebox[\linewidth]{\lst@commentstyle/*~====~Some comment~====~*/}\makeatother$ Of course, wrapping it in a macro would also be possible. –  Werner Jan 23 '13 at 20:39
@David: You could also use a savebox to store the one comment with correct formatting using \lstinline and the include it in the listing inside the \makebox. Unfortunately you can't use \lstinline inside the math-escaped code directly. –  Martin Scharrer Jan 23 '13 at 21:38
I figured out that, \lst@commentstyle gives me the right formatting in terms of color and font, but does not restore the char spacing. In lstlisting environment the source code is monospaced yet the font used is not. Writing something like //some comment...$\makeatletter \lst@commentstyle ... \makeatother$ makes that obvious. The first three dots are monospaced and farther apart than the second ldots although I assume this should not be the case. Why is that? –  David Jan 26 '13 at 7:34
@David: I've added some alternative using Martin's suggestion. –  Werner Jan 26 '13 at 18:07