非垂直居中的 TikZ 节点单元内容矩阵

非垂直居中的 TikZ 节点单元内容矩阵

如何使矩阵单元格内容与顶部对齐?我正在使用 XeTeX 的纯文本格式。

\input tikz
\usetikzlibrary{matrix}
$$\hbox{I'd like for the cells to be top-aligned}\cases{
\tikzpicture[
  a/.style={draw=green!50!black,text width=3.5cm,
    text height=1.5cm,text depth=1.5cm,minimum size=3.5cm,
    anchor=base,
    inner sep=0pt,outer sep=0pt},
  m/.style={matrix of nodes,inner sep=2pt,outer sep=0pt,nodes=a,
    ampersand replacement=\&,row sep=0pt,column sep=0pt,draw=black!50}
  ]
  \parindent0pt
  \matrix[m] (mx) {
    Hey, this is a node with a break\break and stuff\&
    And another one with it. Milkshakes are yummy when the sun is shining\\
    Let's see what happens; I'm trying to find the logic\&
    Does it break when all these cells have different amount of text?\\
  };
  \draw[very thick,red!50] (mx.north)--(mx.south) (mx.west)--(mx.east);
\endtikzpicture
} \hbox{Yes it does}
$$
\bye

在此处输入图片描述

在@Gonzalo 评论更改text height/之后depth

\input tikz
\usetikzlibrary{matrix}
\tikzpicture[
  a/.style={draw=green!50!black,text width=3.5cm,
    text height=0pt,text depth=2.5cm,minimum size=3.5cm,
    anchor=base,
    inner sep=0pt,outer sep=0pt},
  m/.style={matrix of nodes,inner sep=2pt,outer sep=0pt,nodes=a,
    ampersand replacement=\&,row sep=0pt,column sep=0pt,draw=black!50}
  ]
  \parindent0pt
  \matrix[m] (mx) {
    Hey, this is a node with a break\break and stuff\&
    And another one with it. Milkshakes are yummy when the sun is shining\\
    Let's see what happens; I'm trying to find the logic\&
    Does it break when all these cells have different amount of text?\\
  };
  \draw[very thick,red!50] (mx.north)--(mx.south) (mx.west)--(mx.east);
\endtikzpicture
\bye

在此处输入图片描述

答案1

矩阵解决方案(版本 2)

\input tikz
\usetikzlibrary{arrows,calc,matrix,decorations.pathreplacing} 
\parindent=0pt
\tikzpicture[
  b/.style={
      text width=3.2cm,,minimum height=0pt,,minimum width=0pt
      inner sep =.2cm,anchor=north west,
      align=left},
  m/.style={matrix of nodes,
            inner sep=2pt,
            outer sep=0pt,
            column sep=0pt,
            draw=black!50},
            nodes={minimum height=2cm,
                   minimum width=3.6cm}]
  \matrix[m] (M) {%
  {} & {} \\
   {}&  {}\\
  }; 

\node[b] at (M-1-1.north west){%
   Hey, this is a node with a break\break and stuff };
\node[b] at (M-1-2.north west){%
   And another one with it. Milkshakes are yummy when the sun is shining};
\node[b] at (M-2-1.north west){%
   Let's see what happens; I'm trying to find the logic };
\node[b] at (M-2-2.north west){%
   Does it break when all these cells have different amount of text?};      

\draw[very thick,red!50] (M-1-1.north east)--(M-2-1.south east)
                         (M-1-1.south west)--(M-2-2.north east);
\draw[double,green!50!black] (M-1-1.north west) rectangle (M-2-2.south east);
\draw[decorate,decoration={brace,mirror,raise=6pt}]%
     (M-1-1.north west)--(M-2-1.south west);
\node[anchor=east,left=1em] at (M-1-1.south west){%
      I'd like for the cells to be top-aligned};
\node[anchor=west] at (M-1-2.south east) {Yes it does}; 
\endtikzpicture    
\bye

在此处输入图片描述

答案2

您需要对上面的内容进行一些更改。可能有多种方法可以做到这一点,但下面的方法可以给我一个合理的印象。无论它是否是最好的适合您的解决方案取决于您在此处尝试执行的其他操作。要理解它,您需要意识到其中涉及多个层次。文本在节点框中的位置、节点在其矩阵单元中的位置以及矩阵单元的对齐方式。您的样式试图对所有这些起作用并导致冲突。我在下面所做的是通过将大小内容从节点中取出并将其放入矩阵中来简化此过程。

基本问题是你无法完全控制文本的定位之内节点。节点的大小应刚好足以容纳文本并相应地增大。您可以覆盖其自动大小计算,但只能通过指定绝对值。文本是始终位于节点的中心。覆盖该问题的唯一方法是告诉 TikZ/TeX 文本与它认为的并不完全一样 - 这就是text heighttext depth键的作用。但是如果你想告诉 TikZ 确切的高度和深度(因为在这个例子中你不需要,因为它会随着每个节点而变化),那么当你指定 时,它minimum size会在八方以使文本保持处于中心位置。

因此,在进行这种复杂的定位时,最好不要过多地干扰节点,而让矩阵进行对齐。这样anchor=north将每个节点的锚点放在对齐点,但这只有在文本刷新到节点顶部时才有效,并且只有在节点的大小适合其文本时才有效。因此,我的建议(在评论中)没有anchor=north奏效因为您在节点尺寸方面想得太聪明了。

因此我们让节点保持其自然大小,并将控制权交给细胞大小到矩阵。我们使用键来实现这一点row sep,使between origins每个单元格看起来都具有相同的高度。

这确实会弄乱节点的大小,但为了解决这个问题,您可以使用我的matrixcells包(可从 TeX-SX 包中获得,有关更多详细信息,请参阅“从答案到包”)。 免责声明:我不知道它是否适用于纯 TeX。如果你知道节点的确切大小,那么你不需要使用该包(它会过度)。在这种情况下,你需要做的是确保 TikZ/TeX 看到文本作为一个正确尺寸的框(在本例中为3.5cm正方形),并定位文本在那个盒子里随心所欲。在 LaTeX 中,我会使用 minipage 或 parbox 来实现这一点,但我不知道如何在纯 TeX 中实现这一点。

\input tikz
\usetikzlibrary{matrix}
$$\hbox{I'd like for the cells to be top-aligned}\cases{
\tikzpicture[
  a/.style={draw=green!50!black,text width=3.5cm,
    anchor=north,
    inner sep=0pt,outer sep=0pt},
  m/.style={matrix of nodes,inner sep=2pt,outer sep=0pt,nodes=a,
    ampersand replacement=\&,row sep={3.5cm,between origins},column sep=0pt,draw=black!50}
  ]
  \parindent0pt
  \matrix[m] (mx) {
    Hey, this is a node with a break\break and stuff\&
    And another one with it. Milkshakes are yummy when the sun is shining\\
    Let's see what happens; I'm trying to find the logic\&
    Does it break when all these cells have different amount of text?\\
  };
  \draw[very thick,red!50] (mx.north)--(mx.south) (mx.west)--(mx.east);
\endtikzpicture
} \hbox{Yes it does}
$$
\bye

结果:

具有垂直对齐的节点

答案3

哦,天哪,我甚至在评论中大声说了出来,甚至没有意识到它确实可以在节点内部使用:

\input tikz
\usetikzlibrary{matrix}
\tikzpicture
  \parindent0pt
  \matrix[matrix of nodes, inner sep=.5cm, nodes={text width=3cm}] (mx) {
    |[fill=green!10]|\vtop to 3cm{
      Hey, this is a node with a break\hfil\break and stuff\vfil}&
    |[fill=red!10]|\vtop to 3cm{
      And another one with it. Milkshakes are yummy when the sun is shining\vfil}\\
    |[fill=blue!10]|\vtop to 3cm{
      Let's see what happens; I'm trying to find the logic\vfil}&
    |[fill=yellow!10]|\vtop to 3cm{
      Does it break when all these cells have different amount of text?\vfil}\\
  };
  \draw[very thick,red!50] (mx.north)--(mx.south) (mx.west)--(mx.east);
\endtikzpicture
\bye

相关内容