这是一个非常简单的例子;如何foo
用数组查找函数替换\k
?我希望红色标签为foo
,绿色标签为bar
,蓝色标签为baz
。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\foreach\color [count=\k] in {red!90!black, green!50!black, blue!90!black}
{
\node[color=\color] at ($(0,\k)$){foo};
}
\end{tikzpicture}
\end{document}
答案1
请参阅 TikZ/PGF 手册中的“数组运算符”,第 927 页
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\newcommand{\myarray}{{"foo","bar","baz"}}
\begin{tikzpicture}[font=\sffamily]
\foreach\Color [count=\k] in {red!90!black, green!50!black, blue!90!black}
{
\node[color=\Color] at ($(0,\k)$){\pgfmathparse{\myarray[\k-1]}\pgfmathresult};
}
\end{tikzpicture}
\end{document}
数组索引从 0 开始
答案2
的替代方法array
是使用两个变量foreach
。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\foreach \color/\t [count=\k] in {red!90!black/foo,
green!50!black/bar, blue!90!black/baz}
{
\node[color=\color] at ($(0,\k)$){\t};
}
\end{tikzpicture}
\end{document}