# Unwanted White Lines in listings environment

\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?

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 package listings 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:

\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 = $\smash{\lfloor (10\log n \cdot \log\log n)^{1/3}\rfloor}$;
k = $\smash{\lfloor (4\log n \cdot \log n)^{1/3} \rfloor}$;
\end{lstlisting}
\end{document}


## 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 in basicstyle 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:

\documentclass{article}
\usepackage{listings}
\usepackage{color}
\begin{document}
\definecolor{light-gray}{gray}{0.95}
backgroundcolor=\color{light-gray}, xleftmargin=0.7cm,
frame=tlbr, framesep=0.2cm, framerule=0pt,
}
\begin{lstlisting}[mathescape]
k = $\smash{\lfloor (10\log n \cdot \log\log n)^{1/3}\rfloor}$;
k = $\smash{\lfloor (4\log n \cdot \log n)^{1/3} \rfloor}$;
\end{lstlisting}
\end{document}


• Thanks a lot, but is there a solution that does not involve editing every line of the code? Aug 21, 2013 at 14:00
• @Tom: In general no. The background of the line is drawn before the line is typeset. Thus everything beyond the height and depth of the strut box remains white. Aug 21, 2013 at 15:28
• @HeikoOberdiek, your solution really helped, thanks! I wanted to ask, is it possible to set \linespread's value of a specific code listing to another number? Generally 1.1 is good, but there are some listings that still create white lines, and for them I want to specifically set to a bigger value so it won't have the white lines. Thanks! Jun 5, 2019 at 12:51

You can increase the baseline skip so the lines won't be too near (to the eyes of TeX):

\documentclass{article}
\usepackage{listings}
\usepackage{color}
\begin{document}
\definecolor{light-gray}{gray}{0.95}
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}


• This doesn't work for me. It only increases the height of each line :/ May 19, 2020 at 13:28
• @Zorobay Not sure what you mean. I ckecked with a current version of LaTeX and it works exactly the same. May 19, 2020 at 13:44
• I think that what this actually does is set the background color the same as the lines. When I use your code but change the 'light-gray' to a darker color, the lines show up again. May 19, 2020 at 14:57
• @Zorobay Ask a new question with the details. However, the background is better set with the help of tcolorbox. May 19, 2020 at 14:59

Here is what I was looking for. By putting a grey layer (via mdframed) underneath the listings environment every white line disappears:

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{listings}
\begin{document}
xleftmargin=0.0cm, frame=t, framesep=0.15cm, framerule=0pt, tabsize=4,
showspaces=false, showstringspaces=false,showlines=true,
}

\begin{mdframed}[backgroundcolor=black!5,leftmargin=0.5cm,hidealllines=true,%
innerleftmargin=0.2cm,innerrightmargin=0.2cm,innertopmargin=-0.72cm,innerbottommargin=-0.10cm]
\begin{lstlisting}[mathescape, caption = This is some code]
k = $\lfloor (10\log n \cdot \log\log n)^{1/3} \rfloor$;    //with caption
k = $\lfloor (4\log n \cdot \log n)^{1/3} \rfloor$;
\end{lstlisting}
\end{mdframed}

\begin{mdframed}[backgroundcolor=black!5,leftmargin=0.5cm,skipabove=0.3cm,hidealllines=true,%
innerleftmargin=0.2cm,innerrightmargin=0.2cm,innertopmargin=-0.0cm,innerbottommargin=-0.10cm]
\begin{lstlisting}[mathescape]
k = $\lfloor (10\log n \cdot \log\log n)^{1/3} \rfloor$;   //without caption
k = $\lfloor (4\log n \cdot \log n)^{1/3} \rfloor$;
\end{lstlisting}
\end{mdframed}

\end{document}