[Tex/LaTex] Aligning matrix nodes in tikz

matricestikz-pgf

Another question about aligning nodes using matrices in tikz.
Consider the following:

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

\begin{document}
\begin{tikzpicture}

\matrix (m)[matrix of nodes, column  sep=5mm,row  sep=3mm, align=center,
nodes={rectangle,draw, text width = 2cm} ]{
                & \node{row1-2};    &                        \\
\node{row2-1};      &               & \node{row2-3 long in two rows};\\
};

\draw[->] (m-1-2) |- (m-2-1);
\draw[->] (m-1-2) |- (m-2-3);

\end{tikzpicture}
\end{document}

which yields me this:

Unaligned nodes

How can the boxes in the second row be aligned properly?

Best Answer

Figured it out. Just add anchor=center to the node definition, and this does the trick:

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

\begin{document}
\begin{tikzpicture}

\matrix (m)[
    matrix of nodes, column sep=5mm, row sep=3mm, align=center,
    nodes={rectangle, draw, text width=2cm, anchor=center}
]{
                  & \node{row1-2};  &                                 \\
  \node{row2-1};  &                 & \node{row2-3 long in two rows}; \\
};

\draw[->] (m-1-2) |- (m-2-1);
\draw[->] (m-1-2) |- (m-2-3);

\end{tikzpicture}
\end{document}

enter image description here