如何去除清單末端多餘的空白行?

如何去除清單末端多餘的空白行?

我有一個問題,我lstlisting在 an 內部使用 a itemize。這導致了我的原始程式碼區塊中出現額外一行的問題,我無法刪除該行。我已經嘗試了幾乎所有影響間距的參數,但沒有任何效果。這是我的程序:

\documentclass[a4paper]{scrartcl}
\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage[latin1]{inputenc}
\usepackage[ngerman,english]{babel}

\usepackage{listings}
\lstset{language=[Visual]{C++},
keepspaces=true,
tabsize=2,
xrightmargin=\parindent,
basicstyle=\ttfamily,
frame=single,
framesep=1mm,
framerule=0pt,
columns=fullflexible,
backgroundcolor=\color[gray]{0.9}}

\begin{document}
There are two kinds of header files:
\begin{itemize}
  \item content
  \begin{lstlisting}[belowskip=-\baselineskip]
  #include <iostream>
  \end{lstlisting}
  \item more content.
\end{itemize}
\end{document}

這會產生以下輸出:

在此輸入影像描述

我已經看過有關此主題的帖子,如下所示:清單環境內的 lstlisting 環境下方的垂直額外空間 但設定\parsep0pt對我來說不起作用。

我也嘗試過

\begin{lstlisting}[belowskip=-\baselineskip]
#include <iostream>
\end{lstlisting}

就像另一篇文章中建議的那樣,但我的框架有問題。

答案1

程式碼末尾的空白行是由嵌入列表末尾(即行尾)的換行符引起的

#include <iostream>

因為\end{lstlisting}尚未偵測到,listings所以列印該換行符逐字。同樣的現像也發生在verbatim環境中。請參閱“旁注”這個答案

如果您刪除有問題的換行符,則該空白行將消失,方法是:

\begin{lstlisting}
#include <iostream>\end{lstlisting}

代替

\begin{lstlisting}
#include <iostream>
\end{lstlisting}

請參閱下面我的程式碼中的第 2 部分。

或者,如果您載入lstautogobble套件並啟動autogobble金鑰(這在此處是可取的,以防止輸出中出現過多縮排),則多餘的空白行就會消失;請參閱下面我的程式碼中的第 3 部分。

另請注意,獨立清單不需要採取任何對策,即清單駐留在外部來源檔案中並透過\lstinputlisting巨集插入文件中。額外的空行問題僅發生在嵌入式清單(即在環境中包含的清單lstlisting)中。請參閱下面我的程式碼中的第 4 節。

最後,關於空間在列表中,我通常會調整全域的值belowskip;就你而言,-0.5em似乎合適。

在此輸入影像描述

\documentclass[a4paper]{scrartcl}

\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage[latin1]{inputenc}
\usepackage[ngerman,english]{babel}

\usepackage{listings}
\usepackage{lstautogobble}
\lstset
{
  language        = [Visual]{C++},
  keepspaces      = true,
  tabsize         = 2,
  xrightmargin    = \parindent,
  basicstyle      = \ttfamily,
  frame           = single,
  framesep        = 1mm,
  framerule       = 0pt,
  columns         = fullflexible,
  belowskip       = -0.5em,
  backgroundcolor = \color[gray]{0.9},
}

\usepackage{filecontents}
\begin{filecontents*}{sample.c}
#include <iostream>
\end{filecontents*}


\begin{document}

\section{Your listing}
There are two kinds of header files:
\begin{itemize}
  \item content
  \begin{lstlisting}
  #include <iostream>
  \end{lstlisting}
  \item more content.
\end{itemize}

\section{Your listing without the newline at the end}
There are two kinds of header files:
\begin{itemize}
  \item content
  \begin{lstlisting}
  #include <iostream>\end{lstlisting}
  \item more content.
\end{itemize}

\section{Your listing with \texttt{autogobble}}
There are two kinds of header files:
\begin{itemize}
  \item content
  \begin{lstlisting}[autogobble]
  #include <iostream>
  \end{lstlisting}
  \item more content.
\end{itemize}

\section{Your listing inserted from an external file}
There are two kinds of header files:
\begin{itemize}
  \item content
    \lstinputlisting{sample.c}
  \item more content.
\end{itemize}

\end{document}

相關內容