Tikz 流程图节点未对齐

Tikz 流程图节点未对齐

例子:

\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{matrix, shapes, arrows, positioning}
\usepackage{xcolor}

\begin{document}    
\begin{figure}
\begin{center}
\tikzset{%
    decision/.style = {diamond, draw, fill=blue!20, text width=5em, text centered, inner sep=0pt, minimum width=3.5cm},
    block/.style = {rectangle, draw, fill=blue!20,text width=5em, text centered, rounded corners, minimum width=3.5cm},
    io/.style ={trapezium, draw, minimum width=2.5cm,trapezium left angle=60, trapezium right angle=120},
    line/.style = {draw, -stealth},
    cloud/.style = {draw, rectangle, fill=red!20, minimum height=2em, minimum width=1.5cm, rounded corners=10pt}}

\begin{tikzpicture}[auto]
>=triangle 60,
\matrix[matrix of nodes, column sep=2cm, row sep=1cm]{%
    \node [decision] (c) {abc fffffffffffffff};&
    \node [block]    (d) {def ffffffffffffffffffffffff ss} ; &\\
};
% Draw edges
\path [line] (c) -- (d);
\end{tikzpicture}
\end{center}
\end{figure}
\end{document}

输出:

在此处输入图片描述

无论节点的内容是什么,如何确保箭头始终是平的/水平的?

答案1

这里的问题其实是matrix of nodes。错位来自于两个节点base由于 而锚定在 处的事实matrix of nodes,而在您的情况下您希望它们锚定在 处center

请注意,的目的matrix of nodes是为了避免编写全部\node [<options>] (foo) {text};,它允许您|[<options>]| text直接编写,其中|[<options>]|是可选的。由于您实际上并未在所显示的代码中使用该功能,因此您可能只需删除该matrix of nodes选项,即可获得所需的对齐。

另一个选项是添加anchor=center到您使用的节点样式。这将覆盖所做的设置matrix of nodes

在下面的第三部分中tikzpicture,我将展示如何使用matrix of nodes。此类矩阵中的节点会根据行和列自动命名。

最后说明:矩阵中有一个空单元格,导致右侧出现多余的空白。&出于这个原因,我删除了最后一个单元格。

\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{matrix, shapes, arrows, positioning}
\tikzset{%
    decision/.style = {diamond, draw, fill=blue!20, text width=5em, text centered, inner sep=0pt, minimum width=3.5cm},
    block/.style = {rectangle, draw, fill=blue!20,text width=5em, text centered, rounded corners, minimum width=3.5cm},
    io/.style ={trapezium, draw, minimum width=2.5cm,trapezium left angle=60, trapezium right angle=120},
    line/.style = {draw, -stealth},
    cloud/.style = {draw, rectangle, fill=red!20, minimum height=2em, minimum width=1.5cm, rounded corners=10pt}
    }
\begin{document}    

\begin{tikzpicture}[auto]
\matrix[column sep=2cm, row sep=1cm]{%
    \node [decision] (c) {abc fffffffffffffff};&
    \node [block]    (d) {def ffffffffffffffffffffffff ss} ; \\
};
% Draw edges
\path [line] (c) -- (d);
\end{tikzpicture}

\tikzset{ %%% add anchor=center
  decision/.append style={anchor=center},
  block/.append style={anchor=center}
  }

\begin{tikzpicture}[auto]
\matrix[column sep=2cm, row sep=1cm]{%
    \node [decision] (c) {abc fffffffffffffff};&
    \node [block]    (d) {def ffffffffffffffffffffffff ss} ; \\
};
% Draw edges
\path [line] (c) -- (d);
\end{tikzpicture}

\begin{tikzpicture}[auto]

\matrix[matrix of nodes, column sep=2cm, row sep=1cm] (m) {%
    |[decision]| abc fffffffffffffff&
    |[block]| def ffffffffffffffffffffffff ss \\
};
% Draw edges
\path [line] (m-1-1) -- (m-1-2);
\end{tikzpicture}
\end{document}

相关内容