如何将一个节点垂直对齐到一个节点,并水平对齐到另一个节点?
例如,给出下面的代码
\documentclass{standalone}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{makecell}
\usetikzlibrary{arrows,shapes,automata,petri,positioning}
\tikzset{
data/.style={
ellipse,
thick,
draw=black,
minimum size=6mm
},
process/.style={
rectangle,
thick,
draw=black
},
}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[node distance=1 and 1,>=stealth',bend angle=45,auto]
\node [data] (a) {a};
\node [process,align=center] (b) [below= of a] {bbbbbbbbbbb}
edge[pre] (a);
\node [data,align=center] (c) [below= of b]{cc\\ccc\\ccccc}
edge[pre] (b);
\node [process,align=center] (d) [below right= of c] {dddd\\ddddddd}
edge[pre] (c);
\node [data] (e) [above right= of d] {e}
edge[post] (d);
\node [process] (f) [above= of e] {f}
edge[post] (e);
\node [data] (g) [above= of f] {g}
edge[post] (f);
\end{tikzpicture}
\end{document}
结果:
我想要三个节点埃,F,G将是垂直底部/中间/中心对齐,并且(A,G), (bb,F)(加拿大合作委员会,埃)将是水平底部/中间/中心对齐。
例如上图就是我想要的。注意红线不会显示出来,它代表的是“基线对齐线”
感谢您的帮助。
答案1
您可以使用正交坐标,例如:
\node [process] (f) at (b-|g) {f}
代码:
\documentclass{standalone}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{makecell}
\usetikzlibrary{arrows,shapes,automata,petri,positioning,calc}
\tikzset{
data/.style={
ellipse,
thick,
draw=black,
minimum size=6mm
},
process/.style={
rectangle,
thick,
draw=black
},
}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[node distance=1 and 1,>=stealth',bend angle=45,auto]
\node [data] (a) {a};
\node [data] (g) [right = 4cm of a] {g};
\node [process,align=center] (b) [below= of a] {bbbbbbbbbbb}
edge[pre] (a);
\node [process] (f) at (b-|g) {f}
edge[pre] (g);
\node [data,align=center] (c) [below= of b]{cc\\ccc\\ccccc}
edge[pre] (b);
\node [data] (e) at (c-|f) {e}
edge[pre] (f);
\node [process,align=center,anchor=center] (d) at ([yshift=-2cm]$(c.center)!0.5!(e.center)$)
{dddd\\ddddddd}
edge[pre] (c)
edge[pre] (e);
\end{tikzpicture}
\end{document}
和tikz
matrix
:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows,shapes,automata,petri}
\begin{document}
\tikzset{
data/.style={
ellipse,
thick,
draw=black,
minimum size=6mm
},
process/.style={
rectangle,
thick,
draw=black
},
}
\begin{tikzpicture}
\matrix (m) [matrix of nodes,nodes={align=center,anchor=center},%
column sep=1cm,%
row sep=1cm]
{
|[data]|a & & |[data]| g \\
|[process]| bbbbbbbbbbb & & |[process]| f \\
|[data,text width = 0.8cm]|cc ccc ccccc & & |[data]| e \\
& |[process,text width = 2cm]| dddd ddddd$d$ & \\
};
\draw (m-1-1) edge[post] (m-2-1)
(m-2-1) edge[post] (m-3-1)
(m-3-1) edge[post] (m-4-2);
\draw (m-1-3) edge[post] (m-2-3)
(m-2-3) edge[post] (m-3-3)
(m-3-3) edge[post] (m-4-2);
\end{tikzpicture}
\end{document}
答案2
这是一个可能的解决方案,matrix of nodes
用于对齐。特别是 (f) 到 (e) 线应用了正交坐标技巧。
代码
\documentclass{standalone}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{makecell}
\usetikzlibrary{matrix,arrows,shapes,automata,petri,positioning,calc}
\tikzset{
data/.style={
ellipse,
thick,
draw=black,
minimum size=6mm
},
process/.style={
rectangle,
thick,
draw=black
},
line/.style = {draw, -latex'},
}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[node distance=1 and 1,>=stealth',bend angle=45,auto]
\matrix[matrix of nodes, column sep=1cm, row sep=1cm]{
\node [data] (a) {a};
&
\node [data] (g) {g}; \\
\node [process,align=center] (b) {bbbbbbbbbbb};
& \node [process] (f) {f}; \\
\node [data,align=center] (c){cc\\ccc\\ccccc};
&\node [data] at (c.center) (e) {e};\\
};
%Draw edges
\node [process,align=center,yshift=-2cm] (d) at ($(c)!0.5!(e)$,0){dddd\\ddddddd};
\draw[line] (a) -- (b);
\draw[line] (b) -- (c);
\draw[line] (c) -- (d);
\draw[line] (g) -- (f);
\draw[line] (f) -- (f|-c.north);
\draw[line] (e) -- (d);
\end{tikzpicture}
\end{document}