7

I am using @egreg's answer to make listings centered:

\documentclass[a4paper]{book}
\usepackage[T1]{fontenc}

\usepackage[
  hang,
  small,
  it,
  hypcap=true
]{caption}
\usepackage{fancyvrb}
\usepackage{listings}
\lstset{
  fancyvrb=true,
}
\usepackage{varwidth}

\lstset{
    basicstyle=\linespread{0.94}\ttfamily,
    fancyvrb=true,
    captionpos=b
}

\begin{document}

\begin{center}
\begin{varwidth}{\linewidth}
\begin{lstlisting}
trait Sys[S <: Sys[S]] {
  type Tx     <: stm.Txn[S]
  type Var[A] <: stm.Var[S#Tx, A]
  type ID     <: stm.Identifier[S#Tx]
  type Acc
  ...
}
\end{lstlisting}\end{varwidth}
\captionof{lstlisting}{I want to be centered}\label{lst:label2}
\end{center}

\end{document}

This looks ok at first:

enter image description here

However: Something odd happens. When I add the color package, the alignment is broken:

\usepackage{color}  % add somewhere to above document

Result:

enter image description here

Please help. I need to include the color package. How can it break the alignment?

3
  • One workaround (until someone answers) is \begin{varwidth}{.65\linewidth}. Of course, you would hate to have to do that with a different multiplier for every listing. Aug 13, 2014 at 19:46
  • 3
    I think that listings wrongly sets colors without using the appropriate interface, so it does \normalcolor in a place that breaks varwidth. If I remove \normalcolor from \lst@DeInit, there is no problem with the code, but I'm afraid this could cause color leakage.
    – egreg
    Aug 13, 2014 at 20:44
  • This looks like a bug, which should be reported to the current maintainer of listings.
    – jub0bs
    Aug 15, 2014 at 8:18

1 Answer 1

1

This adds an extra brace group to make the box setting in \lst@DeInit "color safe" then removes \normalcolor as egreg mentioned in comments. Seems to work on this example..

\documentclass[a4paper]{book}
\usepackage[T1]{fontenc}

\usepackage{color}

\usepackage[
  hang,
  small,
  it,
  hypcap=true
]{caption}
\usepackage{fancyvrb}
\usepackage{listings}
\lstset{
  fancyvrb=true,
}
\usepackage{varwidth}

\lstset{
    basicstyle=\linespread{0.94}\ttfamily,
    fancyvrb=true,
    captionpos=b
}
\makeatletter
\def\lst@DeInit{%
    \lst@XPrintToken \lst@EOLUpdate
    \global\advance\lst@newlines\m@ne
    \lst@ifshowlines
        \lst@DoNewLines
    \else
        \setbox\@tempboxa\vbox{{\lst@DoNewLines}}%extra group for color
    \fi
    \lst@ifdisplaystyle \par\removelastskip \fi
    \lsthk@ExitVars\everypar{}\lsthk@DeInit\normalbaselines
    \lst@MakeCaption b%
    \lst@ifbox
        \egroup $\hss \egroup
        \vrule\@width\lst@maxwidth\@height\z@\@depth\z@
    \else
        \lst@ifdisplaystyle
            \par\penalty-50\vspace\lst@belowskip
        \fi
    \fi
    \ifx\lst@multicols\@empty\else
        \def\lst@next{\global\let\@checkend\@gobble
                      \endmulticols
                      \global\let\@checkend\lst@@checkend}
        \expandafter\lst@next
    \fi
    \ifx\lst@float\relax\else
        \expandafter\lst@endfloat
    \fi
    \endgroup}
\makeatother
\begin{document}

\begin{center}
\begin{varwidth}{\linewidth}
\tracingmacros2
\begin{lstlisting}
trait Sys[S <: Sys[S]] {
  type Tx     <: stm.Txn[S]
  type Var[A] <: stm.Var[S#Tx, A]
  type ID     <: stm.Identifier[S#Tx]
  type Acc
  ...
}
\end{lstlisting}\end{varwidth}
\captionof{lstlisting}{I want to be centered}\label{lst:label2}
\end{center}

\end{document}

You must log in to answer this question.

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