3

The following MWE

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
  \matrix (A) [matrix of nodes, column sep=-\pgflinewidth, row sep=-\pgflinewidth, nodes={minimum height=3em, draw, anchor=center}] {
    foo & fooy\\
  };
\end{tikzpicture}
\end{document}

produces the following result: enter image description here I would like to align the cell boxes properly. The only solution I know for this is to add anchor=center to the node style, i.e.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
  \matrix (A) [matrix of nodes, column sep=-\pgflinewidth, row sep=-\pgflinewidth, nodes={minimum height=3em, draw, anchor=center}] {
    foo & fooy\\
  };
\end{tikzpicture}
\end{document}

However, this results in misaligned text (red guideline is for visualization purposes and is not part of the MWE): enter image description here

How can I align both the text and the cell boxes in the intended way?

  • Add \strut before or after foo: foo\strut. – Ignasi Dec 15 '17 at 11:58
4

According to the TikZ manual, in the "Tutorial: Diagrams as Simple Graphs", the key is to directly fix the height and depth of the nodes. That way, you force them to be aligned. The key words are text height and text depth

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
    \matrix (A)% 
    [matrix of nodes, 
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth, 
    nodes=
        {minimum height=3em, 
        draw, 
        text height=1.5ex,
        text depth=.25ex,
        anchor=center}
    ] {
    foo & fooy\\
    };

    \draw[red, very thin] (A-1-1.base) -- +(1.5,0);
    \draw[blue, very thin] (A-1-2.base) -- +(-1.5,0);
\end{tikzpicture}
\end{document}

On the left side of the picture, you can see the result when those keys are not specified, I place them side by side and draw the lines on the base for comparison

Solution comparison with previous picture

1

Just add key text depth=.25ex inside nodes options

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
  \matrix (A) [%
  matrix of nodes,
  column sep=-\pgflinewidth,
  row sep=-\pgflinewidth,
  nodes={minimum height=3em, draw, text depth=.25ex, anchor=center}
  ]
  {
    foo & fooy\\
  };
\end{tikzpicture}

\end{document}

UPDATE

If you need the matrix cells squared, you can use the code below

enter image description here

\begin{tikzpicture}

\tikzset{square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth,
    nodes={draw,
      minimum height=3em,
      anchor=center,
      text width=3em,
      text depth=.25ex,
      align=center,
      inner sep=0pt
    },
  },
  square matrix/.default=1cm
}

\matrix[square matrix]
  {
    foo & fooy\\
  };
\end{tikzpicture}
1

If you're ever dealing with text that may have varying height/depth, you can use \strut to ensure similar depth/height. Here's an example with \fbox:

enter image description here

\documentclass{article}

\begin{document}

\fbox{foo}\ \fbox{fooy}

\fbox{\strut foo}\ \fbox{\strut fooy}

\end{document}

In a similar vein, issue \strut with the components you want to have similar height/depth (no guessing required):

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
  \matrix (A) 
    [matrix of nodes, 
    column sep = -\pgflinewidth, 
    row sep = -\pgflinewidth, 
    nodes = {
      minimum height=3em, 
      draw, 
      anchor = center
    }] {
    \strut foo & \strut fooy \\
  };
\end{tikzpicture}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.