I have the problem that I use a lstlisting
inside an itemize
. This leads to the problem of an extra line in my source code block which I'm not able to delete. I already tried nearly every parameter which affects spacing but nothing worked. This is my program:
\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}
which produces me the following output:
I've already seen posts dealing with this topic like this one: vertical extra space below lstlisting environment inside a list environment
But setting \parsep
to 0pt
didn't work for me.
I also tried
\begin{lstlisting}[belowskip=-\baselineskip]
#include <iostream>
\end{lstlisting}
like suggested in another post but then I have a problem with my frame.
Best Answer
The blank line at the end of your code is caused by the newline character at the very end of your embedded listing, i.e. at the end of the line
Because
\end{lstlisting}
hasn't been detected yet,listings
prints that newline character verbatim. The same phenomenon occurs with theverbatim
environment; see the "side note" in this answer.That blank line will go away if you remove the newline character in question, by writing
instead of
See section 2 in my code below.
Alternatively, if you load the
lstautogobble
package and activate theautogobble
key (which is desirable here, to prevent too much indentation in the output), that extra blank line disappears; see section 3 in my code below.Also, note that no countermeasure is required for standalone listings, i.e. listings residing in external source files and inserted in your document via the
\lstinputlisting
macro. The extra blank line problem only occurs for embedded listings (i.e. listings contained within anlstlisting
environment). See section 4 in my code below.Finally, regarding the space after the listing, I usually tweak the value of
belowskip
globally; in your case,-0.5em
seems appropriate.