设置跳行会影响列表中的标题

设置跳行会影响列表中的标题

如果我lineskip在 中指定了lstlisting,它也会影响标题。我该如何防止这种情况发生?

例子:

\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}

在此处输入图片描述

答案1

您可以使用captionsetspace

\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}

在此处输入图片描述

答案2

\lst@MakeCaption要做的补丁\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}

在此处输入图片描述

相关内容