我正在尝试在\lstinputlisting
已有背景颜色的环境中突出显示特定行。我尝试的方法删除了原始背景颜色,仅突出显示特定行。
如何保留原始背景颜色和只突出显示特定的行?
以下是我目前所掌握的信息:
\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}
答案1
如果将背景颜色也添加到线条颜色中,它似乎可以工作。另一种方法是修补内部命令以添加背景颜色:
\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}