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?

  • 2
    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, 2022 at 13:39
  • What about these answers in this post some goes according to what you want?
    – J Leon V.
    Mar 18, 2022 at 16:21

1 Answer 1


Is this the effect you want? enter image description here

\documentclass[margin = 1cm]{standalone}
  \ifnum#1>10 %
    \ifnum#1>1 %

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

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, 2022 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .