5

While laying out a bunch of nodes with a TikZ matrix, I noticed slight vertical mis-alignment of nodes. It seems that words with “tall” letters (p, b, l) shift the box vertically up or down. This does not happen if the node contains only a word without those letters. E.g. test will appear shifted when placed beside a node with uiae as text.

Here's a minimum example:

\usetikzlibrary{matrix}
\begin{tikzpicture}

\matrix[
    matrix of nodes,
    column sep=1ex, row sep=1ex,
    nodes={
        draw,
        minimum height=2em,
        text width=30mm,
    },
] (matrix-optimization) {
    Encodings &
    Problems &
    Algorithms \\
%
    Selection &
    uiae &
    pppp \\
};
\end{tikzpicture}

The nodes Problems and pppp will appear shifted. How can I fix that? I'd like all nodes to have the same height (text vertically centered) and to be perfectly aligned.

  • Looks like I can get the desired effect with text height=1em, text depth=1ex}. Not sure about the implications though – knittl Jul 20 '12 at 12:20
8

The problem is what you said, nodes contain letters with different height and depth and they adjust and align according to it. TiKZ manual explains it in tutorial "Diagrams as simple graphs" and "Cell Pictures".

If you want all nodes aligned but without aligning words you can change nodes anchor with anchor=south.

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[
    matrix of nodes,
    column sep=1ex, row sep=1ex,
    nodes={
        draw,
        minimum height=2em,
        text width=30mm,
        anchor=south
    },
] (matrix-optimization) {
    Encodings &
    Problems &
    Algorithms \\
%
    Selection &
    uiae &
    pppp \\
};
\end{tikzpicture}
\end{document}

enter image description here

But if you want nodes and text aligned you need to fix a certain depth and height to nodes.

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[text height=1.5ex,text depth=.25ex,
    matrix of nodes,
    column sep=1ex, row sep=1ex,
    nodes={
        draw,
        minimum height=2em,
        text width=30mm
    },
] (matrix-optimization) {
    Encodings &
    Problems &
    Algorithms \\
%
    Selection &
    uiae &
    pppp \\
};
\end{tikzpicture}
\end{document}

enter image description here

After posting my answer I've found this previous question which suggest some other solutions: Problem with TikZ and vertical alignment of text inside nodes

0

Possibly a simpler solution is to place a \strut in each of the nodes with font=\strut:

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[
    matrix of nodes,
    column sep=1ex, row sep=1ex,
    nodes={
        draw,
        minimum height=2em,
        text width=30mm,
        anchor=south,
        font=\strut % <<---
    },
] (matrix-optimization) {
    Encodings &
    Problems &
    Algorithms \\
%
    Selection &
    uiae &
    pppp \\
};
\draw[red] (-5,.35) -- (5,.35);
\draw[red] (-5,-.525) -- (5,-.525);
\end{tikzpicture}
\end{document}

Example of aligning text in nodes with a strut

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.