9

When em unit is used in text width style directive, resulting label shifts ever so slightly to the left from its center position directed by preceeding text centered.

When using cm unit, label is exactly centered. What am I doing wrong?

MWE with em:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\tikzstyle{every node}=[draw=black, thin, minimum height=3em, font=\small]

\begin{document}

\begin{tikzpicture}[
    supervisor/.style={%
        text centered, text width=12em,
        text=black
    },
    teammate/.style={%
        text centered, text width=12em,
        text=black
    },
    subordinate/.style={%
        grow=down,
        xshift=-3.2em, % Horizontal position of the child node
        text centered, text width=12em,
        edge from parent path={(\tikzparentnode.205) |- (\tikzchildnode.west)}
    },
    level1/.style ={level distance=4em,anchor=west},
    level2/.style ={level distance=8em,anchor=west},
    level3/.style ={level distance=12em,anchor=west},
    level4/.style ={level distance=16em,anchor=west},
    level 1/.style={%
        edge from parent fork down,
        sibling distance=14em,
        level distance=5em
    }
]
    \node[anchor=south,supervisor](super){Supervisor\\Supervisory position\\Location}[]

    child{node [teammate] {Teammate6\\Position4\\Location4}
        child[subordinate,level1] {node {Subordinate1}}
        child[subordinate,level2] {node {Subordinate2}}}
    % - why is this comment required for proper rendering of the tree???
    child{node [teammate] {Teammate7\\Position5\\Location5}
        child[subordinate,level1] {node {First\\Subordinate}}
        child[subordinate,level2] {node {Subordinate2}}
        child[subordinate,level3] {node {Third\\Teammate}}
        child[subordinate,level4] {node {Longtext-\\teammate}}};

\end{tikzpicture}
\end{document}

MWE with cm:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\tikzstyle{every node}=[draw=black, thin, minimum height=3em, font=\small]

\begin{document}

\begin{tikzpicture}[
    supervisor/.style={%
        text centered, text width=4cm,
        text=black
    },
    teammate/.style={%
        text centered, text width=4cm,
        text=black
    },
    subordinate/.style={%
        grow=down,
        xshift=-3.2em, % Horizontal position of the child node
        text centered, text width=4cm,
        edge from parent path={(\tikzparentnode.205) |- (\tikzchildnode.west)}
    },
    level1/.style ={level distance=4em,anchor=west},
    level2/.style ={level distance=8em,anchor=west},
    level3/.style ={level distance=12em,anchor=west},
    level4/.style ={level distance=16em,anchor=west},
    level 1/.style={%
        edge from parent fork down,
        sibling distance=14em,
        level distance=5em
    }
]
    \node[anchor=south,supervisor](super){Supervisor\\Supervisory position\\Location}[]

    child{node [teammate] {Teammate6\\Position4\\Location4}
        child[subordinate,level1] {node {Subordinate1}}
        child[subordinate,level2] {node {Subordinate2}}}
    % - why is this comment required for proper rendering of the tree???
    child{node [teammate] {Teammate7\\Position5\\Location5}
        child[subordinate,level1] {node {First\\Subordinate}}
        child[subordinate,level2] {node {Subordinate2}}
        child[subordinate,level3] {node {Third\\Teammate}}
        child[subordinate,level4] {node {Longtext-\\teammate}}};

\end{tikzpicture}
\end{document}

Edit:

This is sounding like a bug, as if I add different font sizes to the label as follows: {Teammate6\\\scriptsize Position4\\\scriptsize Location4} then line spacing varies too.

ajeh
  • 2,572
  • I think it is because 4cm is larger than 12em. So, when you specify 4cm, the "box" into which the text is placed is lager and hence the boxes are larger and hence things shift. Try changing from 4cm to 3cm and see the effect. – Peter Grill Feb 12 '14 at 20:17
  • That does not sound relevant, as 12em is just an arbitrary number, as is 4cm. Changing to for instance 8em still keeps label off center, while changing to 3cm does not. Ultimately, I am not yet aware of any reasons em units should behave any different form cm. – ajeh Feb 12 '14 at 20:48
  • 3
    The problem is that you're using em for setting the width and it's relative to the normal font, but then you use \small to typeset the entries and it has a different em width. I get perfect centering if I say \small before the tikzpicture (of course the rectangles are smaller). – egreg Feb 12 '14 at 20:49
  • Oh... this is correct! What's the solution? I need small font... – ajeh Feb 12 '14 at 20:50
  • @ajeh The solution is to use a font independent unit. – egreg Feb 12 '14 at 20:51
  • That would prevent me from calculating required box width when generating tex file depending on data going into the labels :( This is sounding like a bug, as no matter what font is used for the label, the box size is box size, it's fixed and off center label should not occur. – ajeh Feb 12 '14 at 20:52
  • @ajeh How did you compute the 12em? – egreg Feb 12 '14 at 21:02
  • Just picked an arbitrary number that produced satisfactory result for the length of sample text going into the labels. Should it matter? em is width of M in the current font, thus just another unit of measure, constant within the document. – ajeh Feb 12 '14 at 21:12
  • @ajeh em isn't always the width of an M, it is whatever width the font designer specifies, and it isn't constant within the document as (as your example shows) it has different values in different places, depending on the font. – David Carlisle Feb 12 '14 at 21:39
  • Is it still a constant for the current font? – ajeh Feb 12 '14 at 21:55
  • "it is whatever width the font designer specifies" Correct, therefore when only one font is used, 1em is still a constant. Why would it vary in different places without explicit change of fonts? – ajeh Feb 12 '14 at 22:10
  • @ajeh: please note that saying \small is somehow an explicit change font (you change the the font size), hence if you really want to have your picture independent from the font changes, replace em with cm or mm. Somehow it has already been stated in the comments. Please also note that 12em is some number x with \normalfont size, then it is x-e for \small font size; using cm does not prevent you to compute the box width, even if it would be better to let TikZ compute it for you: it does it normally, hence let it work. – Claudio Fiandrino Feb 13 '14 at 07:23

2 Answers2

2

It's a bug corrected in TiKZ 3.00.

With TiKZ 2.10 nodes with text width measured in cm produces:

enter image description here

while with em

enter image description here

With TikZ 3.0 both codes produce first figure.

Ignasi
  • 136,588
  • Pardon my ignorance, I am using TL 2013 updated to this date. Can I get 3.0 with it? I hope it also corrects vertical mis-alignment of the lines in labels when different text sizes are used for different lines... – ajeh Feb 13 '14 at 14:02
  • 1
    @ajeh: I'm a MiKTeX user so I can't help with TL. But TeX.SX can help you! Look, for example, at how can i update tikz pgf or how to install a current version of tikz. You can find TikZ 3.0 in its sourceforge page. – Ignasi Feb 13 '14 at 15:21
  • Maybe one more pointer: what is the specific package name for Tikz? I see pgf, pgfplots, tikz-cd for example, but not specifically tikz. If I knew the package name for tikz itself, I could at least determine which version I have :( – ajeh Feb 13 '14 at 15:39
  • 1
    @ajeh: official name in CTAN is pgf. – Ignasi Feb 13 '14 at 16:13
  • @Ignazi: I wonder if v.3 fixed another issue. Line interval is varying in the screen print above (at a certain zoom level on my screen line 2 is 2.5mm from line 1 and line 3 is 3.5mm from line 2). Could you check in v.3 if you have time? Much appreciated! – ajeh Feb 13 '14 at 20:30
  • @ajeh: I don't understand your question but some precision problems are related with viewers and not TiKZ. Did you tested your output in several viewers? Did you print it? – Ignasi Feb 14 '14 at 08:14
  • Thing is that this document is not destined for printing, but for viewing in Acrobat Reader, and that's where I see that line interval within the labels is different for 2nd and 3d lines. – ajeh Feb 14 '14 at 14:17
0

According to the correct answer by Ignasi, this is a bug in 2.10.

Till submitted v.3.0 to CTAN end of Dec 2013 and received a reply, that due to complexity of the package it would take them time to validate it. Validation is still on-going: v.3 discussion on sourceforge

ajeh
  • 2,572