Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

enter image description here

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.

share|improve this question
1  
Welcome to TeX.SX! A tip: You can use backticks ` to mark your inline code as I did in my edit. –  Adam Liter Mar 23 at 21:07

1 Answer 1

up vote 7 down vote accepted

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

#include <iostream>

Because \end{lstlisting} hasn't been detected yet, listings prints that newline character verbatim. The same phenomenon occurs with the verbatim environment; see the "side note" in this answer.

That blank line will go away if you remove the newline character in question, by writing

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

instead of

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

See section 2 in my code below.

Alternatively, if you load the lstautogobble package and activate the autogobble 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 an lstlisting 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.

enter image description here

\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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.