0

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

1 Answer 1

3

Is this the effect you want? enter image description here

\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.

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

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.