6

The following works:

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

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

\end{document}

but the following does not:

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

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

\end{document}

I get this error message:

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.

I've tried all combinations of backslash-escaping the command chars but no luck. How can I use &, <, and > as the command characters? (Or if it's not possible, why?)

1 Answer 1

11

The problem is that \textcolor expects an actual brace after it, not just a "group open" token: its definition is

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

(here #1 would be the possible "color model" allowed by the syntax). You can do with another command defined in terms of \textcolor:

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

This should work:

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

Not with <>, I'm afraid: these two characters can't be used as delimiters in fancyvrb commandchars (I've already been bitten by this "feature").

7
  • Thanks! Do you know why < and > can't be used as delimiters in fancyvrb? Commented Jul 15, 2011 at 16:38
  • 1
    I've never investigated deeply, but I suspect that some \ifnum or \ifdim is involved.
    – egreg
    Commented Jul 15, 2011 at 18:05
  • what is commandchars in Verbatim?
    – Tiina
    Commented Apr 26, 2018 at 3:04
  • @Tiina Did you look at the documentation of fancyvrb?
    – egreg
    Commented Apr 26, 2018 at 8:23
  • @egreg for tex, usually where do you look for its document? I had bad experience when looked for some other packages. For open source project, usually they have an official site, but I found tex package usually has void official website.
    – Tiina
    Commented Apr 27, 2018 at 6:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .