I prefer to go for simpler designs, both on the web as well as for printed works. Too much color confuses the reader and to paraphrase Bringhurst it is an intrusion
.
rather highligh the point of discussion than the whole code. Code must read like a paragraph! Also I prefer generally environments rather than macros on their own and so here is my minimal!
\gdef\emphasis#1{\lstset{emph={write,void,writeln,#1},
emphstyle={\itshape\ttfamily\textcolor{blue}}}}
\lstnewenvironment{teXXX}[1][]
{\lstset{language=[LaTeX]TeX}\lstset{%
breaklines=true,
framesep=5pt,
basicstyle=\normalsize\ttfamily,
showstringspaces=false,
keywordstyle=\itshape,
stringstyle=\color{maroon},
commentstyle=\color{black},
rulecolor=\color{Gray},
xleftmargin=0pt,
xrightmargin=5pt,
aboveskip=\medskipamount,
belowskip=\medskipamount,
backgroundcolor=\color{LightGray!5}, #1
}}
{}
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}
\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}
Best Answer
The background color and the frame are drawn for every line separately.
listings
assumes a certain line height and such graphic errors appear if the real height is larger than that. From your image I would guess you are using a larger font for the line numbers than for the normal text. You should setnumberstyle
to the same or smaller size than the normal text.In my test I get the same white lines if the
numberstyle
is larger than thebasicstyle
(left) but not if it is smaller (right) or the same size.