# Avoid insertion of space below section title with aboveskip, belowskip in listings environment

I was using the listings package to place source code in my document and I noticed that the space between the text and the lstlisting environment was to little. So I changed the \aboveskip and \belowskip properties.

The problem with that was that along with the space I mentioned it was also affected the space between a section header and a lstlisting environment which is useless and annoying. Also it does not produce a nice document appearance.

Also I don't know if there is the same behaviour in the space between the environment and page breaks or the beginnings of a new page (is it?) or LaTeX erase that space by itself (if it is actually inserted there too).

My question is how can I prevent the space inserted with \aboveskip and \belowskip affect the space between section headers, page breaks and beginning of pages?

This is without the \aboveskip:

This is with \aboveskip=1.5cm (an excessive number to show the "unnecessary" space under the section title):

\documentclass[12pt]{article}

\usepackage{parskip}

\setlength{\parindent}{0cm}

\usepackage{fontspec}

\usepackage[english,greek]{babel}

\usepackage[fleqn]{amsmath}

\usepackage{unicode-math}

\usepackage{listings}

\setmainfont
[
Ligatures=TeX,
Extension=.otf,
UprightFont=*,
BoldFont=*Bold,
ItalicFont=*It,
BoldItalicFont=*BoldIt,
Mapping=tex-text
]{GFSArtemisia}

\setsansfont[Mapping=tex-text]{GFSArtemisia.otf}

\setmathfont{latinmodern-math.otf}

\setmathfont[range=\varnothing]{Asana-Math.otf}

\setmathfont[range=\int]{latinmodern-math.otf}

\begin{document}

\lstset
{
aboveskip=1.5cm,
frame=tb,
rulecolor=\color{black},
basicstyle=\ttfamily\color{BrickRed},
columns=flexible,
morecomment=[s][\color{NavyBlue}]{/*}{*/}
}

\section*{Section Title}

\begin{lstlisting}

Source code here.

\end{lstlisting}

Text here!

\begin{lstlisting}

More source code here.

\end{lstlisting}

More text here!

\end{document}

• Can you show an example? Yes, a MWE is necessary to understand the point. – egreg Dec 19 '14 at 7:27
• Really you don't need to ask: every question is better with an MWE if only to help people test possible answers. – David Carlisle Dec 19 '14 at 9:43
• @David Carlisle I edited my question. – Adam Dec 19 '14 at 19:54
• @egreg I uploaded 2 pictures showing the problem. – Adam Dec 19 '14 at 19:55

The requirement seems a little strange but anyway you can make the vale expand to any dimension based on whatever you want to test, here I use \if@nobreaktrue as an approximation for "follow heading" it's an internal switch latex uses to avoid page breaks after a heading.

\documentclass[12pt]{article}

\usepackage{parskip}

\setlength{\parindent}{0cm}

\usepackage{fontspec}

\usepackage[english,greek]{babel}

\usepackage[fleqn]{amsmath}

\usepackage{unicode-math}

\usepackage{listings}

\setmainfont
[
Ligatures=TeX,
Extension=.otf,
UprightFont=*,
BoldFont=*Bold,
ItalicFont=*It,
BoldItalicFont=*BoldIt,
Mapping=tex-text
]{GFSArtemisia}

\setsansfont[Mapping=tex-text]{GFSArtemisia.otf}

\setmathfont{latinmodern-math.otf}

\setmathfont[range=\varnothing]{Asana-Math.otf}

\setmathfont[range=\int]{latinmodern-math.otf}

\begin{document}

\makeatletter
\lstset
{
aboveskip={\if@nobreak 0pt\else 1.5cm\fi},
frame=tb,
rulecolor=\color{black},
basicstyle=\ttfamily\color{BrickRed},
columns=flexible,
morecomment=[s][\color{NavyBlue}]{/*}{*/}
}
\makeatother
\section*{Section Title}

\begin{lstlisting}

Source code here.

\end{lstlisting}

Text here!

\begin{lstlisting}

More source code here.

\end{lstlisting}

More text here!

\end{document}

• Thank you very much! Why do you think that this is strange? – Adam Dec 19 '14 at 21:10
• @Adam the original "affect where it shouldn't" title made it sound as if the setting was affecting disconnected space choices elsewhere in the document (not impossible in latex:-) but here you are specifying space above a listing and getting space above a listing which seems pretty much exactly what is expected. Space will go at a page break but why after a section but not after other things that add vertical space eg a list or another listing? – David Carlisle Dec 19 '14 at 21:27
• You are right in a way, but I think that the spaces I mentioned shouldn't be affected. I mean it is not logical to add more space under a section as it already has its own, so there should be a different option in case you wanted to do this. The document, in me opinion, would be more beautiful. :) – Adam Dec 20 '14 at 15:09