更改程式碼清單環境中的行間距

更改程式碼清單環境中的行間距

我無法獲得程式碼環境的小行間距。怎麼強制呢?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

相關內容