在 \draw 指令中宣告相對於其他節點的節點

在 \draw 指令中宣告相對於其他節點的節點

為什麼註解掉的行不起作用?我想放置一個在 \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}

因此,從某種意義上說,可以繪製匿名節點,問題是它僅適用於兩個節點,因為您需要更改連續路徑的起點,如果節點是匿名的,則這是不可能的。

相關內容