7

I am using the listings package and I want to automatically add a blank line before every listing. Any idea on how to do this?

lockstep
  • 250,273
jamtur01
  • 281

2 Answers2

8

listings has a parameter aboveskip that defines the space above a listing. Set globally by adding \lstset{aboveskip=<length>} to the preamble.

enter image description here

\documentclass[11pt]{report} 
\usepackage{listings}
\lstset{aboveskip=2\baselineskip,
frame=single}
\usepackage{kantlipsum}

\begin{document}
\kant[1]
\begin{lstlisting}
This be a listing.
\end{lstlisting}
\kant[3]
\end{document}
Torbjørn T.
  • 206,688
2

The easiest would be to create your own environment that adds the skip:

enter image description here

\documentclass{article}
\usepackage{lipsum,listings}% http://ctan.org/pkg/{lipsum,listings}
\lstnewenvironment{mylisting}[1][,]
  {\vspace*{\baselineskip}%
   \lstset{#1}% Add more default options here
  }{}
\begin{document}
\lipsum[1]
\begin{mylisting}
Here is a listing
\end{mylisting}
\lipsum[2]
\end{document}

The use of \vspace* will insert a skip of \baselineskip (an empty line) even if the listing starts at the top of the page. If you wish to not have this, using \vspace instead should do the trick. As reference, see Adding vertical space at the start of a page.

Werner
  • 603,163