When using the lstlisting environment, or similar ones from the listings package, there are paragraph-like margins above and below the actual text. This makes sense in a paper, but gets in the way if you try to put code inside a Tikz node (e.g., for a Beamer presentation):

 \node[inner sep=1mm, text width=6cm, draw=green, line width=2pt, rounded corners]
    \lstinputlisting[basicstyle=\tiny\ttfamily, columns=fullflexible]{alt1.java} 

listing with margins

(Do ignore the question of why I put this jumbled code in a node...)

As you can see there are top and bottom margins that add extra redundant space to the node.

How can I remove these margins? I suppose it is some lstset option, but I couldn't find it.


The space above and below the listing can be set using aboveskip and belowskip. The default value is \medskipamount.



\tikz \node[inner sep=1mm, text width=6cm, draw=green, line width=2pt, rounded corners]
    \lstinputlisting[basicstyle=\tiny\ttfamily, columns=fullflexible, breaklines=true,aboveskip=0pt,belowskip=0pt]{data.txt} 
  • That seems to solve most of the problem. There's still some small space left, although much narrower. Do you know how to get rid of it? – Little Bobby Tables Jan 18 '12 at 13:49
  • @LittleBobbyTables: Remove the inner sep=1mm from the \node option keys. – Daniel Jan 18 '12 at 15:25

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.