我计划为我的软件包编写新的文档,特别是这次我希望改进代码显示。
下面是我目前用于代码突出显示的代码。要突出显示命令,必须将命令名称添加到列表中moretexcs
。这不是很方便,更重要的是,它似乎不能很好地与_
命令名称中包含 expl3 命令配合使用。
我的问题是,应该如何配置才能使传统的 LaTeX2e 命令和新expl3
命令具有不同的颜色?(最好是自动的,并且不依赖于像这样的列表moretexcs
)
提前感谢您的任何善意建议!
\documentclass{article}
\usepackage{setspace}
\usepackage{xcolor}
\usepackage{listings}
\definecolor{maintheme}{RGB}{70,130,180}
\definecolor{forestgreen}{RGB}{21,122,81}
\definecolor{lightergray}{gray}{0.99}
\lstset{language=[LaTeX]TeX,
keywordstyle=\color{maintheme},
basicstyle=\ttfamily,
commentstyle=\color{forestgreen}\ttfamily,
stringstyle=\rmfamily,
showstringspaces=false,
breaklines=true,
frame=lines,
backgroundcolor=\color{lightergray},
flexiblecolumns=true,
escapeinside={(*}{*)},
% numbers=left,
numberstyle=\scriptsize, stepnumber=1, numbersep=5pt,
% firstnumber=last,
}
\providecommand{\meta}[1]{$\langle${\normalfont\itshape#1}$\rangle$}
\lstset{moretexcs=%
{part,parttext,chapter,section,subsection,subsubsection,frontmatter,mainmatter,backmatter,tableofcontents,href,
}
}
\lstnewenvironment{code}%
{\setstretch{1.07}%
\setkeys{lst}{columns=fullflexible,keepspaces=true}%
}{}
\lstnewenvironment{code*}%
{\setstretch{1.07}%
\setkeys{lst}{numbers=left,columns=fullflexible,keepspaces=true}%
}{}
\newcommand{\packageoption}[1]{\texttt{\textcolor{black!67!green}{#1}}}
\newcommand{\commandoption}[1]{\texttt{\textcolor{black!67!cyan}{#1}}}
\begin{document}
\begin{code}
\documentclass[(*\packageoption{options}*)]{class}
\end{code}
\lstinline|\traditionalcommand|
\begin{code}
\mymodule_some_command:nn {} {}
\end{code}
\lstinline|\expl_command:|
\end{document}
答案1
listings
在查阅了一段时间的文档后,我找到了以下解决方案。仍然需要手动将命令名称添加到moretexcs
,但可以分别设置 LaTeX2 和 LaTeX3 命令的颜色。
关键是使用classoffset
,而对于 LaTeX3 命令,需要alsoletter = {_,:}
。
\documentclass{article}
\usepackage{setspace}
\usepackage{xcolor}
\usepackage{listings}
\definecolor{code-main}{RGB}{70,130,180}
\definecolor{code-expl3}{RGB}{240,50,100}
\definecolor{code-comment}{RGB}{21,122,81}
\definecolor{code-background}{gray}{0.99}
\lstset{
language = [LaTeX]TeX,
basicstyle = \ttfamily,
keywordstyle = \color{code-main},
commentstyle = \color{code-comment},
showstringspaces = false,
breaklines = true,
frame = lines,
backgroundcolor = \color{code-background},
flexiblecolumns = true,
escapeinside = {(*}{*)},
alsoletter = {_,:},
% numbers = left,
% firstnumber = last,
numberstyle = \scriptsize\ttfamily,
stepnumber = 1,
numbersep = 5pt,
}
\newcommand{\meta}[1]{$\langle${\normalfont\itshape#1}$\rangle$}
\lstset{% LaTeX2 commands
classoffset = 0,
texcsstyle =* \color{code-main},
moretexcs =
{
part,parttext,chapter,section,subsection,subsubsection,frontmatter,mainmatter,backmatter,tableofcontents,href,
traditionalcommand
}
}
\lstset{% LaTeX3 commands
classoffset = 1,
texcsstyle =* \color{code-expl3},
moretexcs =
{
mymodule_some_command:nn, expl_command:
}
}
\lstnewenvironment{code}{\setstretch{1.07}}{}
\lstnewenvironment{code*}{\setstretch{1.07}\lstset{numbers=left}}{}
\newcommand{\packageoption}[1]{\texttt{\textcolor{black!67!green}{#1}}}
\newcommand{\commandoption}[1]{\texttt{\textcolor{black!67!cyan}{#1}}}
\begin{document}
\begin{code}
\documentclass[(*\packageoption{options}*)]{class}
\end{code}
\lstinline|\traditionalcommand|
\begin{code}
\mymodule_some_command:nn {} {}
\end{code}
\lstinline|\expl_command:|
\end{document}