リストの最後にある余分な空白行を削除するにはどうすればよいですか?

リストの最後にある余分な空白行を削除するにはどうすればよいですか?

lstlistingを 内で使用してしまうという問題があります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}

次のような出力が生成されます。

ここに画像の説明を入力してください

このトピックを扱った次のような投稿をすでに見てきました:リスト環境内のリスト環境の下の垂直方向の余分なスペース しかし、設定\parsepしても0ptうまくいきませんでした。

私も試してみました

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

関連情報