fancyvrb 替代 commandchars 和 \textcolor

fancyvrb 替代 commandchars 和 \textcolor

以下工作:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{color}
\begin{document}

\begin{Verbatim}[commandchars=\\\{\}]
test\textcolor{red}{foo}test
\end{Verbatim}

\end{document}

但以下情况并非如此:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{color}
\begin{document}

\begin{Verbatim}[commandchars=&<>]
test&textcolor<red><foo>test
\end{Verbatim}

\end{document}

我收到此错误信息:

ERROR: Argument of \textcolor has an extra }.

--- TeX said ---
<inserted text> 
                \par 
l.7 test&textcolor<red><foo>test

--- HELP ---
From the .log file...

I've run across a `}' that doesn't seem to match anything.
For example, `\def\a#1{...}' and `\a}' would produce
this error. If you simply proceed now, the `\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.

我尝试了所有使用反斜杠转义命令字符的组合,但都没有成功。如何使用&<>作为命令字符?(或者如果不可能,为什么?)

答案1

问题是,\textcolor它后面需要一个真正的括号,而不仅仅是一个“组打开”标记:它的定义是

\def\textcolor#1#{...}

(这里#1是语法允许的可能的“颜色模型”)。您可以使用另一个根据以下内容定义的命令来执行\textcolor

\newcommand*{\fvtextcolor}[2]{\textcolor{#1}{#2}}

这应该有效:

\begin{Verbatim}[commandchars=&\[\]]
test&fvtextcolor[red][foo]test
\end{Verbatim}

恐怕不行<>:这两个字符不能用作分隔符幻想VRB commandchars(我已经被这个“功能”迷住了)。

相关内容