programming add tag
nsajko
I want to color some parts of an equation, but I'm running into issues.

Reproducer:
```
\documentclass{scrartcl}
\usepackage{mathtools}
\usepackage[svgnames]{xcolor}

\ExplSyntaxOn

\NewDocumentCommand{\quotientColor}{m}{
  \str_case:nnF {#1}
    {
      {0}{Tomato}
      {1}{SpringGreen}
      {2}{Fuchsia}}
    {\msg_error:nn {just-my-document} {unknown-value-given-as-input}}}

\NewDocumentCommand{\cq}{m m}{
  \mathcolor{\quotientColor{#1}}{#2}}

\ExplSyntaxOff

\begin{document}
  \( \mathcolor{Fuchsia}{27} \)

  \( \cq{1}{34} \)
\end{document}
```

Using `\mathcolor` directly works fine, but using `\cq` fails with
```
Package xcolor Error: Undefined color `\quotientColor {1}'
```

So the command `\quotientColor` isn't being evaluated.

I know that some relevant terms here are "protected" and "expandable", but don't know what should I do now.

EDIT: a workaround is to define `\cq` like so:
```
\NewDocumentCommand{\cq}{m m}{
  \mathcolor{
    \str_case:nnF{#1}
      {
        {0}{Tomato}
        {1}{SpringGreen}
        {2}{Fuchsia}}
      {\msg_error:nn {just-my-document} {unknown-value-given-as-input}}}{
    #2}}
```

This is an OK fix for me in this case, but I hope a real solution exists, so that I could use commands within commands, instead of having one huge, unmaintainable, command.
Top Answer
AnonymousRabbit
There are quite a few things you might've misunderstood:

TeX is a macro expansion language. As long as you don't explicitly expand things yourself no "argument" (parameter) is expanded when another macro is reading it. So your provided your `\quotientColor` could expand to `Tomato` directly and you'd have a macro `\newcommand\test[2]{1 is #1, 2 is #2}` and used this:

```
\NewDocumentCommand \cq {m m}
  { \test { \quotientColor {#1} } {#2} }
```

You'd get the following expansion results:

```
% 0
\cq{0}{34}
% 1
\test{\quotientColor{0}}{34}
% 2
1 is \quotientColor{0}, 2 is 34
```

So `\quotientColor` is not expanded just because `\test` or `\mathcolor` is. However in the case of the colour commands (like `\mathcolor`) they expand their argument in the search for the colour name. If you want to actively expand a macro before the next is expanded you'll have to use `\expandafter` or `\expanded` like so:

```
\expandafter\test\expandafter{\quotientColor{#1}} % only 1 step of expansion, most likely not sufficient
\expandafter\test\expanded{{\quotientColor{#1}}} % full expansion
```

However, your `\quotientColor` is not expandable! For this to work you'll have to define it expandable, which `\NewDocumentCommand` doesn't. Instead use `\NewExpandableDocumentCommand`, also you should recover in case of an error, and you should use an expandable error message. So I'd code your `\quotientColor` in the following way:

```
\documentclass{scrartcl}

\usepackage{mathtools}
\usepackage[svgnames]{xcolor}

\ExplSyntaxOn
\msg_new:nnn { just-my-document } { unknown-value } { Unknown value given: #1 }
\NewExpandableDocumentCommand \quotientColor { m }
  {
    \str_case:nnF {#1}
      {
        { 0 } { Tomato }
        { 1 } { SpringGreen }
        { 2 } { Fuchsia }
      }
      {
        \msg_expandable_error:nnn { just-my-document } { unknown-value } {#1}
        Tomato
      }
  }
\NewDocumentCommand \cq { m m }
  { \mathcolor { \quotientColor {#1} } {#2} }
\ExplSyntaxOff

\begin{document}
\(\cq{1}{34}\)
\end{document}
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.