我定义了八种颜色(stepcolor0,...,stepcolor7)。
现在我可以有一个包含超过 8 名旗手的列表。
所以我可能需要一个类似函数
“ \ifcolorexists{stepcolor22}{stepcolor22}{black}
”
我需要做什么?
\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}
答案1
这帖子似乎就是您正在寻找的内容:
据我了解你的观点....此代码可以帮助你:
\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}