tikz:适合节点的宽度但自动高度

tikz:适合节点的宽度但自动高度

我想使用类似以下代码在 tikz 中创建一个图表:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning,fit}
\begin{document}    
%\input{content/renalBrainshake/gfx/flowchart.tikz}

\begin{tikzpicture}[every node/.style={draw, fill=blue, font=\sffamily\tiny}, align=center, node distance = 2.1cm]
    %%%%% COHORT NODES
    %% DIABETICS
    \node (n1)  []              {node1\\ line 2};
    \node (n2)  [right of = n1] {node2\\ line 2};
    \node (n3)  [right of = n2] {node3};
    \node (long)[fit   = (n1)(n3),
            above = 1cm of n1.west,
            anchor= south west, inner sep=0] {long};
\end{tikzpicture}
\end{document}

在此处输入图片描述

存在 3 个问题:

  1. 我希望三个节点n1n2具有n3相同的高度,而不必使用minimum height
  2. 我希望长节点适合其他三个节点的宽度,但long节点的高度应该根据其中的文本自动调整
  3. 中的文本long应该垂直居中,但这里却不是

有人对这一切有好的解决办法吗?

答案1

fit还可将n3和用于节点和label=center:text中的文本。n3long

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning,fit}
\begin{document}    
%\input{content/renalBrainshake/gfx/flowchart.tikz}

\begin{tikzpicture}[every node/.style={draw, fill=blue, font=\sffamily\tiny}, align=center, node distance = 2.1cm]
    %%%%% COHORT NODES
    %% DIABETICS
    \node (n1)  []              {node1\\ line 2};
    \node (n2)  [right of = n1] {node2\\ line 2};
    \node (n3)  [fit= (n2), inner sep=0pt, right of = n2, label=center:node3] {};
    \node (long)[fit = (n1)(n3),
            above = 1cm of n1.west,
            anchor= south west, inner sep=0, label=center:long] {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

更新:

如果您想调整long节点的高度,部分解决方案可能是:

\node (long)[fit = (n1.west)(n3.east),
        above = 1cm of n1.west,
        anchor= south west, inner xsep=0, label=center:{long}, 
        minimum height={height("long")}
        ] {};

新的fit选项并inner xsep=0定义其纵向尺寸和最小高度={height("long")}(来自calctikzlibrary)定义垂直。

对于内容较高的节点,另一种解决方案是使用固定高度的辅助节点和fit覆盖高度的节点。辅助节点可以使用以下方式创建:

\node (aux) [above = 1cm of n1.west, anchor=south west, fill=red!30] {long\\long\\long};

它是一个常规节点,放置在我们想要的位置(above = 1cm of n1.west, anchor=south west),并调整大小以包含其文本。

现在我们可以使用它的锚点来定义新的节点,只要之前的 n1、n2 和 n3 并且调整高度到它的内容。

\node (long)[fit = {(n1.west|-aux.south)(n3.east|-aux.north)},
            above = 1cm of n1.west,
            anchor= south west, 
            opacity = .5, %<--- delete this line in final figure
            inner sep=-.5\pgflinewidth, label=center:{long\\ long\\ long}, 
          ] {};

正如您所见fit = {(n1.west|-aux.south)(n3.east|-aux.north)}inner sep=-.5\pgflinewidth定义新闻节点的大小。

并且opacity=.5已包括查看aux节点。在生成最终结果之前,请注释掉此行或将其删除。

以下代码中的所有拟合节点都包含inner sep=-.5\pgflinewidth。这样,fit节点将具有与引用节点完全相同的大小。使用inner sep=0pt,拟合节点在其边框内包含参考锚点,这意味着结果节点比拟合节点大半个线宽。

完整代码如下:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning,fit}
\begin{document}    
%\input{content/renalBrainshake/gfx/flowchart.tikz}

\begin{tikzpicture}[every node/.style={draw, fill=blue!50, font=\sffamily\tiny}, align=center, node distance = 2.1cm]
    %%%%% COHORT NODES
    %% DIABETICS
    \node (n1)  []              {node1\\ line 2};
    \node (n2)  [right of = n1] {node2\\ line 2};
    \node (n3)  [fit= (n2), inner sep=-.5\pgflinewidth, right of = n2, label=center:node3] {};
    \node (aux) [above = 1cm of n1.west, anchor=south west, fill=red!30] {long\\long\\long};

    \node (long)[fit = {(n1.west|-aux.south)(n3.east|-aux.north)},
            above = 1cm of n1.west,
            anchor= south west, 
            opacity = .5,
            inner sep=-.5\pgflinewidth, label=center:{long\\ long\\ long}, 
          ] {};

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容