5

In my Verbatim I need to specify colour for my code.

In this answer (fancyvrb alternate commandchars and \textcolor) the solution is to put this code in the preamble:

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

and then use

\begin{Verbatim}[commandchars=&\[\]]
&fvtextcolor[blue][function]
\end{Verbatim}

My problem is the commandchars. \textcolor needs a pair of braces, but in my code I use all the pair of braces that is: () [] {}.

So is it possible to specify another pair of commandchars? Something like &fvtextcolor'blue''function' or &fvtextcolor"blue""function"?

  • Yes, but you need two different characters: [commandchars=\&\`\'] would use the backtick for the opening brace and the apostrophe for the closing brace. Any unused character is good. – egreg May 19 '14 at 12:06
  • So this should work &fvtextcolorred'test' ? – Karsten Jancker May 19 '14 at 12:08
  • Sorry, almost any unused character. :(; I tried with " and ! and it seems to work. – egreg May 19 '14 at 12:15
  • Thank you so much, it worked! Can you post it as an answer, so I can mark the question as solved? – Karsten Jancker May 19 '14 at 12:17
6

In theory, any unused characters would do, but unfortunately for certain characters this conflicts with other parts of verbatim typesetting.

I've found out that

[commandchars=\&\"\!]

gives the expected result.

The characters that are likely not to work are

` < > , ' -

If you really need some of them, a workaround can be devised.

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.