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

just a picture describing the situation

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?

11

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}
\lstset{basicstyle=\linespread{1.1}\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}

enter image description here

4
  • 1
    This doesn't work for me. It only increases the height of each line :/ – Zorobay May 19 '20 at 13:28
  • @Zorobay Not sure what you mean. I ckecked with a current version of LaTeX and it works exactly the same. – egreg May 19 '20 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. – Zorobay May 19 '20 at 14:57
  • @Zorobay Ask a new question with the details. However, the background is better set with the help of tcolorbox. – egreg May 19 '20 at 14:59
11

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}

Result

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}
\lstset{basicstyle=\ttfamily\linespread{1.15}\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}

Result

3
  • Thanks a lot, but is there a solution that does not involve editing every line of the code? – Phil-ZXX Aug 21 '13 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. – Heiko Oberdiek Aug 21 '13 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! – Charles Carmichael Jun 5 '19 at 12:51
9

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}
\lstset{language=c++,basicstyle=\linespread{1.1}\ttfamily\footnotesize,
    xleftmargin=0.0cm, frame=t, framesep=0.15cm, framerule=0pt, tabsize=4,
    showspaces=false, showstringspaces=false,showlines=true,
    commentstyle=\ttfamily\footnotesize\color{gray},
}

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.