我想根据一些计算draw
来设置选项。更具体地说,我想根据数组中的某个值来绘制或不绘制边。tikz
pgfmath
如果计算可以在实际\draw
命令之外执行,则此方法可以正常工作,如下例所示:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[every node/.style={circle, inner sep=0mm,minimum size=1mm,draw}]
\foreach \i in {1,...,7}{
\node (\i) at (\i,0) {\i};
}
\foreach \i in {1,...,6}{
\pgfmathparse{ifthenelse(array({1,0,0,0,1,1},\i-1)==1, "black", "none")}
\draw[draw=\pgfmathresult] (\i) -- (\the\numexpr\i+1\relax);
}
\end{tikzpicture}
\end{document}
然而,在某些情况下(例如使用时grow cyclic
,如下所示),如何在命令之外执行计算并不明显\draw
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[
grow cyclic,circle,sibling angle=30,draw,
level 1/.style={level distance=3cm},
level 2/.style={level distance=3cm,sibling angle=60},
every node/.style={inner sep=0mm,minimum size=1mm,draw}]
\node (Ac) {$u$} child foreach \i in {1,...,7} {
node (\i) {\i} %edge from parent[draw=\pgfmathparse{ifthenelse(array({1,0,0,0,1,1},\i-1)==1, "black", "none")}\pgfmathresult];
};
\end{tikzpicture}
\end{document}
注释部分是尝试在现场执行计算,但是这会产生错误消息(我怀疑这tikz
不喜欢\pgfmathparse
不可扩展)。
答案1
改编
- 使用包
etoolbox
- 向数组中添加第 7 个数字以匹配大小
- 创建样式
myline
,获取数组和\i
作为参数,并设置样式mylineB
,然后使用(与/.append style
)。
代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\usepackage{etoolbox}
\begin{document}
\begin{tikzpicture}[
grow cyclic,circle,sibling angle=30,draw,
level 1/.style={level distance=3cm},
level 2/.style={level distance=3cm,sibling angle=60},
every node/.style={inner sep=0mm,minimum size=1mm,draw},
myline/.code 2 args={
\pgfmathparse{array({#1},#2-1)}
\ifnumequal{\pgfmathresult}{1}{%
\tikzset{mylineB/.style={draw=black}}
}{
\tikzset{mylineB/.style={draw=none}}
},
},
myline/.append style={mylineB},
]
\node (Ac) {$u$} child foreach \i in {1,...,7} {
node (\i) {\i} edge from parent[myline={1,0,0,0,1,1,0}{\i}]
};
\end{tikzpicture}
\end{document}