1

I am trying to get the column headers to match up when I \shortstack{} what is in the cell. I would like the top line to be the same and the only hack way I can try to make it the same is to introduce fake spacing with \rule but I'm sure this is not the best way. Can someone help me to line up the top line of the header row in the matrix.

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}

Here is what I get:

mytrytocolumn

Please help me line up the top line for the first row(all three columns)

Zarko
  • 296,517
E.Yu
  • 337
  • TikZ matrix work different than standard table, i.e. nodes heights are not automatic adopted to the node with maximum height. You need to do this manually. For example with row 1/.style = {nodes={minimum height=..., ...} – Zarko Apr 07 '20 at 17:46
  • Alternatively (and equivalently) you can put the headers into \parboxes and specify the heights and alignments using the optional arguments. You might need [anchor=base] for the nodes. – John Kormylo Apr 07 '20 at 19:48
  • @Zarko can you tell me where to put the instructions row 1/.style = {nodes={minimum height=..., ...} does this go in the options for nodes? – E.Yu Apr 07 '20 at 22:27
  • @E.Yu, see my answer below. – Zarko Apr 08 '20 at 00:12
  • you are awesome. Thanks for fixing some of my other ugliness too. so grateful. – E.Yu Apr 08 '20 at 00:18
  • @Zarko Hi Zarko, I would like to repeat tables like this but with different headings, is there a certain advantage to renaming them or can I keep them all called (m)? – E.Yu Apr 08 '20 at 01:06
  • @E.Yu, you can name matrix arbitrary. Names are always valid only local, so you can use the same name in all tables. – Zarko Apr 08 '20 at 05:42

2 Answers2

1

Let me extend my comment to answer:

  • answer is focused only to matrix, therefore the preamble is reduced only to necessary library matrix
  • width of cells is determined in advance by text width=7em, if necessary it can be adopted to each column separately by adding options to matrix, for example:
column 2/.append style = {nodes={text width=6ex}}
  • height of the first row is determined manually by 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}

enter image description here

Zarko
  • 296,517
0

Add something like:

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,
}, 
cis
  • 8,073
  • 1
  • 16
  • 45