I'd like to have a style to make my drawings exactly as tall as a capital letter, in order to draw things in a line of text, even if the drawing command is used in a block of text with a different font or size (say, a footnote or the table of contents). I'd like to avoid metrics like em or ex because their definition relative to capital letters is not consistent across fonts (see What is the local height of a capital letter?).
Getting the height of a capital letter works with \settoheight, but not inside \tikz[<styles>]. I guess that is because the TikZ parser sets the font to nullfont or something like that. Wrapping the whole \tikz command in a macro works, but having an actual TikZ style to achieve the same would be nicer. Can this be done?
\documentclass{article}
\usepackage{tikz}
\newdimen\myLetterHeight
\tikzset{
scale to letter height/.code={%
\settoheight{\myLetterHeight}{#1}%
\pgfkeysalso{/tikz/x=\myLetterHeight, /tikz/y=\myLetterHeight, baseline=0pt}%
},
}
\newcommand*{\TikzScaleToLetterHeight}[2][X]{\begingroup%
\settoheight{\myLetterHeight}{#1}%
\pgfkeysalso{/tikz/x=\myLetterHeight, /tikz/y=\myLetterHeight, /tikz/baseline=0pt}%
#2%
\endgroup}
\begin{document}
WITH KEY: \tikz[scale to letter height=X]
\draw rectangle (1,1) node[right] {\the\myLetterHeight};
WITH MACRO: \TikzScaleToLetterHeight[X]{\tikz
\draw rectangle (1,1) node[right] {\the\myLetterHeight};}
{\Large Large: \TikzScaleToLetterHeight[X]{\tikz
\draw rectangle (1,1) node[right] {\the\myLetterHeight};}}
\end{document}


height()approach it is unnecessarily complex and I would not recommend it as the first solution to anyone coming across this thread in the future). – Fritz Mar 22 '22 at 09:53