tikz text location within node

I've looked around and surprisingly haven't found a clear answer to this question. I have some boxes I'm drawing with tikz. I would like the text (in this case D) for the bigbox boxes to be in the bottom right, without having to do anything to give any coordinates on a case by case basis. Instead the text is appearing in the center of the box. I don't seem to have control over that except through putting a lot of white space before the text.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.markings,shapes,arrows,fit}
\tikzstyle{box}=[draw, minimum size=2em, text width=4.5em, text centered]
\tikzstyle{bigbox}=[draw, inner sep=20pt]

\begin{document}
\begin{tikzpicture}
\node[box] (A) at ( -2,1) {$A$};
\node[box] (B) at ( 1,1) {$B$};
\node[box] (C) at ( 4,1) {$C$};
\node[bigbox, fit=(A)(B)(C)] (D) {D};
\end{tikzpicture}
\end{document}


You can add a label and then shift it to inside like

label={[shift={(-3ex,3ex)}]south east:#1}


in the definition of bigbox:

bigbox/.style={draw, inner sep=20pt,label={[shift={(-3ex,3ex)}]south east:#1}}


and then say bigbox=D in

\node[bigbox=D, fit=(A)(B)(C)] (D) {};


Full code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.markings,shapes,arrows,fit}
\tikzset{box/.style={draw, minimum size=2em, text width=4.5em, text centered},
bigbox/.style={draw, inner sep=20pt,label={[shift={(-3ex,3ex)}]south east:#1}}
}

\begin{document}
\begin{tikzpicture}
\node[box] (A) at ( -2,1) {$A$};
\node[box] (B) at ( 1,1) {$B$};
\node[box] (C) at ( 4,1) {$C$};
\node[bigbox=D, fit=(A)(B)(C),] (D) {};
\end{tikzpicture}
\end{document}


Edit

Here a label is drawn for the node with label={[shift={(-3ex,3ex)}]south east:#1} which is shifted and #1 is the content of the label. For more, refer to pgfmanual. As for the complex content, I hope the following is complex enough :-)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.markings,shapes,arrows,fit}
\tikzset{box/.style={draw, minimum size=2em, text width=4.5em, text centered},
bigbox/.style={draw, inner sep=20pt,label={[align=right,shift={(-1.5ex,3ex)}]south east:\llap{#1}}}
}

\begin{document}
\begin{tikzpicture}
\node[box] (A) at ( -2,1) {$A$};
\node[box] (B) at ( 1,1) {$B$};
\node[box] (C) at ( 4,1) {$C$};
\node[bigbox={$x^n + y^n = z^n$},label=south east:D, fit=(A)(B)(C)] (D) {};
\end{tikzpicture}
\end{document}


• Can you give a few more details about exactly what this label={[shift={(-3ex,3ex)}]south east:#1} is doing? – Joel Jan 19 '15 at 9:30
• Also, what if I wanted the box to have a label D, but have the text showing be, $D_1^r$ --- that is I want a simpler label to use for references within the figure, but I want it to display a more complicated math formula? – Joel Jan 19 '15 at 9:33
• @Joel Please see the edit. – user11232 Jan 19 '15 at 12:18

You could add an extra node for setting the D text:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.markings,shapes,arrows,fit}
\tikzstyle{box}=[draw, minimum size=2em, text width=4.5em, text centered]
\tikzstyle{bigbox}=[draw, inner sep=20pt]

\begin{document}
\begin{tikzpicture}
\node[box] (A) at ( -2,1) {$A$};
\node[box] (B) at ( 1,1) {$B$};
\node[box] (C) at ( 4,1) {$C$};
\node[bigbox, fit=(A)(B)(C)] (D) {};
\node[above left] at (D.south east) {D};
\end{tikzpicture}
\end{document}