Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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

1 Answer 1

up vote 7 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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