# Make lstlisting honour baselinestretch

I have to use lstlisting instead of Verbatim in a particular case, because math escape doesn't work properly with the latter. I managed to have both look the same, but lstlisting ignores my baselinestretch settings, so the line spacing is too big:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage[scaled=.73]{beramono}
\fvset{baselinestretch=0.94}
\lstdefinelanguage{scala}{
morekeywords={type}%
}
\lstset{
language=scala,%
% baselinestretch=0.94,% keyword doesn't exist
basicstyle=\ttfamily,%
fancyvrb=true,%
mathescape=true,%
}

\begin{document}

\begin{lstlisting}
type Vertex = ($\mathit{lb}_\mathsf{pre}$: Label, $\mathit{lb}_\mathsf{post}$: Label)
Next Line
Next Line
\end{lstlisting}

\begin{Verbatim}
type Vertex = ($\mathit{lb}_\mathsf{pre}$: Label, $\mathit{lb}_\mathsf{post}$: Label)
Next Line
Next Line
\end{Verbatim}

\end{document}


You can see the different line spacing here:

The difference is not that big, but it makes a huge impact on long listings. What is the best way to apply the base line stretching to lstlisting?

-
basicstyle=\linespread{0.94}\ttfamily –  egreg Mar 16 '13 at 0:39
@egreg Great, that does it, although I found I have to use a value of 0.92 to have visually the same spacing. –  Emit Taste Mar 16 '13 at 0:45
I don't think so. –  egreg Mar 16 '13 at 0:54
You are right, that was I think a screen rendering glitch. I have a full A4 page now, and it seems it's indeed the same factor. –  Emit Taste Mar 16 '13 at 1:01

Use basicstyle=\linespread{0.94}\ttfamily

As the following example shows, the lines are spaced exactly in the same way

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage[scaled=.73]{beramono}
\fvset{baselinestretch=.94}
\lstdefinelanguage{scala}{
morekeywords={type}
}
\lstset{
language=scala,
fancyvrb=true,
mathescape=true,
}

\begin{document}

\begin{lstlisting}
type Vertex = ($\mathit{lb}_\mathsf{pre}$: Label, $\mathit{lb}_\mathsf{post}$: Label)
Next Line
Next Line
\end{lstlisting}

\kern-50pt % back up to see that the lines are equally spaced

\begin{Verbatim}
type Vertex = ($\mathit{lb}_\mathsf{pre}$: Label, $\mathit{lb}_\mathsf{post}$: Label)
Next Line
Next Line
\end{Verbatim}

\end{document}


-