[Tex/LaTex] Highlight code in lstlisting

colorboxhighlightinglistings

I would like to highlight code in a listlisting environment. I tried two techniques: using \usepackage{lstlinebgrd} and using \usepackage{color} (based on this conversation) but none really match what I want.
If a line of code is too long to fit on one line,
– colorbox will not respect the line break
– lstlinebgrd will respect the line break but won't highlight the second line (see example)

In top of that, colorbox creates white spaces in the frame of the lstlisting.

Would you know a solution that would work even with long lines in lstlisting?

enter image description here

MWE (it's a bit long as I want to show all the lstlisting options I use on my main doc):

\documentclass{article}
\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=!]
dummycode
!\colorbox{blue!35}{dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-}!
\end{lstlisting}

second example
\begin{lstlisting}[ 
    linebackgroundcolor={%
    \ifnum\value{lstnumber}=2
            \color{green!35}
    \fi
    }
    ]
dummycode
dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode
\end{lstlisting}
\end{document}

Best Answer

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: enter image description here

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: enter image description here

Related Question