# Assign a color to a variable name in a newcommand

I am writing a complex diagram using TikZ in which the colors used for certain nodes depend on the value of the input variable. I cannot use the color itself as an input variable as certain things other than a color also change.

I have not written this yet, so cannot provide a MWE, but the idea is:

1. Check the value of an input parameter (say #1) using \ifthenelse.
2. Assign some colors to a set of variable, say boxcolor=red!90!black,flatcolor=blue!30!green, juxtaposecolor=yellow if #1 takes one value, another set of the above colors if #1 takes another value.
3. Draw the diagram, using boxcolor, flatcolor, juxtaposecolor, etc. directly, and colors calculated based on them (boxcolor!40!flatcolor).

How do I do this inside a newcommand? Can color variables be first set in some way, and then used in some common code block that follows?

• It would be useful for you to provide a minimum working example of how you would like to use these color assignments. As to the test, if the comparison is a number, \ifnum#1=<test value>\relax<one set of assignments>\else<other set of assignments>\fi. If #1 is a string, not a number, a different syntax is needed. Mar 17 at 13:39
• What about these answers in this post some goes according to what you want? Mar 18 at 16:21

Is this the effect you want?

\documentclass[margin = 1cm]{standalone}
\usepackage{tikz}
\begin{document}
\def\test#1{%
\ifnum#1>10 %
\colorlet{main}{red}%
\else
\ifnum#1>1 %
\colorlet{main}{blue}%
\else
\colorlet{main}{brown}%
\fi
\fi
}

\begin{tikzpicture}
\foreach \x in{-1,5,12}
{
\test{\x}
\draw[main] (\x,0)circle(2);
}
\end{tikzpicture}
\end{document}


when #1 > 10 , main = red; and 1 < #1 <= 10, main = blue; else, main = brown.

• Indeed. \colorlet is exactly what I was looking for. Mar 18 at 16:49