如何指定主对象的位置而不重新绘制所有图形?

如何指定主对象的位置而不重新绘制所有图形?

我已经绘制了原始图表(左图),我需要绘制一棵树(右图)。在原始图表中,节点 v1 是主圆。它用于绘制所有对象(圆和线)。我的问题是 v1 节点的位置,因为所有对象都相对于它放置。

在此处输入图片描述

我的尝试是:

\documentclass[a4paper,12pt]{article}
\usepackage[english]{babel}
\usepackage{tikz}

\begin{document}
\begin{center}
\begin{figure}
\subfloat[]
{
\begin{tikzpicture}[
node distance = 60pt,
C/.style={circle, draw, minimum size=10pt}
]
\node[C] (v1) {$v_1$};
\node[C, below of=v1] (v8) {$v_8$};
\node[C, right of=v8] (v9) {$v_9$};
\node[C, below of=v8, xshift=-1cm, yshift=1cm] (v7) {$v_7$};
\node[C, below of=v8] (v6) {$v_6$};
\node[C, left  of=v6, xshift=-1cm] (v2) {$v_2$};
\node[C, right of=v6] (v5) {$v_5$};
\node[C, below of=v6] (v3) {$v_3$};
\node[C, right of =v3, xshift=1cm] (v4) {$v_4$}; 
\draw
(v1) --node[above]{1} (v9)
(v9) --node[left]{1} (v5)
(v1) --node[above]{10} (v2)
(v9) --node[above]{0.5} (v8)
(v8) --node[pos = 0.9, right]{0.5} (v7)
(v7) --node[pos = 0.9, left]{1} (v6)
(v2) --node[above]{1} (v6)
(v6) --node[above]{1} (v5)
(v2) --node[above]{8} (v3)
(v5) --node[above]{2} (v3)
(v5) --node[left]{2} (v4)
(v3) --node[above]{9} (v4);
\end{tikzpicture}
}
\subfloat[]
{
\begin{tikzpicture}[
node distance = 60pt,
C/.style={circle, draw, minimum size=10pt}
]
\node[C] (v1) {$v_1$}; % 
\node[C, below of=v1, xshift=2.1cm, yshift=2cm] (v9) {$v_9$};
\node[C, below of=v1] (v6) {$v_6$};
\node[C, left  of=v6, xshift=-1cm] (v2) {$v_2$};
\node[C, right of=v6] (v5) {$v_5$};
\node[C, below of=v6] (v3) {$v_3$};
\node[C, right of =v3, xshift=1cm] (v4) {$v_4$}; 
\draw
(v1) --node[above]{1} (v9)
(v9) --node[left]{1} (v5)
(v2) --node[above]{1} (v6)
(v6) --node[above]{1} (v5)
(v5) --node[above]{2} (v3)
(v5) --node[left]{2} (v4);
\end{tikzpicture}
}

\end{figure}
\end{center}
\end{document}

问题。是否可以在不重新绘制所有图形的情况下指定主要对象的位置?

答案1

您从一个空(v1)节点(或\coordinate (v1);)开始,然后在最后将节点替换到您真正想要的位置。

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}[
node distance=60pt,
C/.style={circle, draw, minimum size=10pt}
]
\node (v1) {};
\node[C, right of=v1] (v9) {$v_9$};
\node[C, below of=v1] (v6) {$v_6$};
\node[C, left  of=v6, xshift=-1cm] (v2) {$v_2$};
\node[C, right of=v6] (v5) {$v_5$};
\node[C, below of=v6] (v3) {$v_3$};
\node[C, right of =v3, xshift=1cm] (v4) {$v_4$}; 
\node[C, above left of=v9] (v1) {$v_1$};
\draw
(v1) --node[above]{1} (v9)
(v9) --node[left]{1} (v5)
(v2) --node[above]{1} (v6)
(v6) --node[above]{1} (v5)
(v5) --node[above]{2} (v3)
(v5) --node[left]{2} (v4);
\end{tikzpicture}
\end{document}

连通图

相关内容