[Tex/LaTex] tikzstyle default node text with fill color

tikz-pgf

As a follow-up to this solution on defining the default text for a node, how can it be done with the use of a fill color? Note below that specification of a fill color abolishes the node text.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzstyle{stuff_nofill}=[rectangle,draw,font={A}]
\tikzstyle{stuff_fill}=[rectangle,draw,fill=black!20,font={A}]
\begin{document}
\begin{tikzpicture}[node distance=0.5cm,auto]
 \node at (0,1) [stuff_nofill] {};
 \node at (0,0) [stuff_fill] {};
\end{tikzpicture}
\end{document}

Best Answer

Yet another hack but I think a little bit more robust since it utilizes a legitimate way of putting text inside a node.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzstyle{stuff_nofill}=[rectangle,draw,font={A}]
\tikzstyle{stuff_fill}=[rectangle,draw,fill=black!20,minimum size=1.4em,label={center:A}]
\begin{document}
\begin{tikzpicture}
 \node at (0,1) [stuff_nofill] {};
 \node at (0,0) [stuff_fill] {};
\end{tikzpicture}
\end{document}

enter image description here

My assumption is that the default text would be defined once and there is no need to change it often otherwise you would just put a regular node. So, we can do a little calculation about the default text size and draw the background node accordingly. The text will then fit into the filled rectangle.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newdimen\mywidth
\newdimen\myheight
\def\mytext{\Huge A}% define the text
\pgfmathparse{width("\noexpand\noexpand\noexpand\mytext")} %the width calc
\pgfmathsetlength{\mywidth}{\pgfmathresult} % Put it into a length register
\pgfmathparse{height("\noexpand\noexpand\noexpand\mytext")}%the height calc
\pgfmathsetlength{\myheight}{\pgfmathresult} % Put it into a length register
\tikzset{stuff_fill/.style={
                    rectangle,draw,fill=black!20, 
                    minimum height=1.5\myheight, % set the min height to the value above
                    minimum width=1.5\mywidth, % set the min width to the value above
                    label={center:\mytext} 
                    },
stuff_nofill/.style={rectangle,draw,font={A}}
}
\begin{document}
\begin{tikzpicture}
 \node at (1,0) [stuff_nofill] {};
 \node at (0,0) [stuff_fill] {};
\end{tikzpicture}
\end{document}

enter image description here

If you use \def\mytext{\tiny tiny text},

enter image description here