27

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?

0

3 Answers 3

28

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}
6
  • Shouldn't this be handled by fontspec the same way it handles color and word spacing, for instance?
    – Manuel
    Commented Sep 8, 2014 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 :)
    – jub0bs
    Commented Sep 8, 2014 at 18:27
  • It was meant to see an opinion if you thought this should be added as a feature request to fontspec.
    – Manuel
    Commented Sep 8, 2014 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.
    – jub0bs
    Commented Sep 8, 2014 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 :-\ Commented Sep 8, 2014 at 19:03
12

To reduce the line spacing for all lstlistings, just set

basicstyle=\linespread{0.8}

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

1
  • Makes no effect (at least in pdflatex)
    – yegor256
    Commented Feb 1 at 15:14
1

This is how it works for me (replace 4.2 with any scale factor you need):

\documentclass{article}
\usepackage{listings}
\begin{document}
\lstset{basicstyle=\linespread{4.2}\selectfont}
\begin{lstlisting}
Hello, world!
Second line.
\end{lstlisting}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .