# Broken line between line numbers and code in lstinputlisting with setstretch<1

If I use \setspace{0.8} to tighten up my listings* I get a broken line between the line numbers and the code.

\documentclass{article}
\usepackage{listings}
\usepackage{setspace}
\lstset{basicstyle=\footnotesize\ttfamily\selectfont,  breakatwhitespace=false, breakindent=0.5em, breaklines=true, captionpos=b, extendedchars=false, frame=l, keepspaces=true} %this line intentionally long

%and some intentional blanks.
\lstset{
numbers=left,
numbersep=5pt,
numberstyle=\tiny\ttfamily\selectfont,
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=2,
tabsize=2,
}
\begin{document}
This is a singlespaced listing:
\lstinputlisting[frame=leftline,firstline=1,lastline=9]{lstline.tex}

This is after \textbackslash setstretch\{0.8\}:
\setstretch{0.8}
\lstinputlisting[frame=leftline,firstline=1,lastline=9]{lstline.tex}
\end{document}


(save it as lstline.tex and it's its own example)

If the breaking was regular I'd just accept it as a dashed line and move on, but it's not (see the very long, multiply wrapping line 3 for example).

I've played around with a few things while stripping down to an MWE but nothing helped.

Note that these are multi-page listings, so a box round them looks odd, the rule is really only to separate the line numbers from the code.

EDIT I compile with pdflatex (I think I have to).

*(the line spacing looks pretty big in Latin Modern Typewriter Light Condensed which I normally use, but the issue isn't font-dependent)

• The left bar is formed by adding rules as high as a strut, but, with your setting of the baselineskip, some characters are higher than a strut. Mar 31, 2014 at 16:03
• @egreg so I guess I just have to keep increasing the spacing until it looks OK, to hit the minimum spacing that will work? Mar 31, 2014 at 16:10
• Basically so, unless you're willing to change how the frame is drawn. Mar 31, 2014 at 16:11
• @Jubobs it looks to me like a small nonzero positive value works by overriding the \setstretch - the 2 rules in the above example render to the same length within 1mm. A negative value is good though. Mar 31, 2014 at 16:26
• @Jubobs... bizarrely lineskip=-.1\baselineskip or even -0.15 works a treat, but at -0.18 the gaps reappear. The leading is also pretty bad by this point so -0.1 looks like the best option - at least in cm tt, I could go a bit tighter in lmodern tt condensed I think. Mar 31, 2014 at 16:40

The left frame is drawn by pieces of rules as high as a strut, but with \baselinestretch set to 0.8 some characters are higher than a strut.

You could solve by changing the way the rules are computed, for instance with

\documentclass{article}
\usepackage{listings}
\usepackage{setspace}
\lstset{basicstyle=\footnotesize\ttfamily\selectfont,  breakatwhitespace=false, breakindent=0.5em, breaklines=true, captionpos=b, extendedchars=false, frame=l, keepspaces=true} %this line intentionally long

\usepackage{etoolbox}
\makeatletter
\patchcmd{\lst@frameInit}
{\vrule\@width\lst@framerulewidth}
{\smash{\vrule\@width\lst@framerulewidth\@height1.2\ht\strutbox\@depth1.2\dp\strutbox}}
{}{}
\makeatother

%and some intentional blanks.
\lstset{
numbers=left,
numbersep=5pt,
numberstyle=\tiny\ttfamily\selectfont,
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=2,
tabsize=2,
}
\begin{document}
This is a singlespaced listing:
\lstinputlisting[frame=leftline,firstline=1,lastline=9]{\jobname}

This is after \verb|\setstretch{0.8}|:
\lstinputlisting[frame=leftline,firstline=1,lastline=9]{\jobname}
\end{document}


However the leading is uneven. I don't like loading setspace, so I changed the setting to an equivalent one.

I think it's better to just reducing the leading at most so that the strut is still a bit higher than characters, say 0.9:

\documentclass{article}
\usepackage{listings}
\usepackage{setspace}
\lstset{basicstyle=\footnotesize\ttfamily\selectfont,  breakatwhitespace=false, breakindent=0.5em, breaklines=true, captionpos=b, extendedchars=false, frame=l, keepspaces=true} %this line intentionally long

%and some intentional blanks.
\lstset{
numbers=left,
numbersep=5pt,
numberstyle=\tiny\ttfamily\selectfont,
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=2,
tabsize=2,
}
\begin{document}
This is a singlespaced listing:
\lstinputlisting[frame=leftline,firstline=1,lastline=9]{\jobname}

This is after \verb|\setstretch{0.9}|:

• I've been playing with values and even at 0.9 a "g", a comma or a bracket above a bracket or a slash leads to a tiny notch if I zoom in, even though there's now quite a gap between the bottom of the descender and the top of the next line (in lmodern tt condensed) so your new method (or @Jubobs' small negative lineskip for some reason) does the trick. I think 0.9 and your recalculated rule might be required - but maybe that's font-specific. Mar 31, 2014 at 16:33