2

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

\documentclass[12pt]{article}

\usepackage{pgffor}
\usepackage{tikz,amsmath}

\begin{document}

\definecolor{dkgreen}{rgb}{0,0.6,0}

\def\var{}
\newenvironment{environment}[1]
{
    \foreach \i/\j in {#1}
    {
        \xdef\var{\var \color{dkgreen}\i\color{black} \j}
    }
    \var
    \linebreak
}

\begin{environment}{a/b, c/d}
\end{environment}       
\end{document}
  • 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
3

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

\def\japivar{}
\newenvironment{myenvironment}[3]
{%
    {\let\textcolor\relax
      \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.