Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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 at 20:15

3 Answers 3

up vote 5 down vote accepted

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}
share|improve this answer

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:

Output

share|improve this answer
    
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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.