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

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

  • Thanks. Accepted until an easy listings-only solution appears. – Jellby Aug 25 '15 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.