내 목록 끝에 추가 빈 줄을 어떻게 제거합니까?

내 목록 끝에 추가 빈 줄을 어떻게 제거합니까?

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}

그러면 다음과 같은 출력이 생성됩니다.

여기에 이미지 설명을 입력하세요

나는 이미 다음과 같이 이 주제를 다루는 게시물을 보았습니다.목록 환경 내부의 목록 환경 아래 수직 추가 공간 그러나 설정 \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}

관련 정보