TikZ:宽度相等但高度“合适”的背景矩形

TikZ:宽度相等但高度“合适”的背景矩形

我正在尝试为一系列步骤制作某种流程图,但这些步骤也是分组的,我希望我的图表能够反映这一点。因此,我使用 pgfonlayer/background 环境在这些组周围绘制矩形。

我发现了一个类似的例子,它使用fit参数来找到正确的尺寸。

这个例子运行得很好,除了组的宽度不相等,所以矩形看起来不太好看:

丑陋的例子

这就是该代码(引出第一个节点“Wissenserhebung”):

\begin{pgfonlayer}{background}
  \node [background, fit=(elicit)] {};
  % others similiar
\end{pgfonlayer}

然后,我找到了另一种方法,使用矩形和大量计算:

[很好的例子][2]

  \draw [background] ($(elicit.north west)-(0.2,-0.2)$) rectangle ($(matrix.east)!(elicit.south east)!(matrix.east)+(0.2,-0.2)$);
  \draw [background] ($(elicit.north west)!(interpret.north west)!(elicit.north west)-(0.2,-0.2)$) rectangle ($(matrix.east)!(model.south east)!(matrix.east)+(0.2,-0.2)$);
  \draw [background] ($(elicit.north west)!(representation.north west)!(elicit.north west)-(0.2,-0.2)$) rectangle ($(matrix.east)!(representation.south east)!(matrix.east)+(0.2,-0.2)$);

通过该代码,我得到了漂亮的矩形,但它们似乎不是节点,所以我无法将文本附加到它们。

有没有一种“规范”的方法可以让这些矩形具有相同的宽度,但适合内部节点的高度,并能够附加文本?我找到了关于修改边界矩形的帖子,但我不确定这是否或如何能帮助我。

这是完整的代码(减去样式定义):

\begin{tikzpicture}
\matrix (matrix) [row sep=0.5cm,column sep=0.5cm] {
  \node (elicit) [schritt] {Wissenserhebung}; & \\
  \node (interpret) [schritt] {Interpretation}; & \\
  & \node (model) [schritt] {Modellierung}; \\
  \node (representation) [schritt] {Repräsentation}; & \\
  \node (integration) [schritt] {Integration}; & \\
  \node (maintenance) [schritt] {Wartung}; & \\
};

\path[->]
  (elicit) edge (interpret)
  (interpret) edge node[right] {\hspace{.35cm}\tiny Modellbasierter Ansatz} (model)
  (interpret) edge node[right] {\tiny Rapid Prototyping} (representation)
  (model) edge (representation)
  (representation) edge (integration)
  (integration) edge (maintenance);
\begin{pgfonlayer}{background}
  % ugly, but nodes
  %\node [background, fit=(elicit)] {};
  %\node [background, fit=(interpret) (model)] {};
  %\node [background, fit=(representation)] {};

  % no nodes, but correct width
  %\draw [background] ($(elicit.north west)-(0.2,-0.2)$) rectangle ($(matrix.east)!(elicit.south east)!(matrix.east)+(0.2,-0.2)$);
  %\draw [background] ($(elicit.north west)!(interpret.north west)!(elicit.north west)-(0.2,-0.2)$) rectangle ($(matrix.east)!(model.south east)!(matrix.east)+(0.2,-0.2)$);
  %\draw [background] ($(elicit.north west)!(representation.north west)!(elicit.north west)-(0.2,-0.2)$) rectangle ($(matrix.east)!(representation.south east)!(matrix.east)+(0.2,-0.2)$);
\end{pgfonlayer}
\end{tikzpicture}

答案1

您可以通过向位于边界框左边界和右边界的fit每个列表添加坐标来将节点与库一起使用,但这些坐标与要突出显示的节点的垂直位置相同。您可以使用库及其语法来执行此操作,该语法将点投影到从到 的线上。fitcalc($(A)!(C)!(B)$)(C)(A)(B)

在下面的示例中,我使用了一个脚本,该脚本以节点名称作为参数并返回三个坐标:节点本身及其在边界框左右边界上的投影。为了使新框的宽度相同,边界框需要“冻结”,这样一来,一个框就不会影响下一个框的大小。您可以通过在\path [use as bounding box] (current bounding box.north west) (current bounding box.south east)第一个背景框之前发出此命令来做到这一点,这会停止边界框的更新。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{backgrounds,fit,calc}


\begin{document}

\tikzset{
    schritt/.style={
        draw,
        rounded corners,
        fill=blue!20,
        inner xsep=2em,
    },
    background/.style={
        draw,
        fill=yellow!30,
        align=right
    }
}

% Returns three nodes: The argument, and the projections of the argument on the left and right borders of the bounding box
\newcommand{\extendnode}[1]{
    (#1)
    ($(current bounding box.north east)!(#1)!(current bounding box.south east)$)
    ($(current bounding box.north west)!(#1)!(current bounding box.south west)$)
}

\begin{tikzpicture}
\matrix (matrix) [row sep=0.5cm,column sep=0.5cm] {
  \node (elicit) [schritt] {Wissenserhebung}; & \\
  \node (interpret) [schritt] {Interpretation}; & \\
  & \node (model) [schritt] {Modellierung}; \\
  \node (representation) [schritt] {Repräsentation}; & \\
  \node (integration) [schritt] {Integration}; & \\
  \node (maintenance) [schritt] {Wartung}; & \\
};

\path[->]
  (elicit) edge (interpret)
  (interpret) edge node[right] {\hspace{.35cm}\tiny Modellbasierter Ansatz} (model)
  (interpret) edge node[right] {\tiny Rapid Prototyping} (representation)
  (model) edge (representation)
  (representation) edge (integration)
  (integration) edge (maintenance);

\begin{pgfonlayer}{background}

  \path [use as bounding box] (current bounding box.north west) (current bounding box.south east); % Freeze current bounding box
  \node [fit={\extendnode{elicit}}, background] {First};
  \node [fit={\extendnode{interpret} (model)}, background] {Second};
  \node [fit=\extendnode{representation}, background] {Third};
  \node [fit=\extendnode{integration}, background] {Fourth};
  \node [fit=\extendnode{maintenance}, background] {Fifth};

\end{pgfonlayer}
\end{tikzpicture}


\end{document}

等宽节点

相关内容