# How can I reduce the line spacing in a listing?

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?

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.

\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}

/* Hello World program */
#include<stdio.h>
main()
{
printf("Hello World");
}
\end{lstlisting}

\end{document}

• Shouldn't this be handled by fontspec the same way it handles color and word spacing, for instance? Sep 8 '14 at 18:25
• @Manuel I don't know. I'm not that familiar with fontspec, to be honest. Don't hesitate to chime in if you think of a more sensible approach :) Sep 8 '14 at 18:27
• It was meant to see an opinion if you thought this should be added as a feature request to fontspec. Sep 8 '14 at 18:52
• @Manuel I don't know fontspec well enough to have an opinion on that, but note that the use of fontspec in my answer is only incidental; I only used it because the OP (presumably) wants to use it. A good solution should be independent of whether fontspec is used or not. Sep 8 '14 at 18:53
• I wasn't even aware of using fontspec because I didn't have to add a \usepackage{fontspec} and everything is working :-\ Sep 8 '14 at 19:03

To reduce the line spacing for all lstlistings, just set

basicstyle=\linespread{0.8}


in your \lstset{} configuration. No need to use fontspec.