【LaTeX Tips】listings 中高亮定界符内的文本而不高亮定界符本身

背景图里的代码高亮十分漂亮,用 listings 宏包实现的难点在于如何高亮某种定界符内的内容,而定界符本身保留 basicstyle 设定的样式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
delim = [s][\ttfamily\color{orange}]{$}{$}
}
\begin{document}
\begin{lstlisting}
\documentclass{article}
\usepackage{amsmath}
\begin{document}
$E = mc^2$
\end{document}
\end{lstlisting}
\end{document}

效果:

$ 之间的公式随着 $ 本身都变成了橘色。

解决问题的方法,可以参照 David Carlisle 在 TeX.SX 上对这个问题的回答

我们可以定义这样一个 listings 样式:

1
2
3
4
\def\beginlstdelim#1#2#3{
\def\endlstdelim{#2\egroup}%
\ttfamily#1\bgroup\color{#3}\aftergroup\endlstdelim%
}

它有三个参数,第一个参数是定界符的左边,第二个是定界符的右边,第三个是高亮的颜色。我们可以这样使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
\documentclass{article}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{listings}


\def\beginlstdelim#1#2#3{
\def\endlstdelim{#2\egroup}%
\ttfamily#1\bgroup\color{#3}\aftergroup\endlstdelim%
}
\lstset
{
moredelim = **[is][\beginlstdelim{\$}{\$}{orange}]{\$}{\$},
moredelim = **[is][\beginlstdelim{\{}{\}}{ForestGreen}]{\{}{\}},
moredelim = **[is][\beginlstdelim{[}{]}{red}]{[}{]},
}

\begin{document}
\begin{lstlisting}
\documentclass{article}
\usepackage{amsmath}
\begin{document}
$E = mc^2$
{E = mc^2}
[E = mc^2]
\end{document}
\end{lstlisting}
\end{document}

效果是这样的:



输出效果


The code in the background figure is very pretty. In terms of using listings to reproduce this beautiful style, nodus turns out to be how to color contents between two delimiters but not the delimiters themselves (remain basicstyle).

If we have the code following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
delim = [s][\ttfamily\color{orange}]{$}{$}
}
\begin{document}
\begin{lstlisting}
\documentclass{article}
\usepackage{amsmath}
\begin{document}
$E = mc^2$
\end{document}
\end{lstlisting}
\end{document}

we will get:

The equation between $s was colored by orange, so did $s themselves.

Inspired by David Carlisle‘s answer to this question, TeX.SX, Jubobs defined a new listings style:

1
2
3
4
\def\beginlstdelim#1#2#3{
\def\endlstdelim{#2\egroup}%
\ttfamily#1\bgroup\color{#3}\aftergroup\endlstdelim%
}

It has three arguments: the first one should be the left delimiter, the second one should be the right one, and the third one specifying the color. Let’s take a example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
\documentclass{article}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{listings}


\def\beginlstdelim#1#2#3{
\def\endlstdelim{#2\egroup}%
\ttfamily#1\bgroup\color{#3}\aftergroup\endlstdelim%
}
\lstset
{
moredelim = **[is][\beginlstdelim{\$}{\$}{orange}]{\$}{\$},
moredelim = **[is][\beginlstdelim{\{}{\}}{ForestGreen}]{\{}{\}},
moredelim = **[is][\beginlstdelim{[}{]}{red}]{[}{]},
}

\begin{document}
\begin{lstlisting}
\documentclass{article}
\usepackage{amsmath}
\begin{document}
$E = mc^2$
{E = mc^2}
[E = mc^2]
\end{document}
\end{lstlisting}
\end{document}

It’s output:



The Output.


您的鼓励是我写作最大的动力

俗话说,投资效率是最好的投资。 如果您感觉我的文章质量不错,读后收获很大,预计能为您提高 10% 的工作效率,不妨小额捐助我一下,让我有动力继续写出更多好文章。


撰写评论

写了这么多年博客,收到的优秀评论少之又少。在这个属于 SNS 的时代也并不缺少向作者反馈的渠道。因此,如果你希望撰写评论,请发邮件至我的邮箱并注明文章标题,我会挑选对读者有价值的评论附加到文章末尾。