中途改变 TikZ 节点的位置

中途改变 TikZ 节点的位置

我正在尝试将节点“H”移动到下面以红色显示的两个位置之一:

我不确定如何将其定位在我建议的“中间”位置。这该怎么做?有没有更合理的方式来定义节点的整体位置?代码如下:

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{arrows}
\begin{document}

\begin{tikzpicture}[
    -,
    every loop/.style={},
    auto,
    node distance = 4cm,
    thick,
    main node/.style={
        circle,
        fill=gray!50,
        draw,
        font=\rmfamily\Large\bfseries,
        minimum size=15mm
    }
]

\node[main node] (W) [] {W};
\node[main node] (Z) [right of=W] {Z};
\node[main node] (q) [above right of=Z] {q};
\node[main node] (g) [below right of=q] {g};
\node[main node] (l) [above left of=W] {l};
\node[main node] (gamma) [below left of=l] {${\gamma}$};
\node[main node] (H) [below left of=W] {H};

  \path[
    every node/.style={
        font=\rmfamily\small,
        fill=white,
        inner sep=1pt
    }
]

(H) edge [loop below] node {} (H)
(H) edge [] node {} (W)
(H) edge [] node[right=1mm] {} (Z)
(H) edge [] node[right=1mm] {} (l)
(W) edge [loop left] node[right=1mm] {} (W)
(W) edge [] node[right=1mm] {} (Z)
(q) edge [] node[right=1mm] {} (Z)
(q) edge [] node[right=1mm] {} (W)
(q) edge [] node[right=1mm] {} (H)
(Z) edge [] node[right=1mm] {} (l)
(l) edge [] node[right=1mm] {} (gamma)
(q) edge [] node[right=1mm] {} (gamma)
(q) edge [] node[right=1mm] {} (g)
(g) edge [loop below] node {} (g)
;

\end{tikzpicture}
\end{document}

答案1

我建议采用positioning引入更多灵活放置选项的库用法,或者将您的图表转换为matrix对于此类常规构造非常有用的图表。

positioning您的代码可以更改为

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{arrows, positioning}
\begin{document}

\begin{tikzpicture}[
    -,
    every loop/.style={},
    auto,
% node distance = 4cm,
% on grid,
    thick,
    main node/.style={
        circle,
        fill=gray!50,
        draw,
        font=\rmfamily\Large\bfseries,
        minimum size=15mm
    }
]

\node[main node] (W) [] {W};
\node[main node] (l) [above left = of W] {l};
\node[main node] (gamma) [below left = of l] {${\gamma}$};
\node[main node] (H) [above right = of W] {H};
\node[main node] (Z) [below right = of H] {Z};
\node[main node] (q) [above right = of Z] {q};
\node[main node] (g) [below right = of q] {g};

  \path[
    every node/.style={
        font=\rmfamily\small,
        fill=white,
        inner sep=1pt
    }
]

(H) edge [loop above] node {} (H)
(H) edge [] node {} (W)
(H) edge [] node[right=1mm] {} (Z)
(H) edge [] node[right=1mm] {} (l)
(W) edge [loop left] node[right=1mm] {} (W)
(W) edge [] node[right=1mm] {} (Z)
(q) edge [] node[right=1mm] {} (Z)
(q) edge [] node[right=1mm] {} (W)
(q) edge [] node[right=1mm] {} (H)
(Z) edge [] node[right=1mm] {} (l)
(l) edge [] node[right=1mm] {} (gamma)
(q) edge [] node[right=1mm] {} (gamma)
(q) edge [] node[right=1mm] {} (g)
(g) edge [loop below] node {} (g)
;

\end{tikzpicture}
\end{document}

结果是

在此处输入图片描述

更新

按照 Harish Kumar 的建议,解决方案如下matrix

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{arrows, matrix}
\begin{document}

\begin{tikzpicture}[
    -,
    every loop/.style={},
    auto,
    thick,
    main node/.style={
        circle,
        fill=gray!50,
        draw,
        font=\rmfamily\Large\bfseries,
        minimum size=15mm
    }
]

\matrix (A) [matrix of nodes, nodes={main node}, row sep=8mm, column sep=4mm]{
& I & & H & & q \\
$\gamma$ & & W & & Z & & g\\
};

\path[
    every node/.style={
        font=\rmfamily\small,
        fill=white,
        inner sep=1pt
    }
]

(A-1-4) edge [loop above] node {} (A-1-4)
(A-1-4) edge [] node {} (A-2-3)
(A-1-4) edge [] node[right=1mm] {} (A-2-5)
(A-1-4) edge [] node[right=1mm] {} (A-1-2)
 (A-2-3) edge [loop left] node[right=1mm] {}  (A-2-3)
 (A-2-3) edge [] node[right=1mm] {} (A-2-5)
(A-1-6) edge [] node[right=1mm] {} (A-2-5)
(A-1-6) edge [] node[right=1mm] {}  (A-2-3)
(A-1-6) edge [] node[right=1mm] {}(A-1-4)
(A-2-5) edge [] node[right=1mm] {} (A-1-2)
(A-1-2) edge [] node[right=1mm] {} (A-2-1)
(A-1-6) edge [] node[right=1mm] {} (A-2-1)
(A-1-6) edge [] node[right=1mm] {} (A-2-7)
(A-2-7) edge [loop below] node {} (A-2-7)
;

\end{tikzpicture}
\end{document}

相关内容