我正在尝试将一个值(第一个输入参数)和一个数组(第二个输入参数)传递给 Tikzset,我想检查该值是否在数组中。每次运行代码时,都会将数组作为整体读取,而不是逐个元素读取。这里,颜色填充是进行搜索,然后调用我之前定义的其他 Tikzset。当我使用 Ifnum 时,只将值作为输入和一个数字,一切都运行正常,但我在数组输入方面遇到了困难。
\begin{figure*}[h]
\begin{adjustbox}{minipage=\textwidth,scale=0.8}
\begin{tikzpicture}[node distance = 2cm, on grid,>={Stealth[inset=0pt,length=6pt,angle'=28,round]},fill fraction/.style n args={2}{path picture={
\fill[#1] (path picture bounding box.center) -- (path picture bounding box.south) arc [start angle=-90, delta angle=-100, radius=1] -- cycle;
\fill[#2] (path picture bounding box.center) -- (path picture bounding box.south) arc [start angle=-90, delta angle=100, radius=1] -- cycle;
}}
,fill fraction2/.style n args={1}{path picture={
\fill[#1] (path picture bounding box.center) -- (path picture bounding box.south) arc [start angle=-90, delta angle=-180, radius=1] -- cycle;
}}
]
\tikzset{
redfill/.code args={}{
\definecolor{mycolour1}{rgb}{1,0,0}%
\pgfkeysalso{/tikz/fill=mycolour1}
}
}
\tikzset{
greenfill/.code args={}{
\definecolor{mycolour2}{rgb}{0,1,0}%
\pgfkeysalso{/tikz/fill=mycolour2}
}
}
\tikzset{
bluefill/.code args={}{
\definecolor{mycolour3}{rgb}{0,0,1}%
\pgfkeysalso{/tikz/fill=mycolour3}
}
}
\tikzset{colorfill/.code n args={2}{
\foreach \x in {#2}{
\ifnum \x = #1
\tikzset{greenfill={}}
\else
\tikzset{redfill={}}
\fi
}
}}
\def\greenstatesind{
{2,4}
}
\def\redstatesind{
3
}
\node[state,initial](q1){$q^{1}$};
\foreach \i[evaluate=\i as \j using int(\i-1),evaluate=\i as \k using int(\i)] in {2,...,5}{
\node[state, right of=q\j, colorfill={\k},{\greenstatesind}] (q\i){$q^{\i}$};
}
\end{tikzpicture}
\end{adjustbox}
\end{figure*}
答案1
有几个小问题,比如不可见空格等,这些问题很容易解决。如果您想使用循环来查看列表中是否包含给定元素,则还有两个更微妙的问题:
- 如果想要密切关注您提供的代码,则必须使用密钥
/.expanded
以免混淆 pgf。 \tikzset{}
如果在循环中使用foreach
,则不起作用,因为\tikzset
是本地的并且foreach
使用组。但是,/.list
密钥不构成这些组,因此可以改用。
此代码实现了这些更改,并完成了提供给完整文档的片段。
\documentclass{article}
\usepackage{geometry}
\usepackage{adjustbox}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,automata,positioning}
\begin{document}
\begin{figure*}[h]
\begin{adjustbox}{minipage=\textwidth,scale=0.8}
\begin{tikzpicture}[node distance = 2cm, on grid,>={Stealth[inset=0pt,length=6pt,angle'=28,round]},fill fraction/.style n args={2}{path picture={
\fill[#1] (path picture bounding box.center) -- (path picture bounding box.south) arc [start angle=-90, delta angle=-100, radius=1] -- cycle;
\fill[#2] (path picture bounding box.center) -- (path picture bounding box.south) arc [start angle=-90, delta angle=100, radius=1] -- cycle;
}}
,fill fraction2/.style n args={1}{path picture={
\fill[#1] (path picture bounding box.center) -- (path picture bounding box.south) arc [start angle=-90, delta angle=-180, radius=1] -- cycle;
}}
]
\tikzset{
redfill/.code={% args={}{....} is at the very least unnecessary
\definecolor{mycolour1}{rgb}{1,0,0}%
\pgfkeysalso{/tikz/fill=mycolour1}
}
}
\tikzset{%
greenfill/.code={% args={}{....} is at the very least unnecessary
\definecolor{mycolour2}{rgb}{0,1,0}%
\pgfkeysalso{/tikz/fill=mycolour2}
}
}
\tikzset{
bluefill/.code={% args={}{....} is at the very least unnecessary
\definecolor{mycolour3}{rgb}{0,0,1}%
\pgfkeysalso{/tikz/fill=mycolour3}
}
}
\tikzset{colorfill sub/.code={\ifnum\pgfkeysvalueof{/tikz/colorfill item}=#1\relax
\tikzset{greenfill}% greenfill has no argument
\else
\pgfkeysalso{/tikz/redfill}% redfill has no argument
\fi},
colorfill item/.initial=0,
colorfill/.code n args={2}{%
\tikzset{colorfill item=#1,colorfill sub/.list=#2}%
}
}
\def\greenstatesind{%
{2,4}%
}
\def\redstatesind{%
{3}%
}
\node[state,initial](q1){$q^{1}$};
\foreach \i[evaluate=\i as \j using int(\i-1),evaluate=\i as \k using int(\i)] in {2,...,5}{
\node[state,right of=q\j,colorfill/.expanded={\k}{\greenstatesind}% removed comma and added /.expanded
] (q\i){$q^{\i}$};
}
\end{tikzpicture}
\end{adjustbox}
\end{figure*}
\end{document}
请注意,这项任务很可能可以大大简化。pgf 有\pgfutil@in@
一个低级成员资格测试,在 pgf 中被广泛使用。我不知道它是否为此类测试提供了用户界面。