I am trying to color some text inside an xdef command. I am getting the following error: Use of \@undeclaredcolor doesn't match its definition. Does anybody know how to resolve this? Thanks!

edit: Modified example to be a MWE





    \foreach \i/\j in {#1}
        \xdef\var{\var \color{dkgreen}\i\color{black} \j}

\begin{environment}{a/b, c/d}
  • 1
    Welcome to LaTeX! Welcome to TeX.SX! Just to make sure, have you defined dkblue in your document? If that's not the issue, then you'll need to post a complete MWE so that we can identify the issue. – JP-Ellis Mar 1 '16 at 14:54
  • it is never safe to use \xdef on general latex tokens. Also you are missing % from almost every line – David Carlisle Mar 1 '16 at 15:10

You need to protect \textcolor from expanding, and avoid adding spurious spaces:

      \xdef\japivar{#1 #2(}%
    \foreach \i/\j/\k in {#3}%
        \xdef\japivar{\japivar \textcolor{dkblue}{\i} \j}%

This is assuming that the tokens you pass in as #1,#2,#3 are safe in an \xdef.

| improve this answer | |

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.