# Wrong vertical text position with Tikz

This is driving me nuts. The "Job5" and "Job6" text is slightly verticaly higher than the rest of text.

I'm suspecting the "inner sep", but I need that for the fit.

How can I re-adjust the text?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}[auto,
singlejob/.style = {rectangle, minimum height = 1cm, minimum width = 2cm, fill=blue!20, thick, outer sep=0pt, inner sep=0pt, align=center, anchor=mid, text centered},
doublejob/.style = {rectangle, minimum height = 1cm, minimum width = 4.1cm, fill=blue!20, thick, inner sep = 0pt, align=center, anchor=mid, text centered}
]
\matrix[column sep=0.1cm, row sep=0.1cm, ampersand replacement=\& ] {
\node[rectangle, minimum height = 1cm, minimum width = 1cm ] { $Resc_1$ };\&
\node[draw, singlejob] {$Job_1$};\& \node[draw, singlejob] {$Job_2$};\&
\node[draw, singlejob] {$Job_3$};\& \node[draw, singlejob] {$Job_4$};\\

\node[rectangle, minimum height = 1cm, minimum width = 1cm ] { $Resc_2$ };\&
\node[singlejob] (j51) {}; \& \node[singlejob] (j52) {}; \&
\node[singlejob] (j61) {}; \& \node[singlejob] (j62) {}; \\
};
\node[draw,doublejob] (outer) [fit=(j51) (j52)] {$Job_5$};
\node[draw,doublejob] (outer) [fit=(j61) (j62)] {$Job_6$};
\end{tikzpicture}
\end{document}


Smaller example (added by Andrew Stacey). The problem appears to be to do with how the fit library expands its nodes:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

\begin{tikzpicture}
\node[draw,inner sep=0pt,fit={(0,0) (1,-2)}] (a) {a};
\node[draw,minimum width=1cm,minimum height=2cm,anchor=north west] (b)
at (1.5,0) {a};
\draw[ultra thick,red] (a.base) -- (b.base);
\end{tikzpicture}

\end{document}


-
I narrowed it down to the fit library and so came up with a smaller example. I edited it in (since it's just another example and not an "answer" by any stretch of the imagination). Hope that's okay. –  Loop Space Oct 26 '12 at 14:21
@AndrewStacey Yup, no problem. Thx for the effort. –  Let_Me_Be Oct 26 '12 at 14:23
A workaround that is hinted at in the documentation is to use label=center:<text> for the node text in the fitted node. –  Jake Oct 26 '12 at 14:31
@Jake Cool, that seems to work. –  Let_Me_Be Oct 26 '12 at 14:37

I don't know if this is a bug or a feature. The fit node works by figuring out the centre of where the node should be to encompass all the points it is given and then setting the size of its text box to the right size so that the resulting node will include all of the given points. The issue comes in setting the height and depth of the text box. These have to sum up to the right size but there's scope for adjusting them relative to each other. Once the height is set then obviously the depth is the desired total height minus the set height, so the crucial line is the following from the fit library code:

  /tikz/text height/.expanded=\the\pgf@y-.5\dp\pgfnodeparttextbox,


This says to set the height of the box to be half the desired height (\the\pgf@y) minus half of the depth of the actual box (-.5\dp\pgfnodeparttextbox). After testing a few values, I found that I got better alignment if I used:

  /tikz/text height/.expanded=\the\pgf@y+.5\ht\pgfnodeparttextbox


-.5\dp\pgfnodeparttextbox,

(This wasn't a completely random choice, it was suggested by looking at how the rectangle shape computes its anchors.)

Fortunately, right after these statements then TeX evaluates the key every fit. So we can hook in here and substitute the formulae.

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{fit}

\makeatletter
\tikzset{
every fit/.style={
text height/.expanded=\the\pgf@y+.5\ht\pgfnodeparttextbox -.5\dp\pgfnodeparttextbox,
}
}
}
\makeatother

\begin{document}

\begin{tikzpicture}[every node/.style={outer sep=0pt}]
\node[draw,inner sep=0pt,fit={(0,0) (1,-2)}] (a) {a};
\node[draw,minimum width=1cm,minimum height=2cm,anchor=north west] (b) at (1.5,0) {a};
\node[draw] (c) at (3,-1) {a};
\draw[ultra thick,green] (a.center) -- (b.center) -- (c.center);
\draw[ultra thick,red] (a.base) -- (b.base) -- (c.base);
\end{tikzpicture}

\begin{tikzpicture}[every node/.style={outer sep=0pt}]
\node[draw,inner sep=0pt,fit={(0,0) (1,-2)}] (a) {bq};
\node[draw,minimum width=1cm,minimum height=2cm,anchor=north west] (b) at (1.5,0) {bq};
\node[draw] (c) at (3,-1) {bq};
\draw[ultra thick,green] (a.center) -- (b.center) -- (c.center);
\draw[ultra thick,red] (a.base) -- (b.base) -- (c.base);
\end{tikzpicture}

\begin{tikzpicture}[every node/.style={outer sep=0pt}]
\node[draw,inner sep=0pt,fit={(0,0) (1,-2)}] (a) {a};
\node[draw,minimum width=1cm,minimum height=2cm,anchor=north west] (b) at (1.5,0) {a};
\node[draw] (c) at (3,-1) {a};
\draw[ultra thick,green] (a.center) -- (b.center) -- (c.center);
\draw[ultra thick,red] (a.base) -- (b.base) -- (c.base);
\end{tikzpicture}

\begin{tikzpicture}[every node/.style={outer sep=0pt}]
\node[draw,inner sep=0pt,fit={(0,0) (1,-2)}] (a) {bq};
\node[draw,minimum width=1cm,minimum height=2cm,anchor=north west] (b) at (1.5,0) {bq};
\node[draw] (c) at (3,-1) {bq};
\draw[ultra thick,green] (a.center) -- (b.center) -- (c.center);
\draw[ultra thick,red] (a.base) -- (b.base) -- (c.base);
\end{tikzpicture}

\end{document}


With before and after pictures:

-