1

I have put some pseudocode in a listings object, with a caption and box around it. I have used the fancybox solution to listings objects suggested here: How to center a lstlisting

Everything is fine, except the caption isn't centered.

Here's the output:

Output of listings object with non-centered caption

Here's the code:

\documentclass[paper=a4, fontsize=11pt]{article} % A4 paper and 11pt font size
\usepackage[top=0.8in, bottom=0.8in, left=0.8in, right=0.8in]{geometry}

\usepackage{fourier} % Use the Adobe Utopia font for the document
\usepackage[english]{babel} % English language/hyphenation

\usepackage{listings}
\lstset{numbers=left, numbersep=10pt, frame = single, framexleftmargin=17pt}

\usepackage{fancybox}
\makeatletter
\newenvironment{CenteredBox}{% 
\begin{Sbox}}{% Save the content in a box
\end{Sbox}\centerline{\parbox{\wd\@Sbox}{\TheSbox}}}% And output it centered
\makeatother

\usepackage{sectsty} % Allows customizing section commands
\allsectionsfont{\centering \normalfont\scshape} % Make all sections centered, the default font and small caps

\begin{document}
\section{Chapter}
\begin{CenteredBox}
\begin{lstlisting}[caption={Cool code},label=cool,linewidth=6in]
validity_type <- 0,1,2
uid <- '00000'
start_date <- '2014-11-20 00:00:00'
end_date <- '2014-11-21 00:00:00'

sql_query <- "SELECT variable_1
         , variable_2
         , variable_3
         FROM prices
         WHERE validity_type = ",validity_type," AND
         uid = '",uid,"'
         GROUP BY variable_1, variable_2, variable_3
         HAVING from_ts >= '",start_date,"' AND from_ts < '",end_date,"'
         ORDER BY variable_1 ASC, variable_2 ASC")
\end{lstlisting}
\end{CenteredBox}

\end{document}

Any help is much appreciated.

fifthace
  • 197
  • 1
  • 6

1 Answers1

2

There are several things going on here. The first is that a normal lstlisting is left aligned. In addition, the line numbering (normally) is left-lapped and thus protrudes into the left margin. Finally, the OP's MWE arbitrarily chooses 6in as the listing linewidth, a number that is not intrinsically related to the \textwidth.

Without the use of the CenteredBox environment, the label is centered. But, when the lstlisting box is centered, the label moves rightward with it. It required two changes, both to bring the label back to the center, and indeed to center the box:

  1. First, I made the caption centered, by adding space following the caption, as in caption={Cool code\hspace{\dimexpr\textwidth-6in+2ex}}. The portion that is \textwidth-6in represents the difference between the text width and the specified lstlisting width, whereas the 2ex is my estimate as to the additional lapped width arising from the two-digit numbered labels on the left edge of the listing.

  2. The other thing I did was to add the space \hspace{\dimexpr.5\textwidth-3in+1ex} to the end of the listing, so as to shift the listing leftward. Note that this distance is exactly 1/2 of that added to the caption width.


\documentclass[paper=a4, fontsize=11pt]{article} % A4 paper and 11pt font size
\usepackage[top=0.8in, bottom=0.8in, left=0.8in, right=0.8in]{geometry}

\usepackage{fourier} % Use the Adobe Utopia font for the document
\usepackage[english]{babel} % English language/hyphenation

\usepackage{listings}
\lstset{numbers=left, numbersep=10pt, frame = single, framexleftmargin=17pt}

\usepackage{fancybox}
\makeatletter
\newenvironment{CenteredBox}{% 
\begin{Sbox}}{% Save the content in a box
\end{Sbox}\centerline{\parbox{\wd\@Sbox}{\TheSbox}}}% And output it centered
\makeatother

\usepackage{sectsty} % Allows customizing section commands
\allsectionsfont{\centering \normalfont\scshape} % Make all sections centered, the default font and small caps

\begin{document}
\section{Chapter}
\noindent\rule{.5\textwidth}{2pt} this line is .5 textwidth

\begin{CenteredBox}
\begin{lstlisting}[caption={Cool code\hspace{\dimexpr\textwidth-6in+2ex}},%
   label=cool,linewidth=6in]
validity_type <- 0,1,2
uid <- '00000'
start_date <- '2014-11-20 00:00:00'
end_date <- '2014-11-21 00:00:00'

sql_query <- "SELECT variable_1
         , variable_2
         , variable_3
         FROM prices
         WHERE validity_type = ",validity_type," AND
         uid = '",uid,"'
         GROUP BY variable_1, variable_2, variable_3
         HAVING from_ts >= '",start_date,"' AND from_ts < '",end_date,"'
         ORDER BY variable_1 ASC, variable_2 ASC")
\end{lstlisting}
\hspace{\dimexpr.5\textwidth-3in+1ex}
\end{CenteredBox}

\medskip
\noindent\hrulefill\\
the above line is textwidth

\end{document}

enter image description here