21

I wanted to draw an almost empty matrix of nodes with TikZ similar to the left most one below. All nodes would have same size (through minimum width and minimum height) and no separation between columns and rows. But when I include a text which needs two lines I obtain the central matrix and, if the text just needs one line the right most one. I don't know what's wrong. Do you? I'm using CVS version of TikZ.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix}

\tikzset{
proces/.style={draw, minimum height=15mm, minimum width=2cm, outer sep=0pt, text width=1.5cm, align=center}
}

\begin{document}
\begin{tikzpicture}

\matrix (A) [matrix of nodes,nodes=proces,nodes in empty cells] {
 & \\
 & \\
};

\begin{scope}[xshift=4.5cm]
\matrix (A) [matrix of nodes,nodes=proces,nodes in empty cells] {
 & OOO OOO \\
 & \\
};
\end{scope}

\begin{scope}[xshift=9cm]
\matrix (B) [matrix of nodes,nodes=proces,nodes in empty cells] {
 & A \\
 & \\
};
\end{scope}

\end{tikzpicture}
\end{document}

enter image description here

3
  • 2
    Weigh anchor and hoist sail for TikZland. Seriously, try nodes={anchor=center}. The style matrix of nodes automatically sets the anchor to base and I suspect (only suspect) that this is the cause of the shifts. Oct 24, 2012 at 17:54
  • @AndrewStacey Verified. Godspeed...
    – percusse
    Oct 24, 2012 at 20:15
  • @AndrewStacey: Perfect! Thank you! Do you want to write an answer?
    – Ignasi
    Oct 25, 2012 at 6:32

1 Answer 1

28

What's happened here is that the matrix of nodes has quietly set anchor=base on each of your matrix subnodes. The problem with this is that when the node has text in it then the base anchor is not in the same place as the center anchor. So your nodes are lining up so that their bases are in a line but this means that their centers (and thus their boundary) are not.

The simplest solution is to put nodes={anchor=center}, or you could add it to the proces style, after the matrix of nodes to revert this change.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix}

\tikzset{
  process base/.style={
    draw,
    minimum height=15mm,
    minimum width=2cm,
    outer sep=0pt,
    text width=1.5cm,
    align=center
  }
}


\begin{document}
\begin{tikzpicture}[
  proces/.style={
    process base,
  }
]

\matrix (A) [matrix of nodes,nodes=proces,nodes in empty cells] {
 & \\
 & \\
};

\begin{scope}[xshift=4.5cm]
\matrix (B) [matrix of nodes,nodes=proces,nodes in empty cells] {
 & OOO OOO \\
 & \\
};
\end{scope}

\begin{scope}[xshift=9cm]
\matrix (C) [matrix of nodes,nodes=proces,nodes in empty cells] {
 & A \\
 & \\
};
\end{scope}

\foreach \lbl in {A,B,C} {

\draw[line width=3mm,green] (\lbl-1-1.base) -- (\lbl-1-2.base);
\draw[ultra thick,red] (\lbl-1-1.center) -- (\lbl-1-2.center);

}
\end{tikzpicture}


\begin{tikzpicture}[
  proces/.style={
    process base,
    anchor=center,
  }
]


\matrix (A) [matrix of nodes,nodes=proces,nodes in empty cells] {
 & \\
 & \\
};

\begin{scope}[xshift=4.5cm]
\matrix (B) [matrix of nodes,nodes=proces,nodes in empty cells] {
 & OOO OOO \\
 & \\
};
\end{scope}

\begin{scope}[xshift=9cm]
\matrix (C) [matrix of nodes,nodes=proces,nodes in empty cells] {
 & A \\
 & \\
};
\end{scope}


\foreach \lbl in {A,B,C} {

\draw[line width=3mm,green] (\lbl-1-1.base) -- (\lbl-1-2.base);
\draw[ultra thick,red] (\lbl-1-1.center) -- (\lbl-1-2.center);

}

\end{tikzpicture}

\end{document}

aligned anchors

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .