1

I have defined eight colors (stepcolor0,...,stepcolor7).

Now I could have a list with more than 8 color-bearers.
So I could need something like a function
"\ifcolorexists{stepcolor22}{stepcolor22}{black}"

What do I have to do?

enter image description here

\documentclass[a4paper]{article}
\usepackage{tikz}

\foreach \N/\n in {0/brown, 1/red!70, 2/orange, 3/yellow!80!orange, 
4/green!70!black, 5/blue!50, 6/violet!60, 
7/gray}{    \xglobal\colorlet{stepcolor\N}{\n}      }

\begin{document}
\foreach \n in {0,...,7}{%
\tikz[] \node[fill=stepcolor\n, label=below:{stepcolor\n}]{};
}

\pgfmathtruncatemacro\Endnumber{7} % works
%\pgfmathtruncatemacro\Endnumber{10} % works not

\foreach \n in {0,...,\Endnumber}{%
\begin{tikzpicture}
\node[text=stepcolor\n] {S\n};
\end{tikzpicture}
}

\end{document}
2

This post seems to be what you are looking for:

As far as I got your point.... This code could help you :

\documentclass[a4paper]{article}
\usepackage{tikz}

\foreach \N/\n in {0/brown, 1/red!70, 2/orange, 3/yellow!80!orange, 
4/green!70!black, 5/blue!50, 6/violet!60, 
7/gray}{    \xglobal\colorlet{stepcolor\N}{\n}      }

\makeatletter
\def\IsColorDefined#1{\@ifundefined{\string\color@#1}{black}{#1}}
\makeatother


\begin{document}
\foreach \n in {0,...,7}{%
\tikz[] \node[fill=stepcolor\n, label=below:{stepcolor\n}]{};
}



%\pgfmathtruncatemacro\Endnumber{7} % works
%\pgfmathtruncatemacro\Endnumber{10} % works mot
\xdef\Endloop{0}
\foreach \n in {0,...,1000}{%
\begin{tikzpicture}
\node[text=\IsColorDefined{stepcolor\n}] {S\n};
\end{tikzpicture}
}

\end{document}
| improve this answer | |
  • Yes, thats good. But I meant that if the colors are not defined, a default color is used, e.g. black – cis Jul 5 at 12:03
  • @cis ... Fixed to your requirement.... – koleygr Jul 5 at 12:12
  • Ah, very good. Helps me a lot. :) – cis Jul 5 at 12:14
  • 1
    Thanks @cis ... Happy TeXing... – koleygr Jul 5 at 12:15

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.