[Tex/LaTex] How to reduce the line spacing in a listing

line-spacinglistings

I have some listings in my beamer presentation. Sometimes, the last line of the code I want to explain doesn't fit on the slide. I've already configured the font size to scale .7:

\newfontfamily\listingsfont[Scale=.7]{DejaVu Sans Mono}
\lstset{basicstyle=\listingsfont}

Is there a possibility to reduce the line spacing in a listing as well?

Best Answer

To reduce the space between lines, you can invoke \linespread{<x>} in the value passed to the basicstyle key, where <x> is a number smaller than 1, as in this answer by egreg. Don't abuse it, though.

enter image description here

\documentclass{article}
\usepackage{fontspec}
\usepackage{listings}
\newfontfamily\listingsfont[Scale=.7]{Menlo}

\lstset{frame=single}

\begin{document}

\begin{lstlisting}[basicstyle=\listingsfont]
/* Hello World program */
#include<stdio.h>
main()
{
    printf("Hello World");
}
\end{lstlisting}

\begin{lstlisting}[basicstyle=\linespread{0.8}\listingsfont]
/* Hello World program */
#include<stdio.h>
main()
{
    printf("Hello World");
}
\end{lstlisting}

\end{document}