如何在 tikz 矩阵中排列列标题?

如何在 tikz 矩阵中排列列标题?

我试图让列标题与\shortstack{}单元格中的内容相匹配。我希望顶行相同,而我能尝试使其相同的唯一方法是使用 \rule 引入假间距,但我确信这不是最好的方法。有人能帮我将矩阵中标题行的顶行对齐吗?

https://tex.stackexchange.com/a/536391/209444

\documentclass{examdesign}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric,calc,matrix}
\class{necessary for examdesign}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
%%thanks to Schrodinger's Cat for the following code.
%https://tex.stackexchange.com/a/536391/209444 
\usepackage{eqparbox}
\newbox\matrixcellbox
\tikzset{center align per column/.style={column #1/.style={nodes={execute at begin
 node={\setbox\matrixcellbox=\hbox\bgroup\strut},
 execute at end
 node={\egroup\eqmakebox[\tikzmatrixname\the\pgfmatrixcurrentcolumn][c]{\copy\matrixcellbox}}}}},
}  
\newcommand{\red}{\textcolor{red}}
\newcommand{\white}{\textcolor{white}}
\newcommand{\blue}{\textcolor{blue}}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,inner sep=0pt,outer sep=0pt,
    nodes={draw,inner sep=5pt,font=\sffamily,inner xsep=5pt},
    column sep=-\pgflinewidth/2,row sep=-\pgflinewidth/2,
    center align per column/.list={1,2,3},
    ampersand replacement=\&] (mat) {
        \shortstack{\\Number of times\\ Ben says,\\ "Power Ranger"} \& \shortstack{\rule{0pt}{9pt}\\number$\times$ gap\\\rule{0pt}{9pt}}\& \shortstack{Number of fries\\ Connor eats in total}\\ %%%%%my hack for attempting to make the top line equal.   
          $0$ \& $0$ \&{8} \\
          1 \& {} \& 13\\ 
          {} \& {} \& 18\\ 
          {}\& {} \&23 \\
          };
 \path[every node/.style={font=\sffamily\small},->,>=latex,line width =1pt]
    (mat-2-1.east-|mat.east) edge[draw=blue,bend left=110,min distance=1.5cm] (mat-3-2.east-|mat.east)
    ([yshift=-1mm]mat-5-2.south) edge[bend right]node[below]{\red{add ? each time}}([yshift=-1mm]mat-5-3.south)
    ;
\path[nodes={circle,anchor=west,inner sep=10pt,draw,semithick,midway}] 
    (mat-2-2.south-|mat.east) -- (mat-3-2.north-|mat.east) node{\blue{}}
    ;
\end{tikzpicture}
\end{document}

以下是我得到的结果:

桃金娘柱

请帮我将第一行(所有三列)的顶线对齐

答案1

让我扩展我的评论来回答:

  • 答案仅关注矩阵,因此序言仅减少到必要的库matrix
  • 单元格的宽度由 预先确定text width=7em,如果需要,可以通过在 中添加选项来分别适应每一列matrix,例如:
column 2/.append style = {nodes={text width=6ex}}
  • 第一行的高度由 手动确定row 1/.append style = {nodes={minimum height=3\baselineskip}}
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
    \begin{tikzpicture}[
> = latex,
    thick,
                        ]
\matrix (m) [matrix of nodes,
             nodes in empty cells,
             inner sep=0pt, outer sep=0pt,
             nodes = {draw, thin, minimum height=4ex, text width=7em, 
                      font=\sffamily\linespread{0.84}\selectfont,
                      align=center,
                      inner ysep=0pt, inner xsep=5pt, anchor=center},
            column sep=-\pgflinewidth/2,
            row sep=-\pgflinewidth/2,
            row 1/.append style = {nodes={minimum height=3\baselineskip, fill=gray!10}}
            ] 
{
Number of times Ben says, "Power Ranger"
    &   number$\times$ gap
        &   Number of fries Connor eats in total      \\ 
0   & 0 & 8         \\
1   &   & 13        \\
    &   & 18        \\
    &   & 23        \\
};
\node (c) [circle, draw, inner sep=8pt, semithick, right] at (m-2-3.south east) {};
\draw[blue, ->]  
    (c.north -| m.east) to[bend left=110,min distance=13mm] (c.south -|  m.east);
\draw[->, shorten >=5pt, shorten <=5pt]
    (m-5-2.south) to [bend right]
            node[below,text=red] {add ? each time}
    (m-5-3.south);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

添加如下内容:

nodes={draw,inner sep=5pt,font=\sffamily,inner xsep=5pt,
%inner sep=0pt,   outer sep=0pt,
%minimum width=1.9em,
% New: 
text height=4\ht\strutbox,
text depth=\dp\strutbox,
text width =11\ht\strutbox,
align=center,
}, 

相关内容