\documentclass{article}
\usepackage{listings}
\usepackage{color}
\begin{document}
\definecolor{light-gray}{gray}{0.95}
\lstset{basicstyle=\ttfamily\footnotesize,
backgroundcolor=\color{light-gray}, xleftmargin=0.7cm,
frame=tlbr, framesep=0.2cm, framerule=0pt,
}
\begin{lstlisting}[mathescape]
k = $\lfloor (10\log n \cdot \log\log n)^{1/3} \rfloor$;
k = $\lfloor (4\log n \cdot \log n)^{1/3} \rfloor$;
\end{lstlisting}
\end{document}
produces
An answer to a similar question said that "this line is just an artifact of the viewer". But this line appears in two viewers and various zoom steps. On top of that, one can clearly see that the 1/3
overlaps with the white line. So I do not accept it as a mere artifact anymore.
How could this problem be approached? Is it possible to somehow put another grey layer underneath the lstlisting environment which would "eat up" any white lines?
edit: Is there a solution that would not involve editing every line of the code?
Best Answer
This question is a duplicate of "listings with background color and mathescape broken". The answers there explain the reason. In short:
The background is drawn by putting a colored rule at the start of each line (hook
EveryLine
). Then the line is typeset. Thus it packagelistings
does not know the maximum height and depth of the line. Everything beyond the height and depth of the strut box remains white.Workaround
\smash
\smash
creates a box, where the height and depth are zero:Workaround via increasing the line spacing
If you fear, the lines are too narrow and might touch, then a larger line spacing can be used. Setting of
\baselineskip
inbasicstyle
does not have an effect, but\fontsize
or\linespread
with explicit/implicit\selectfont
work. The following example uses\linespread
(see egreg's answer).\footnotesize
contains the implicit\selectfont
: