为什么注释掉的行不起作用?我想放置一个在 \draw 命令中声明的节点(实际上是一个匿名节点)。
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc}
\begin{document}
\title{arrow test}
\begin{tikzpicture}[>=latex,
font=\sffamily,
]
\node[draw, thin, black, fill=green, rectangle] (P1) at (0cm,0cm){};
\node[draw, thin, black, fill=red, rectangle, right=1cm of P1] (P2){};
\node[draw, thin, black, fill=green, rectangle] (P3) at (0cm,0.5cm){};
\draw[->] (P1) -- (P2);
%\draw[->] (P3) to node[draw, thin, black, fill=red, rectangle, right=1cm of P3](P4){};
\end{tikzpicture}
\end{document}
答案1
尝试以下 MWE:
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\title{arrow test}
\begin{tikzpicture}[%>=latex,
node distance = 10mm,
box/.style = {rectangle, draw, thin, fill=#1, font=\sffamily},
]
\node[box=green] (P1) at (0cm,0cm) {};
\node[box=red, right=of P1] (P2) {};
\node[box=green] (P3) at (0cm,1cm) {};
\draw[->] (P1) -- (P2);
\draw[->] (P3) node[box=red, right=of P3] (P4) {} -- (P4);
\end{tikzpicture}
\end{document}
坐标无法“引导”……首先您需要定义它,然后使用它。比较您和我的 MWE 中的最后一行。
答案2
这几乎就是您想要的,缺陷在于 1cm 距离是从第一个节点的中心计算的(因此在示例中,两个右侧节点未正确对齐)
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc}
\begin{document}
\title{arrow test}
\begin{tikzpicture}[>=latex,
font=\sffamily,
]
\node[draw, thin, black, fill=green, rectangle] (P1) at (0cm,0cm){};
\node[draw, thin, black, fill=red, rectangle, right=1cm of P1] (P2){};
\node[draw, thin, black, fill=green,opacity=0.5, rectangle] (P3) at (0cm,0.5cm){};
\draw[->] (P1) -- (P2);
\draw[->] (P3) -- +(1cm,0) node[draw, thin, black, fill=red, rectangle, right] (P4) {};
% \draw[->] (0,0.5cm) node[draw, thin, black, fill=green,opacity=0.5,
% rectangle,left] {} -- +(1cm,0) node[draw, thin, black, fill=red, rectangle,
% right] {};
\end{tikzpicture}
\end{document}
因此,从某种意义上说,可以绘制匿名节点,问题是它只适用于两个节点,因为您需要改变连续路径的起点,而如果节点是匿名的,这是不可能的。