[Tex/LaTex] Remove top and bottom margins around listings

beamerlistingsspacingtikz-pgf

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.

Best Answer

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

\documentclass{article}

\usepackage{tikz}
\usepackage{listings}


\begin{document}
\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} 
  };   
\end{document}