# tikz node align text to bottom of rectangle

How can I align the text of a node to the bottom of the rectangle shape without hard coding values? (centered horizontally but justified to the bottom)

\tikz[]{
\node[shape=rectangle, inner sep=8pt, minimum height=1.2cm, fill=black, text=white, font=\huge, text depth=-0.6cm] {test};
}\tikz[]{
\node[shape=rectangle, inner sep=8pt, minimum height=1.2cm, fill=black, text=white, font=\huge, text depth=0cm] {test};
}\tikz[]{
\node[shape=rectangle, inner sep=8pt, minimum height=1.2cm, fill=black, text=white, text depth=-0.6cm] {test};
}\tikz[]{
\node[shape=rectangle, inner sep=8pt, minimum height=1.2cm, fill=black, text=white, text depth=0cm] {test};
}


Obviously text depth can be hard coded but I would like it to automatically work for any given font and rectangle height.

-
Your question is unclear. What alignment do you want with letters like 'y' and 'A'? –  Paul Gaborit Jul 16 '12 at 11:44
also related: tex.stackexchange.com/questions/140496/… –  sdaau Mar 19 '14 at 20:15

If the text size is smaller than the minimum height you could try to use text height instead of minimum height:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\tikz[]{
\node[shape=rectangle, inner sep=0pt, fill=black, text=white,
font=\huge, text height=1.2cm] {test};
}

\tikz[]{
\node[shape=rectangle, inner sep=0pt, fill=black, text=white,
font=\huge, text height=1.2cm] {test yjp};
}

\tikz[]{
\node[shape=rectangle, inner sep=0pt, fill=black, text=white,
font=\huge, text height=1.2cm,text depth=0cm] {test yjp};
}

\end{document}

-

You can create a vertical box of a given height and put your text at the bottom of that box. This can be done with the following macro:

\def\bottom#1#2{\hbox{\vbox to #1{\vfill\hbox{#2}}}}


You can use the above macro inside a TikZ node. The "bad" part is that you have to give the desired height of that box, and that you have to take into account the inner sep (which would be added to the given height), and the minimum height (which has to be greater or equal, preferibly equal, to the sum of the given height plus the two inner seps).

The following MWE will make more clear what I mean, I hope:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\def\bottom#1#2{\hbox{\vbox to #1{\vfill\hbox{#2}}}}

\tikz[]{
\node[shape=rectangle, inner sep=1mm, minimum height=1.2cm, fill=black, text=white, font=\huge] {test};
}\tikz[]{
\node[shape=rectangle, inner sep=1mm, minimum height=1.2cm, fill=black, text=white, font=\huge] {\bottom{1cm}{test}};
}\tikz[]{
\node[shape=rectangle, inner sep=1mm, minimum height=1.2cm, fill=black, text=white] {test};
}\tikz[]{
\node[shape=rectangle, inner sep=1mm, minimum height=1.2cm, fill=black, text=white] {\bottom{1cm}{test}};
}
\end{document}


With the following result:

-
yes, but my goal is to avoid having to manually calculate things. Else I can just set text depth directly. In both cases if something changes I'll have to manually change all the values. In your case though there is a lot more overhead. –  AbstractDissonance Jul 16 '12 at 12:36

Possible : \parbox this macro takes an optional argument to set the height of the box.

\documentclass{article}
\usepackage{tikz}

\begin{document}

\tikz \node[shape          = rectangle,
inner sep      = 0pt, draw,
fill           = black,
text           = white,
minimum height = 2cm,
minimum width  = 4cm] {\parbox[b][2cm]{4cm}{Simple \huge{test}}};

\end{document}


-