\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 と私の MWE の最後の行を比較してください。

答え2

これはほぼ希望どおりですが、1cm の距離が最初のノードの中心から計算される点が欠点です (したがって、例では右側の 2 つのノードが正しく整列していません)。

 \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}

したがって、ある意味では匿名ノードを描画することは可能ですが、問題は、後続のパスの開始点を変更する必要があるため、2 つのノードに対してのみ機能することです。これは、ノードが匿名の場合は不可能です。

関連情報