I have received a wonderful comment how to get listings centered:
\documentclass[a4paper]{book}
%\usepackage[hang,small,it,hypcap=true]{caption} % screw hypcap
\usepackage[T1]{fontenc}
\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}[caption={I want to be centered},label={lst:label2}]
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}
\end{center}
\end{document}
So far so good:
If I add the caption
package (uncomment in the example above) which I must do because a lot of stuff relies on it, the centering is destroyed:
I also found that adding the showframe
package has the same problem, so perhaps the culprit is center
or varwidth
and not caption
. How to fix this?