我需要能够在listing
环境中突出显示添加的代码行。请考虑以下示例:
Let's create variables:
\begin{lstlisting}
let msg = "Hello";
let name = "Jody";
\end{lstlisting}
And then add the method for printing:
\begin{lstlisting}
let msg = "Hello";
let name = "Jody";
// My first hack
console.log(msg + " " + name);
\end{lstlisting}
在第二部分中,我想用灰色表示两个变量,用黑色表示新添加的行。
重要的 应保留根据我使用的语言所采用的关键字格式。
几点说明
我已经看到很多方法可以实现单行代码。我需要经常使用它,有没有办法封装一个代码块并设置不同的颜色,而不是针对每行代码执行操作?我得到的建议是使用:
\lstset{escapeinside={(*@}{@*)}}
和:
(*@\textcolor{black}{ <Different color code in here> }@*)
但这种方法是有效的每行和删除语言格式在里面\textcolor
(如果我输入:)(*@\textcolor{black}{ function my() \{ \} }@*)
,关键字function
将不会以粗体显示。
答案1
编辑:此处描述了更简单、更强大的方法。原始答案如下。
您可以将颜色设置为自定义命令,而commentstyle=\mycommentcolor
不是直接使用commentstyle=\color{colorname}
,然后您可以在列表期间使用切换命令的定义escapeinside
。
梅威瑟:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\begin{document}
\def\setallcolors{%
\gdef\mybasiccolor{\color{black}}%
\gdef\mykwcolor{\color{blue}}%
\gdef\myndkwcolor{\color{darkgray}}%
\gdef\myidcolor{\color{black}}%
\gdef\mycommentcolor{\color{purple}}%
\gdef\mystringcolor{\color{red}}%
}
\def\setallgray{%
\gdef\mybasiccolor{\color{gray}}%
\gdef\mykwcolor{\color{gray}}%
\gdef\myndkwcolor{\color{gray}}%
\gdef\myidcolor{\color{gray}}%
\gdef\mycommentcolor{\color{gray}}%
\gdef\mystringcolor{\color{gray}}%
}
\setallcolors
\lstdefinelanguage{JavaScript}{
basicstyle=\mybasiccolor,
keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, let, if, in, while, do, else, case, break},
keywordstyle=\mykwcolor\bfseries,
ndkeywords={class, export, boolean, throw, implements, import, this},
ndkeywordstyle=\myndkwcolor\bfseries,
identifierstyle=\myidcolor,
sensitive=false,
comment=[l]{//},
morecomment=[s]{/*}{*/},
commentstyle=\mycommentcolor\ttfamily,
stringstyle=\mystringcolor\ttfamily,
morestring=[b]',
morestring=[b]"
}
\lstset{numbers=left,escapeinside={(*@}{@*)}}
Let's create variables:
\begin{lstlisting}[language=JavaScript,name=Variables]
let msg = "Hello";
let name = "Jody";
\end{lstlisting}
And then add the method for printing:
\begin{lstlisting}[language=JavaScript,name=Printing]
(*@\setallgray@*)let msg = "Hello";
let name = "Jody";
(*@\setallcolors@*)// My first hack
console.log(msg + " " + name);
\end{lstlisting}
\end{document}
结果与下面的原始答案相同。
原始答案:
一种可能的方法是定义两种语言,一种是彩色或黑色,一种是灰色,并在这两种语言之间切换。需要一些技巧才能使它们显示为一个列表,例如设置aboveskip
和belowskip
(如果需要)并恢复行号。语言定义改编自列表中支持的语言选项. 或者你可以尝试类似使用 tikzmark 自动对列表进行背景着色或者http://www.texample.net/tikz/examples/tikz-listings/。
梅威瑟:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\begin{document}
\lstdefinelanguage{JavaScript}{
keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, let, if, in, while, do, else, case, break},
keywordstyle=\color{blue}\bfseries,
ndkeywords={class, export, boolean, throw, implements, import, this},
ndkeywordstyle=\color{darkgray}\bfseries,
identifierstyle=\color{black},
sensitive=false,
comment=[l]{//},
morecomment=[s]{/*}{*/},
commentstyle=\color{purple}\ttfamily,
stringstyle=\color{red}\ttfamily,
morestring=[b]',
morestring=[b]"
}
\lstdefinelanguage{JavaScriptGray}{
basicstyle=\color{gray},
keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, let, if, in, while, do, else, case, break},
keywordstyle=\color{gray}\bfseries,
ndkeywords={class, export, boolean, throw, implements, import, this},
ndkeywordstyle=\color{gray}\bfseries,
identifierstyle=\color{gray},
sensitive=false,
comment=[l]{//},
morecomment=[s]{/*}{*/},
commentstyle=\color{gray}\ttfamily,
stringstyle=\color{gray}\ttfamily,
morestring=[b]',
morestring=[b]",
belowskip=-5pt
}
\lstset{numbers=left}
Let's create variables:
\begin{lstlisting}[language=JavaScript]
let msg = "Hello";
let name = "Jody";
\end{lstlisting}
And then add the method for printing:
\begin{lstlisting}[language=JavaScriptGray]
let msg = "Hello";
let name = "Jody";
\end{lstlisting}
\begin{lstlisting}[language=JavaScript,firstnumber=last]
// My first hack
console.log(msg + " " + name);
\end{lstlisting}
\end{document}
结果: