我有一张有三行节点的图。我想通过将节点稍微向右移动,使第一行和第三行居中,以使图看起来更平衡:
我尝试过\centering
,但没有效果。
代码如下:
\documentclass{paper}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=4cm,
thick]
\tikzstyle{author node} = [circle,fill=blue!20,right]
\tikzstyle{thread node} = [circle,fill=red!20,right]
\node[author node] (1) {A};
\node[author node] (2) [right of=1] {B};
\node[thread node] (3) [below of=1] {1};
\node[thread node] (4) [right of=3] {2};
\node[thread node] (5) [right of=4] {3};
\node[thread node] (6) [right of=5] {4};
\node[author node] (7) [below of=3] {C};
\node[author node] (8) [right of=7] {D};
\node[author node] (9) [right of=8] {E};
\path[every node/.style={font=\sffamily\small}]
(1) edge node {} (3)
(1) edge node {} (4)
(2) edge node {} (5)
(2) edge node {} (6)
(7) edge node {} (3)
(7) edge node {} (4)
(7) edge node {} (4)
(7) edge node {} (5)
(8) edge node {} (4)
(8) edge node {} (6)
(8) edge node {} (6)
(9) edge node {} (3)
(9) edge node {} (5)
(9) edge node {} (6);
\end{tikzpicture}
\end{figure}
\end{document}
答案1
加载positioning
库可以更好地控制节点的相对定位。但由于您的节点大小略有不同,我设置了在定位过程中on grid
仅引用节点锚点的选项(这意味着节点可能会重叠)。.center
圆形形状更难像网格那样定位,因为它们的指南针锚点放在圆圈上:circle
适合rectangle
(正方形) 的 有相同的.north
,.east
等等 锚点,但没有.north east
,.south east
等等。
我使用了,这样图表就小了,也更适合在这里展示。 的一次出现node distance
必须给出手动距离,这样垂直部分与当前距离相同,但水平距离是其一半。2cm
below right
node distance
代码
\documentclass[tikz,class=paper]{standalone}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}[
node distance=2cm, on grid,
outer sep=+0pt,
author node/.style={circle, fill=blue!20},
thread node/.style={circle, fill=red!20 }]
\node[author node] (tA) {A};
\node[author node, right=of tA] (tB) {B};
\node[thread node, below=of tA] (c2) {2};
\node[thread node, left =of c2] (c1) {1};
\node[thread node, right=of c2] (c3) {3};
\node[thread node, right=of c3] (c4) {4};
\node[author node, below right=2cm and 1cm of c1] (bC) {C};
\node[author node, right=of bC] (bD) {D};
\node[author node, right=of bD] (bE) {E};
\path[-stealth', shorten >=+1pt, thick]
(tA) edge (c1) edge (c2)
(tB) edge (c3) edge (c4)
(bC) edge (c1) edge (c2) edge (c3)
(bD) edge (c2) edge (c4)
(bE) edge (c1) edge (c3) edge (c4)
;
\end{tikzpicture}
\end{document}