[Tex/LaTex] tikz text location within node

tikz-pgf

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}

Best Answer

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}

enter image description here

Edit

Response to the comments:

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}

enter image description here