3

If I specify a lineskip in lstlisting, it affects the caption too. How can I prevent it?

Example:

\documentclass[a5paper]{article}
\usepackage{listings}
\begin{document}

\begin{lstlisting}[float,
basicstyle=\ttfamily\footnotesize,
lineskip=-0.7ex,
columns=fixed,basewidth=0.5em,
captionpos=b,
caption={Long caption text that spans at least two lines to show the lineskip.}
]
one
two
three
\end{lstlisting}

\begin{lstlisting}[float,
basicstyle=\ttfamily\footnotesize,
%lineskip=-0.7ex,
columns=fixed,basewidth=0.5em,
captionpos=b,
caption={Long caption text that spans at least two lines to show the lineskip.}
]
one
two
three
\end{lstlisting}

\end{document}

enter image description here

2 Answers 2

1

You can use caption package with setspace.

\documentclass[a5paper]{article}
\usepackage{listings}
\usepackage{caption}
\usepackage{setspace}
\captionsetup[lstlisting]{font={stretch=1}}
\begin{document}

\begin{lstlisting}[float,
basicstyle=\ttfamily\footnotesize,
lineskip=-0.7ex,
columns=fixed,basewidth=0.5em,
captionpos=b,
caption={Long caption text that spans at least two lines to show the lineskip.}
]
one
two
three
\end{lstlisting}

\begin{lstlisting}[float,
basicstyle=\ttfamily\footnotesize,
%lineskip=-0.7ex,
columns=fixed,basewidth=0.5em,
captionpos=b,
caption={Long caption text that spans at least two lines to show the lineskip.}
]
one
two
three
\end{lstlisting}

\end{document}

enter image description here

1
  • Thanks. Accepted until an easy listings-only solution appears.
    – Jellby
    Aug 25, 2015 at 13:36
0

Patch \lst@MakeCaption to do \linespread{1}:

\documentclass[a5paper]{article}
\usepackage{listings}
\usepackage{etoolbox}

\makeatletter
\patchcmd\lst@MakeCaption
  {\ignorespaces}
  {\linespread{1}\selectfont\ignorespaces}
  {}{}
\makeatother

\begin{document}

\begin{lstlisting}[
  float,
  basicstyle=\ttfamily\footnotesize,
  lineskip=-0.7ex,
  columns=fixed,
  basewidth=0.5em,
  captionpos=b,
  caption={Long caption text that spans at least two lines to show the lineskip.}
]
one
two
three
\end{lstlisting}

\begin{lstlisting}[
  float,
  basicstyle=\ttfamily\footnotesize,
  columns=fixed,basewidth=0.5em,
  captionpos=b,
  caption={Long caption text that spans at least two lines to show the lineskip.}
]
one
two
three
\end{lstlisting}

\end{document}

enter image description here

You must log in to answer this question.

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