例如图片:
可以用以下 MWE 绘制:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{chains, positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 7mm and 7mm,
start chain = going right,
base/.style = {draw, minimum size=7mm},
box/.style = {base, on chain, join=by ->}
]
\coordinate (a) at (0,-1);
\node (n1) [box] at (0,0) {A};
\node (n2) [box] {B};
\node (n3) [box] {C};
\node (n4) [base] at (a -| n2) {D};
\draw[red,->] (n3) |- (n4);
\draw[red,->] (n4) -| (n1);
\end{tikzpicture}
\end{document}
现在,出于某种原因,我只想对节点有一个定义并再次绘制上述图像:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{chains, positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 7mm and 7mm,
start chain = going right,
box/.style = {draw, minimum size=7mm,
on chain, join=by ->}
]
\coordinate (a) at (0,-1);
\node (n1) [box] at (0,0) {A};
\node (n2) [box] {B};
\node (n3) [box] {C};
\node (n4) [box] at (a -| n2) {D};
\draw[red,->] (n3) |- (n4);
\draw[red,->] (n4) -| (n1);
\end{tikzpicture}
\end{document}
结果预期错误:
问题:是否可以说,上述 MWE 中的节点“D”不在链上?例如
\node (n4) [box, suppress chain] at (a -| n2) {D};
join
类似地,它可以通过定义新的选项在链中的节点之间停止:
\tikzset{suppress join/.code={\def\tikz@after@path{}}}
并用作
\node (n4) [box, suppress join] at (a -| n2) {D};
我查看了 TikZ 文档,其中描述了chainin
(第 5449 页),但没有找到任何具有相反操作的内容。
答案1
像这样吗?
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{chains, positioning}
\begin{document}
\makeatletter
\tikzset{
suppress join/.code={\def\tikz@after@path{}},
off chain/.code={\def\tikz@lib@on@chain{}}%
}
\makeatother
\begin{tikzpicture}[
node distance = 7mm and 7mm,
start chain = main going right,
box/.style = {draw, minimum size=7mm, on chain, join=by ->}
]
\coordinate (a) at (0,-1);
\node (n1) [box] at (0,0) {A};
\node (n2) [box] {B};
\node (n3) [box] {C};
\node (n4) [suppress join, off chain, box] at (a -| n2) {D};
\draw[red,->] (n3) |- (n4);
\draw[red,->] (n4) -| (n1);
\end{tikzpicture}
\end{document}
请注意,这里的顺序很重要:off chain
必须在 之前box
。然后,如果您愿意,可以继续该链。
\begin{tikzpicture}[
node distance = 7mm and 7mm,
start chain = main going right,
box/.style = {draw, minimum size=7mm, on chain, join=by ->}
]
\coordinate (a) at (0,-1);
\node (n1) [box] at (0,0) {A};
\node (n2) [box] {B};
\node (n3) [box] {C};
\node (n4) [suppress join, off chain, box] at (a -| n2) {D};
\node [box] {E};
\draw[red,->] (n3) |- (n4);
\draw[red,->] (n4) -| (n1);
\end{tikzpicture}
但请注意,该chains
库已被弃用。