我正在尝试制作一个距离节点 D 右侧 4.5 厘米的表格,然后将其从那里向下移动 1 厘米(向右移动 4.5 厘米,然后向下移动 1 厘米)。
这是我的代码:
\begin{figure}[H]\centering
\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=2.5cm]
\node[state] (S) {$S$};
\node[state] (A) [above right of=S] {$A$};
\node[state] (B) [above right of=A, node distance = 2cm, xshift = 1cm] {$B$};
\node[state] (C) [below right of=A, node distance = 2cm, xshift = 1cm] {$C$};
\node[state] (D) [above right of=C, node distance = 2cm, xshift = 1cm] {$D$};
\node[state] (G) [below right of=D] {$G$};
\path[->] (S) edge node {$1$} (A);
\path[->] (A) edge node {$3$} (B);
\path[->] (B) edge node {$3$} (D);
\path[->] (D) edge node {$3$} (G);
\path[<-] (G) edge [bend left] node {$12$} (S)
edge node {$2$} (C);
\path[<-] (C) edge node {$1$} (A);
\path[->] (C) edge node {$1$} (D);
%\path[->] (S) edge [loop above] node {a} (S)
%edge node {a} (q1)
%(q1) edge [bend left] node {a} (S)
%edge node {b} (q2)
%(q2) edge [loop above] node {b} (q2)
%edge [bend left] node {b} (q1);
\node[right of = D, node distance = 4.5 cm, below of = D, node distance = 1 cm]
{
\begin{tabular}{|c|c|}
\hline
State ($n$) & $h\left(n\right)$\\
\hline
$S$ & 4\\
\hline
$A$ & 2\\
\hline
$B$ & 6\\
\hline
$C$ & 1\\
\hline
$D$ & 3\\
\hline
$G$ & 0\\
\hline
\end{tabular}
};
\end{tikzpicture}
\end{figure}
将桌子摆放到正确位置的正确方法是什么?
答案1
您应该将其用于positioning
所有定位。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,automata,positioning}
\begin{document}
\begin{tikzpicture}[>=Stealth,shorten >=1pt,auto,node distance=1.6cm]
\node[state] (S) {$S$};
\node[state] (A) [above right=of S] {$A$};
\node[state] (B) [above right=of A] {$B$};
\node[state] (C) [below right=of A] {$C$};
\node[state] (D) [above right=of C] {$D$};
\node[state] (G) [below right=of D] {$G$};
\path[->] (S) edge node {$1$} (A);
\path[->] (A) edge node {$3$} (B);
\path[->] (B) edge node {$3$} (D);
\path[->] (D) edge node {$3$} (G);
\path[<-] (G) edge [bend left] node {$12$} (S)
edge node {$2$} (C);
\path[<-] (C) edge node {$1$} (A);
\path[->] (C) edge node {$1$} (D);
\node[right=2.5cm of D,yshift=-1cm]
{
\begin{tabular}{|c|c|}
\hline
State ($n$) & $h\left(n\right)$\\
\hline
$S$ & 4\\
\hline
$A$ & 2\\
\hline
$B$ & 6\\
\hline
$C$ & 1\\
\hline
$D$ & 3\\
\hline
$G$ & 0\\
\hline
\end{tabular}
};
\end{tikzpicture}
\end{document}
答案2
我认为实现此目的更清晰的方法之一是使用xshift
和yshift
选项移动节点坐标。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[>=stealth,shorten >=1pt,auto,node distance=2.5cm]
\node (S) {$S$};
\node (A) [above right of=S] {$A$};
\node (B) [above right of=A, node distance = 2cm, xshift = 1cm] {$B$};
\node (C) [below right of=A, node distance = 2cm, xshift = 1cm] {$C$};
\node (D) [above right of=C, node distance = 2cm, xshift = 1cm] {$D$};
\node (G) [below right of=D] {$G$};
\path[->] (S) edge node {$1$} (A);
\path[->] (A) edge node {$3$} (B);
\path[->] (B) edge node {$3$} (D);
\path[->] (D) edge node {$3$} (G);
\path[<-] (G) edge [bend left] node {$12$} (S)
edge node {$2$} (C);
\path[<-] (C) edge node {$1$} (A);
\path[->] (C) edge node {$1$} (D);
\node at ([xshift=4.5cm, yshift=-1cm]D)
{
\begin{tabular}{|c|c|} \hline
State ($n$) & $h\left(n\right)$ \\ \hline
$S$ & 4 \\ \hline
$A$ & 2 \\ \hline
$B$ & 6 \\ \hline
$C$ & 1 \\ \hline
$D$ & 3 \\ \hline
$G$ & 0 \\ \hline
\end{tabular}
};
\end{tikzpicture}
\end{document}