17

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.

6
  • 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
25

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}
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}
3
  • 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

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.