![定义 moretexcs(列表)](https://linux22.com/image/258974/%E5%AE%9A%E4%B9%89%20moretexcs%EF%BC%88%E5%88%97%E8%A1%A8%EF%BC%89.png)
我想在宏moretexcs
之外定义列表,lstdefinestyle
因为我想从单独的 tex 文件中加载这些定义。但是内部lstset
moretexcs
被忽略了:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{lstStyleLaTeX}{%
,language = [LaTeX]TeX%
% ,moretexcs={abc},
,keywordstyle=\color{red}
}
\lstset{%
language=[LaTeX]TeX,
moretexcs={abc},
}%
\begin{document}
\begin{lstlisting}[style=lstStyleLaTeX]
\abc{content}
\end{lstlisting}
\end{document}
答案1
以下示例定义了宏\addmoretexcs
。可选参数指定方言(默认为common
)。如果语言尚不可用,则加载该语言。然后\lstlang@<language>$<dialect>
通过设置附加列表来扩展内部存储在 中的语言定义moretexcs
:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{lstStyleLaTeX}{%
,language = [LaTeX]TeX%
% ,moretexcs={abc},
,keywordstyle=\color{red}
}
\makeatletter
\newcommand*{\addmoretexcs}[2][common]{%
\lowercase{\@ifundefined{lstlang@tex$#1}}{%
\lstloadlanguages{[#1]TeX}%
}{}%
\lowercase{\expandafter\g@addto@macro\csname lstlang@tex$#1\endcsname}{%
\lstset{moretexcs={#2}}%
}%
}
\makeatother
\addmoretexcs[LaTeX]{abc}
\begin{document}
\begin{lstlisting}[style=lstStyleLaTeX]
\abc{content}
\end{lstlisting}
\end{document}