2

I'm trying to build a diagram of variable-sized lumps aligned to a regular underlying grid. Here's my MWE:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,chains}
\begin{document}
\begin{tikzpicture}[%
    start chain=c going right,
    node distance=40mm,
    every node/.style={%
        anchor=center,
        on grid,
        align=center,
        text width=30mm,
        minimum height=6ex,
        outer sep=0mm,
        inner sep=0mm,
    },
]
\node [on chain] (a) {} ;
\node [on chain] (b) {} ;
\node [on chain] (c) {} ;
\node [on chain] (d) {} ;
\node at (a) [draw] {First} ;
\node [draw,fit=(b) (b)] {Second} ; 
\node [draw,fit=(c) (d)] {Third} ; 
\end{tikzpicture} 
\end{document}

And here's part of the output:

enter image description here

Now, as you can see, the baselines of the two pieces of text are severely misaligned, although their enclosing boxes are well aligned.

(The "Third" item, whose output is not shown, is aligned with the second; it is simply intended to demonstrate that this isn't an artifact of fitting to a single node.)

What, if anything, am I doing wrong, and how do I correct it?

4

I don't think that you are doing anything wrong; the positioning of text in fitting nodes requires special care.

The following variation on your code shows that, for fitting nodes, the text is not vertically placed in the center anchor of the node:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}
\coordinate (a) at (0,0);
\coordinate (b) at (3,2);
\node[
  inner sep=0pt,
  outer sep=0pt,
  draw,
  fit={(a) (b)},
  label={[red]center:center}
  ] {center} ; 
\node[
  inner sep=0pt,
  outer sep=0pt,
  draw,
  align=center,
  text width=3cm,
  minimum height=2cm,
  label={[red]center:center}
  ]
  at (5,1) 
  {center} ; 
\end{tikzpicture} 
\end{document}

The result:

enter image description here

The PGF manual (on page 630) warns about the text positioning of the text in fitting nodes, although I admit it might be misleading because it seems to suggest that the text will be placed at the center anchor, when it clearly isn't (as my example above shows or even as the example on the manual if one modifies the code and places "box" at the proper center anchor of the fitting node):

The above means that, generally speaking, if the node contains text like box in the above example, it will be centered inside the box. It will be difficult to put the text elsewhere, in particular, changing the anchor of the node will not have the desired effect.

The ensuing sentence mentions the possible solutions:

Instead, what you should do is to create a node with the fit option that does not contain any text, give it a name, and then use normal nodes to add text at the desired positions. Alternatively, consider using the label or pin options.

In the following example I used your code iluustrating the use of label and of an auxiliary node to place the text at the desired location:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,chains}
\begin{document}
\begin{tikzpicture}[%
    start chain=c going right,
    node distance=40mm,
    every node/.style={%
        anchor=center,
        on grid,
        align=center,
        text width=30mm,
        minimum height=6ex,
        outer sep=0mm,
        inner sep=0mm,
    },
]
\node [on chain] (a) {} ;
\node [on chain] (b) {} ;
\node [on chain] (c) {} ;
\node [on chain] (d) {} ;
\node at (a) [draw] {First} ;
\node [draw,fit=(b),label={center:Second}]  {} ; 
\node [draw,fit=(c) (d)] (fitcd) {} ;
\node at (fitcd.center) {Third}; 
\end{tikzpicture} 
\end{document}

The result:

enter image description here

  • Thanks for that idea, Gonzalo. In my case, I don't specifically align the text vertically. Is the default text anchor different for a non-fitted node? It looks like the text is not actually centred in the fit box. – Brent.Longborough Aug 7 '15 at 19:37
  • @Brent.Longborough Yes, as my examples show, it isn't (even if the documentation seems to indicate it is). – Gonzalo Medina Aug 7 '15 at 19:38

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.