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}
I modified your MWE into this:
\documentclass{article}
\def\cccolorbox#1#2{\ifx#2\relax\let\next\allowbreak\else
\def\next{\colorbox{#1}{#2}\allowbreak\cccolorbox{#1}}\fi\next}
\def\ccolorbox#1#2{\fboxsep0pt\cccolorbox{#1}#2\relax}
\def\!#1{\ifx#1\ccolorbox\allowbreak\expandafter\ccolorbox\else
\ifx#1\end\expandafter\expandafter\expandafter\end\else
#1\allowbreak\expandafter\expandafter\expandafter\!\fi\fi}
\def\cccolorbox#1#2{\ifx#2\relax\let\next\allowbreak\else
\def\next{\colorbox{#1}{\strut #2}\allowbreak\cccolorbox{#1}}\fi\next}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
\usepackage{lstlinebgrd}
\usepackage{listings}
\usepackage{lstlinebgrd}
\lstset{columns=fullflexible,
basicstyle=\small,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{ForestGreen},
frame=single,
extendedchars=true,
keywordstyle=\color{blue},
language=Python,
numbers=left,
numbersep=5pt,
numberstyle=\scriptsize\color{gray},
rulecolor=\color{black},
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
stringstyle=\color{purple},
tabsize=2,
title=\lstname,
belowskip=-1em,
aboveskip=1em,
belowcaptionskip=0em,
}
\begin{document}
First example
\begin{lstlisting}[escapechar=!]
! \ccolorbox{blue!35}{dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-} !
\end{lstlisting}
second example
\begin{lstlisting}[escapechar=!]
dummycode
!\colorbox{blue!35}{\parbox{0.95\textwidth}{dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-}}!
\end{lstlisting}
\end{document}
The first example is based on this discussion:
Listing with background color not line breaking correctly
But the results looks ugly to me. Alternatively, in the second example I used a \parbox inside the \colorbox. It looks better but there is no indentation and the external frame it's still fragmented.
Here is what I obtain:
EDIT:
by commenting the line:
frame=single,
and using the framed environment defined in the framed package you could fix the fragmented box issue. Morever using hangpars environment you could indent correctly inside the parbox.
Here is the updated example:
\documentclass{article}
\def\cccolorbox#1#2{\ifx#2\relax\let\next\allowbreak\else
\def\next{\colorbox{#1}{#2}\allowbreak\cccolorbox{#1}}\fi\next}
\def\ccolorbox#1#2{\fboxsep0pt\cccolorbox{#1}#2\relax}
\def\!#1{\ifx#1\ccolorbox\allowbreak\expandafter\ccolorbox\else
\ifx#1\end\expandafter\expandafter\expandafter\end\else
#1\allowbreak\expandafter\expandafter\expandafter\!\fi\fi}
\def\cccolorbox#1#2{\ifx#2\relax\let\next\allowbreak\else
\def\next{\colorbox{#1}{\strut #2}\allowbreak\cccolorbox{#1}}\fi\next}
\usepackage{framed}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
\usepackage{lstlinebgrd}
\usepackage{listings}
\usepackage{lstlinebgrd}
\usepackage{hanging}
\lstset{columns=fullflexible,
basicstyle=\small,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{ForestGreen},
% frame=single,
extendedchars=true,
keywordstyle=\color{blue},
language=Python,
numbers=left,
numbersep=5pt,
numberstyle=\scriptsize\color{gray},
rulecolor=\color{black},
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
stringstyle=\color{purple},
tabsize=2,
title=\lstname,
belowskip=-1em,
aboveskip=1em,
belowcaptionskip=0em,
}
\begin{document}
First example
\begin{framed}
\begin{lstlisting}[escapechar=!]
! \ccolorbox{blue!35}{dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-} !
\end{lstlisting}
\end{framed}
second example
\begin{framed}
\begin{lstlisting}[escapechar=!]
dummycode
!\colorbox{blue!35}{\parbox{0.95\textwidth}{\begin{hangparas}{.25in}{1}dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-\end{hangparas}}}!
\end{lstlisting}
\end{framed}
\end{document}
which give me this results:
Best Answer
To draw such a frame you should use an other package which allows such modifications. The default packages are
mdframed
ortcolorbox
. In the example below I provided a solution usingtcolorbox
whereby a new listings environmentlstmcode
is defined. Please note that I am using the version 2.22 for the example below.Here an other approach using
mdframed
. The output is equal: