3

I am trying to highlight a specific line in a \lstinputlisting environment that already has a background color. The approach that I tried removes my original background color and just highlights the specific line.

enter image description here

How to go about keeping the original background color and just highlight the specific line?

Here is what I have thus far:

\documentclass{book}

\usepackage{listings}
\usepackage{lstlinebgrd}
\usepackage{xcolor}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{filecontents}

\begin{filecontents*}{\jobname.m}
 % detect if next step is an obstacle
                if bug.isoccupied(robot + [dx; dy])
                    bug.message('(%d,%d) obstacle!', n);
                    bug.H(bug.j,:) = robot; % define hit point
                    bug.step = 2;
                    % get a list of all the points around the obstacle
                    bug.edge = edgelist(bug.occgridnav == 0, robot);
                    %bug.k = 2;  % skip the first edge point, we are already there
                    bug.k = size(bug.edge,2)
                else
                    n = robot + [dx; dy];
                end
            end % step 1
\end{filecontents*}


\lstdefinestyle{mystyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
%language=Matlab,
style=Matlab-editor,
basicstyle=\mlttfamily\scriptsize,
%numbersep=22pt,
backgroundcolor=\color{blue!10}
}


\begin{document}


\lstinputlisting[firstnumber = 236,
style=mystyle,
linebackgroundcolor={\ifnum\value{lstnumber}=244\color{green}\fi},
caption={[First Change to the Bug2 Algorithm]{First Change to the Bug2 Algorithm}},
label = mat:line246]{\jobname.m}

\end{document} 
0

1 Answer 1

4

It seems to work if you add the background color also to the line color. An alternative is to patch the internal command to add the background color:

\documentclass{book}

\usepackage{listings}
\usepackage{lstlinebgrd}
\usepackage{xcolor}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{filecontents}

\begin{filecontents*}{\jobname.m}
 % detect if next step is an obstacle
                if bug.isoccupied(robot + [dx; dy])
                    bug.message('(%d,%d) obstacle!', n);
                    bug.H(bug.j,:) = robot; % define hit point
                    bug.step = 2;
                    % get a list of all the points around the obstacle
                    bug.edge = edgelist(bug.occgridnav == 0, robot);
                    %bug.k = 2;  % skip the first edge point, we are already there
                    bug.k = size(bug.edge,2)
                else
                    n = robot + [dx; dy];
                end
            end % step 1
\end{filecontents*}


\lstdefinestyle{mystyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
%language=Matlab,
style=Matlab-editor,
basicstyle=\mlttfamily\scriptsize,
%numbersep=22pt,
backgroundcolor=\color{blue!10}
}
\usepackage{xpatch}
\makeatletter
%alternative: patch \lst@bkgcolor 
%\xpatchcmd\lst@linebgrd{\color{-.}}{\lst@bkgcolor}{}{\fail}
\makeatother

\begin{document}


\lstinputlisting[firstnumber = 236,
style=mystyle,
%add background color to all lines:
linebackgroundcolor={\color{blue!10}\ifnum\value{lstnumber}=244\color{green}\fi},
caption={[First Change to the Bug2 Algorithm]{First Change to the Bug2 Algorithm}},
label = mat:line246]{\jobname.m}

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