# Extra vertical glue in listings within beamer frame

The following blocks are the result of the source below. You can see the difference in vertical spacing. Where is it coming from and how to get rid of it?

\documentclass[a4paper]{beamer}

\usepackage{listings}
\usetheme{Warsaw}

\begin{document}
\lstset{language=Python, columns=fullflexible}
\begin{frame}[fragile]{Test}
\begin{block}{Excessive vertical space both above and below}
\begin{lstlisting}
nums = list(range(1,10))
\end{lstlisting}
\end{block}
\begin{block}{No problems with vertical spacing}
Is there?
\end{block}
\end{frame}
\end{document}


lstlisting is an environment like a figure or an equation and includes in its definition vertical spaces above and below of it.

You could control this vertical above and below spaces by using the command: \lstset{aboveskip=0pt, belowskip=0pt}

This way you could have a result like:

\documentclass$a4paper${beamer}

\usepackage{listings}
\usetheme{Warsaw}

\begin{document}
\lstset{language=Python, columns=fullflexible}
\begin{frame}$fragile${Test}
\begin{block}{Excessive vertical space both above and below}
\lstset{aboveskip=0pt,belowskip=0pt}
\begin{lstlisting}
nums = list(range(1,10))
\end{lstlisting}
\end{block}
\begin{block}{No problems with vertical spacing}
Is there?
\end{block}
\end{frame}
\end{document}


creating an environment like lstblock could be useful too:

\newenvironment{lstblock}[1]
{\begin{block}{#1}
\lstset{aboveskip=0pt,belowskip=0pt}
\begin{lstlisting}}
{\end{lstlisting}\end{block}}

• Your answer removes the biggest vertical space, but there seems still to be a small amount of vertical glue left compared to a text block. I've tried setting parskip to 0, but that does not seem to be it. – hkBst Oct 6 '19 at 7:00