Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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

2 Answers 2

up vote 6 down vote accepted

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}
share|improve this answer

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}
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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