7

If I place an lstlisting directly after the start of a (sub)section, the margin before the following (sub)section (title) disappears.

Here is a an example document showing the issue:

\documentclass[twocolumn]{article}
\usepackage{listings}
\begin{document}

\section{Lorem ipsum}
Consectetur adipiscing elit.

\begin{lstlisting}
Lorem
\end{lstlisting}

Sed quis ante nec mi volutpat congue eu id leo.

\section{Dolor sit amet}
Suspendisse at lorem mattis, semper massa id, euismod risus.

\newpage

\section{Lorem ipsum}
\begin{lstlisting}
Lorem
\end{lstlisting}

Sed quis ante nec mi volutpat congue eu id leo.

\section{Dolor sit amet}
Suspendisse at lorem mattis, semper massa id, euismod risus.

\end{document}

And you can see the (annotated) result here:

Annotated screenshot of the PDF created using the example document

Why is there no margin before the fourth section (title)? Is this a bug in my LaTeX environment (MacTeX-2013), or is this the expected behavior?

6

I'd say is to be expected since lstlisting will be found still in vertical mode and the environment doesn't take any precautions here. You can prevent the undesired result using \leavevmode before the offending listing:

\documentclass[twocolumn]{article}
\usepackage{listings}
\begin{document}

\section{Lorem ipsum}
Consectetur adipiscing elit.

\begin{lstlisting}
Lorem
\end{lstlisting}

Sed quis ante nec mi volutpat congue eu id leo.

\section{Dolor sit amet}
Suspendisse at lorem mattis, semper massa id, euismod risus.

\newpage

\section{Lorem ipsum}
\leavevmode\begin{lstlisting}
Lorem
\end{lstlisting}

Sed quis ante nec mi volutpat congue eu id leo.

\section{Dolor sit amet}
Suspendisse at lorem mattis, semper massa id, euismod risus.

\end{document}

enter image description here

|improve this answer|||||
  • Thanks, this works fine! I'll have to read more about this (vertical) mode in LaTeX. Maybe the behavior will make sense then. – Jonathan Mar 12 '14 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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