How can I reduce the line spacing in a listing?
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}
\begin{lstlisting}[basicstyle=\linespread{0.8}\listingsfont]
/* Hello World program */
#include<stdio.h>
main()
{
printf("Hello World");
}
\end{lstlisting}
\end{document}
To reduce the line spacing for all lstlistings, just set
basicstyle=\linespread{0.8}
in your \lstset{}
configuration. No need to use fontspec
.