21

How can I set the space between a listing (lstlisting) and text that surrounds it? Is there a parameter I can set at the beginning of my document, that controls this? I tried framesep, but it just inflates the frame box.

EDIT:

text
\addvspace\medskipamount
\begin{lstlisting}
...
\end{lstlisting}

\addvspace does what I need. How do I set this at the beginning of the document, so I don't have to add this manually to every listing?

EDIT:

I use listings like so:

\begin{lstlisting}[caption={cap},label=lbl]
 ... 
\end{lstlisting}

Where cap and lbl are different captions/labels, so they are not constant for all listings.

qwe
  • 213
  • Try \addvspace\medskipamount? Don't understand your question really well though. Or maybe a \vskip5pt. Is this a horizontal space or vertical space? – azetina Aug 26 '12 at 19:42
  • @azetina: I updated my question. – qwe Aug 26 '12 at 19:49
  • @qwe Why not make it into a \newenvironment or a \newcommand like \newcommand{\mylist}[1]{\addvspace\medskipamount \begin{lstlisting} {#1} \end{lstlisting}} – azetina Aug 26 '12 at 19:49
  • listings provides aboveskip and belowskip, both of which are \medskipamount by default. You could increase this using \lstset{aboveskip=\bigskipamount, belowskip=\bigskipamount} in your document preamble. – Werner Aug 26 '12 at 20:04
  • @Werner I didn't notice your comment while I was typing my answer. – lockstep Aug 26 '12 at 20:12
  • @lockstep: I snooze, I lose. :-| No worries. – Werner Aug 26 '12 at 20:15

2 Answers2

30

Use the \lstset command and its aboveskip and belowskip keys. See section 4.3 of the manual for details.

\documentclass{article}

\usepackage{listings}

\lstset{aboveskip=20pt,belowskip=20pt}

\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{lstlisting}[caption={A listing}]
(Listing contents)
\end{lstlisting}

\lipsum[2]

\end{document}
lockstep
  • 250,273
2

You can use a \newcommand:

\newcommand{\myconfiguredlisting}[1]{%
\addvspace\medskipamount %
\begin{lstlisting}[caption={cap},label=lbl] 
{#1} 
\end{lstlisting}}

Then use it in the form:

\myconfiguredlisting{content of listing goes in here}

Or

\newenvironment{mylst}
  {\addvspace\medskipamount %
    \begin{lstlisting}[caption={cap},label=lbl]}
  {\end{lstlisting}}

Then use the above as:

\begin{mylst}
....... % lst content goes here.
\end{mylst}
azetina
  • 28,884
  • I use lstlisting like so: \begin{lstlisting}[caption={cap},label=lbl] ... \end{lstlisting}. Your solution doesn't seem to handle parameters? – qwe Aug 26 '12 at 19:58
  • I know that, just edit the solution as you please. I cannot update my answer thoroughly for now but I presume someone will post a more appropriate answer. – azetina Aug 26 '12 at 20:01
  • Sorry for being imprecise, I edited my question yet again. – qwe Aug 26 '12 at 20:05