如何在 tikz 中对齐节点

如何在 tikz 中对齐节点

我希望节点在我的 tikz 图中以某种方式对齐。

首先考虑下面的代码:



\tikzstyle{rec style}= [draw , shape = rectangle , fill = white , drop shadow , minimum height = 1.5cm , rounded corners , minimum width = 2cm , align = center]

\newcommand\ppbb{path picture bounding box}


\tikzstyle{diamond plus} = [diamond, draw, minimum size=8mm , font=\bfseries ,
path picture = {\draw[line width=1mm,shorten <=2mm,shorten >=2mm]
(path picture bounding box.north) edge (\ppbb.south)
(\ppbb.west)  edge (\ppbb.east);
},node contents={}]


\tikzstyle{arrow style} = [-{Latex[scale = 1.2]} , semithick]



\usepackage{positioning}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[node distance = 2cm]

\node (a)[draw, rec style ]{A};


\node (b)[draw, rec style , below = of a.center ]{B};


\node (c)[draw, diamond plus , below = of b.center];



\node (d)[draw, rec style , below left = of c.center , xshift = -2cm]{C};

\node (e)[draw, rec style , below right = of c.center , xshift = 2cm]{D};


\node (f)[draw, diamond plus , below = of d.center];


\node (g)[draw, diamond plus , below = of e.center];




\node (h)[draw, rec style, below left = of f.center , yshift = -1cm]{E};


\node (i)[draw, rec style, below left = of g.center , yshift = -1cm]{F};


\node(j)[diamond plus , below = of c.center , yshift = -9cm];

\node(k)[draw , rec style , below = of j]{G};


% Now let's draw the arrows;


\draw [arrow style]  (a) --  (b);


\draw [arrow style]  (b) --  (c);


\draw [arrow style]  (c) -|  (d);


\draw [arrow style]  (c) -|  (e);

\draw [arrow style]  (d) --  (f);


\draw [arrow style]  (e) --  (g);



\draw [arrow style]  (f)  -|   (h);   % Node on which we will place text;


\draw [arrow style]  (g) -|  (i);


\draw [arrow style]  (h) |-  (j);


\draw [arrow style]  (h) |-  (j);


\draw [arrow style]  (f) -|  (j);


\draw [arrow style](j) -- (k);




\end{tikzpicture}
\end{document}

下图说明了我想要做的事情。我希望图中红点所示的节点与节点 i 和 j 的中心对齐。

工作示例

答案1

我认为您在处理相对位置未沿树向下对齐的问题(我的代码中的节点 h 和 i)。我为可能对此更了解的其他人清理了 MWE。我自己会使用绝对坐标。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}

\node (a)[draw]{A};
\node (b)[draw, below = of a]{B};
\node (junc1a)[draw, below = of b]{};
\node (junc1b)[right = of junc1a]{};
\node (c)[draw, below left = of junc1a]{C};
\node (d)[draw, below right = of junc1b]{D};
\node (junc2a)[draw, below = of c]{};
\node (junc2b)[draw, below = of d]{};
\node (e)[draw, below left = of junc2a]{E};
\node (junc3)[below right = of junc2a]{};
\node (f)[draw, below left = of junc2b]{F};
\node (junc4)[draw, below = of junc3]{};
\node (g)[draw, below = of junc4]{G};

\node (h)[draw, below = of f]{};
\node (i)[draw, right = of junc4]{};

\draw (a) -- (b);
\draw (b) -- (junc1a);
\draw (junc1a) -| (c);
\draw (junc1a) -| (d);
\draw (c) -- (junc2a);
\draw (d) -- (junc2b);
\draw (junc2a) -| (e);
\draw (junc2a) -| (junc4);
\draw (junc2b) -| (f);
\draw (e) |- (junc4);
\draw (junc4) -- (g);

\end{tikzpicture}
\end{document}

为了明确起见,我会使用:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}

\node (a)[draw] at (2,0) {A};
\node (b)[draw] at (2,-1) {B};
\node (junc1)[draw] at (2,-2) {};
\node (c)[draw] at (1,-3) {C};
\node (d)[draw] at (4,-3) {D};
\node (junc2a)[draw] at (1,-4) {};
\node (junc2b)[draw] at (4,-4) {};
\node (e)[draw] at (0,-5) {E};
\node (f)[draw] at (3,-5) {F};
\node (junc3)[draw] at (2,-6) {};
\node (g)[draw] at (2,-7) {G};

\node (h)[draw] at (3,-6) {};

\draw (a) -- (b);
\draw (b) -- (junc1);
\draw (junc1) -| (c);
\draw (junc1) -| (d);
\draw (c) -- (junc2a);
\draw (d) -- (junc2b);
\draw (junc2a) -| (e);
\draw (junc2a) -| (junc3);
\draw (junc2b) -| (f);
\draw (e) |- (junc3);
\draw (junc3) -- (g);

\end{tikzpicture}
\end{document}

相关内容