4

I wonder why node's text of node positioned by fit is not centered?

Edit: As suggested Peter Grill in his comment, I change previous MWE with more realistic example:

\documentclass[tikz,border=3mm]{standalone}
        \usetikzlibrary{chains,fit,positioning}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \begin{document}
%%%% se-fit
    \begin{tikzpicture}[
every label/.append style = {label distance=1mm, inner ysep=0mm,
                             font=\scriptsize\sffamily, text=blue!50!black, 
                             align=center},
            node distance = 0mm,
              start chain = going right,
          N/.style 2 args = {name=n#1,
                             minimum width=#2,
                             draw, text height=2ex, text depth=1ex,
                             inner sep=2mm, outer sep=0mm, on chain}
                        ]
\node[N={1}{11mm},draw=none,fill=blue!10]   {};
\node[N={2}{11mm},draw=none,fill=blue!20]   {};
    \node[draw,inner sep=0pt, outer sep=0pt,
          fit=(n1) (n2),
          label=above left:{bits:},
          label=above:144]                  {preamble};
\node[N={3}{11mm},fill=blue!20,right=of n2,
      label=above:8]                        {signal};
\node[N={4}{11mm},fill=blue!20,
      label=above:8]                        {service};
\node[N={5}{11mm},fill=blue!20,
      label=above:12]                       {length};
\node[N={5}{7mm},fill=blue!20,
      label=above:16]                       {FCS};
\node[N={6}{55mm},
      label=above:<length> octets]          {payload};
    \end{tikzpicture}
%
    \end{document}

Is this a features or it is bug? I wonder, why node, when is placed by fit option behavior differently when is positioned at some coordinate.

enter image description here

Put a node text as label text at center node is not convenient since real pictures have many labels with common option settings as show MWE above and also many similar construction of nodes (not shown in MWE).

The Peter Grill suggestion to use another node for text solve the problem, but my primary question remain unanswered.

  • It is interesting especially since the documentation notes that it is supposed to be difficult to place the text anywhere other than the centre and suggests various workarounds for those cases. So it looks as if it is not expected to behave like this? Might it be an undocumented feature (aka bug)? – cfr Nov 6 '14 at 0:02
  • Note that the problem can be reproduced with the following, simpler MWE: \documentclass[tikz,border=3mm]{standalone} \usetikzlibrary{fit} \begin{document} \begin{tikzpicture} \node [fill=blue, text opacity=.5] (a) {aby}; \node [draw, fit=(a)] (c) {aby}; \end{tikzpicture} \end{document} – cfr Nov 6 '14 at 0:14
  • I'm not really seeing why label=center:preamble isn't a good option... (Even though I agree that the behaviour is at least odd.) Is it just to avoid having to override default options for styling other labels? – cfr Nov 6 '14 at 2:32
5

I do not know why the \node text is not centered by default, but you can use the label=center:my text option to place the text in the same node:

enter image description here

Alternatively you can place a separate \node after you have your fit.


Code:

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\usetikzlibrary{fit,positioning}

\begin{tikzpicture}[
    node distance=0mm,
    every node/.style={inner sep=0pt, outer sep=0pt,font=\large}
    ]
    \node[minimum size=3em,fill=blue]           (a) {};
    \node[minimum size=3em,fill=red,right=of a] (b) {};
    \node[draw=brown, thick,fit={(a) (b)}, label=center:my text]   (c) {};
\end{tikzpicture}
\end{document}

Code: Separate \node:

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\usetikzlibrary{fit,positioning}

\begin{tikzpicture}[
    node distance=0mm,
    every node/.style={inner sep=0pt, outer sep=0pt,font=\large}
    ]
    \node[minimum size=3em,fill=blue]           (a) {};
    \node[minimum size=3em,fill=red,right=of a] (b) {};
    \node[draw=brown, thick,fit={(a) (b)}]      (c) {};
    \node at (c) {my text};
\end{tikzpicture}
\end{document}
  • Peter Grill, this possibility I mentioned in my question. Since this mentions contain some tipping errors, now I correct it, maybe was not clear enough. In simple cases as it is in given MWE I use this approach. But in picture with many nodes constructed as shown in MWE and many other labels with style common for labels, I looking for more convenient solution. Use separate node as you show in second example is the way to go, but I still wonder, why I should use another node to put a text in center of underlying node? Is this a features of nodes positioning by fit options? – Zarko Nov 5 '14 at 20:02
  • @Zarko: Sorry, I am not understanding. I suggest you update the MWE you provided with a more realistic test case where using either of these solutions would be inconvenient. – Peter Grill Nov 5 '14 at 20:05
  • I try to update my MWE with real picture, but arise some new problems regarding labels and nodes in chain. I need resolve the problem, why this work in my document and not in MWE. – Zarko Nov 6 '14 at 1:14

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.