6

When I try this:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{xcolor}
\begin{document}
\begin{Verbatim}[commandchars=\\\{\}]
\textcolor[RGB]{255,0,0}{test}
\end{Verbatim}
\end{document}

... I get (instead of a red "test") an unintelligible \unhbox\voidb@x\kern,\z@,\char,‘\protect,\relax,\kern,.16667em,0\unhbox,\voidb@x,\kern,\z@,\...

I suspect this has something to do with "active" characters ...

What do I have to do to make this work?

Ideally, this should be done by only changing things inside the Verbatim environment, because in my use case, the environment is provided by an external template that's out of my control.

6

Yes, you're right: the comma is made active in order to break ligatures (,, is a ligature defined in the T1 encoded fonts).

Since this is done as part of the initialization of verbatim environments, it cannot be undone with codes={\catcode`,=12 }, but needs a different treatment (see Check for special character in verbatim environment)

Since it's not desirable to globally redefine \verbatim@nolig@list, here's a “local” solution.

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{xcolor}

\makeatletter
\newenvironment{Cverbatim}% C for "commands"
  {\VerbatimEnvironment
   \def\verbatim@nolig@list{\do\`\do\<\do\>\do\'\do\-}% no comma
   \fvset{commandchars=\\\{\}}%
   \begin{Verbatim}}
  {\end{Verbatim}}
\makeatother

\begin{document}
\begin{Cverbatim}
\textcolor[RGB]{255,0,0}{test}
\end{Cverbatim}
\end{document}

However, if you don't plan needing to break the ,, ligature in your verbatim texts, just

\makeatletter
\def\verbatim@nolig@list{\do\`\do\<\do\>\do\'\do\-}% no comma
\makeatother

in the preamble will do.

If you have control on how the verbatim text is written by your auxiliary program, you can do with

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{xcolor}
\usepackage{xparse}

\NewDocumentCommand{\verbatimtextcolor}{o>{\SplitArgument{2}{/}}mm}{%
  \makeverbatimtextcolor#2{#3}%
}
\NewDocumentCommand{\makeverbatimtextcolor}{mmmm}{%
  \textcolor[RGB]{#1,#2,#3}{#4}%
}

\begin{document}

\begin{Verbatim}[commandchars=\\\{\}]
\verbatimtextcolor{255/0/0}{test}
\end{Verbatim}
\end{document}

It would also be possible to overload \textcolor, but an independent macro seems better.

It is actually possible to do this without external code, provided you use very clumsy input:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{xcolor}

\begin{document}

\begin{Verbatim}[commandchars=\\\{\}]
\def\tcRGB{\textcolor[RGB]}\expandafter\tcRGB\expandafter{\detokenize{255,0,0}}{test}
\end{Verbatim}
\end{document}
  • Thanks, that works great! But is there also a way to make this work by changing only things inside the Verbatim environment? In other words, is there a way to write \textcolor[RGB]{255,0,0}{test} without the commas? – Matthias Feb 4 '16 at 15:15
  • @Matthias I'm not sure I understand; however, defining a color name is much easier. – egreg Feb 4 '16 at 15:18
  • Yes, I know, but in my use case I'm auto-generating LaTeX code with a Jinja2 template and I want to generate arbitrarily many colors from the 24-bit range. Also, I have only control about the LaTeX code within the Verbatim environment, that's why I'm asking this strange question ... I don't care how ugly or complicated the result is, since this is auto-generated anyway. – Matthias Feb 4 '16 at 15:24
  • @Matthias I added a way with a different macro that converts a specification A/B/C into A,B,C for passing it to \textcolor. – egreg Feb 4 '16 at 15:37
  • Thanks a lot, but I still have to make changes outside of the Verbatim environment in this case. I could also simply define such a command \newcommand{\textcolorRGB}[3]{\textcolor[RGB]{#1,#2,#3}} and use \textcolorRGB{255}{0}{0}{test}. Still, it would be much better for my use case if I could do that without changes outside of the Verbatim environment. Is there a way do make a comma non-active temporarily? Or some other way to write a non-active comma? Or probably something that's specific to the \textcolor macro (and \colorbox)? – Matthias Feb 4 '16 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.