更改代码列表环境中的行距

更改代码列表环境中的行距

我无法为代码环境设置较小的行距。如何强制设置?basicstyle=\linespread{0.8}没有任何改变。

\documentclass[12pt, a4paper]{report}
\usepackage{listings}
\linespread{1.5}

\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{10pt}

\lstset{
  basicstyle=\ttm\linespread{0.8},
}

\begin{document}
The line spacing is perfect here.

The line spacing is perfect here too.

\begin{lstlisting}
The code has 
too high 
line spacing
\end{lstlisting}

The line spacing is perfect here.

The line spacing is perfect here too.

\end{document}

答案1

您需要添加命令\selectfont来“激活”行距变化:

\lstset{
  basicstyle=\ttm\linespread{0.8}\selectfont
}

在此处输入图片描述

如果您不喜欢列表下方的额外空格,您也可以设置belowskip=0pt

相关内容