# Text in corner of tikz rectangle

How do I write some text in the bottom-right hand corner of a rectangle so that the text is as far to the right and down as possible without exceeding the bounds of the rectangle?

The rectangle is defined using fit, like so:

\node[draw=black,inner sep=2mm,thick,rectangle,fit=(a) (b)] {};

-
Do you need the text to be flowed, or are you using explicit \\s? –  Brent.Longborough Aug 30 '10 at 11:26
I just need one line of equation text –  Neil G Aug 30 '10 at 11:34

While driving up from Wales to London, I realised that the tabular was redundant for the question as asked, and when I arrived, I found that @Caramdir had justly taken me to task. Here is a completely new answer, which addresses a bit more than the original question.

For a single line of text, all you need to do is align the node:

\begin{tikzpicture}
\node at (0,0) (a) {a};
\node at (5,5) (b) {b};
\node[draw=black,inner sep=2mm,thick,rectangle,fit=(a) (b)] (x) {};
\node[anchor=south east,inner sep=1pt] at (x.south east)
{$H \Psi = \nabla \Psi$};
\end{tikzpicture}


For multiple lines, things get a bit more elephantine, though the next release of Tikz will have, I believe, constructs like align=right for text. Meanwhile, one way is to use a tabular:

\begin{tikzpicture}
\node at (0,0) (a) {a};
\node at (5,5) (b) {b};
\node[draw=black,inner sep=0pt,thick,rectangle,fit=(a) (b)] (x) {};
\node[anchor=south east] at (x.south east) {%
\begin{tabular}{r}
$H \Psi = \nabla \Psi$\\
$J \Phi = \nabla \Phi$
\end{tabular}
};
\end{tikzpicture}


You may want to tweak the tabular parameters to alter the space between the right/bottom of the text and the corner of the rectangle...

-
Is there any reason for the tabular environment? –  Caramdir Aug 30 '10 at 12:50
Only my stupidity. I'll fix it when I've finished dinner. –  Brent.Longborough Aug 30 '10 at 18:27
This works beautifully. Thank you. –  Neil G Aug 30 '10 at 18:54
@Neil: hope you used the simpler one... @Caramdir: I kmew there was a use for tabular in the multiline case. Was I right? –  Brent.Longborough Aug 30 '10 at 18:58