[Tex/LaTex] Determine height and depth of letters relative to the font size

calculationsdimensionsfontsize

I would like to scale graphical elements (like images, tikz and tikz-timing diagrams) relative to the font size, so that they have the same height as an normal uppercase letter (i.e. X or M; I noticed they have about the same height, but the tip of A is slightly higher). I also sometimes like to do this with the normal letter depth (e.g. the depth of y or g).

I know that besides the possibility to use the ex or em units for font size relative length (1.6ex =~ height of X), the current font size is stored inside \f@size as string length with the pt stripped. So for normal 10pt font it contains 10. There is also \ht\strutbox and \dp\strutbox which are .7\baselineskip and .3\baselineskip, respectively, which in turn is about 1.2x the font size.
However, a \rule{1pt}{10pt} is significant higher than a 10pt X. This is not that surprising, because \ht\strutbox (which is anyway supposed to by higher than X) is 10pt x 1.2 x 0.7 = 8.4pt in size.

Question:
How is the actual letter height and depth calculated if the font size is known? Is this always a constant factor? Is this font dependent?

I would like to avoid to have to box an X and measure its size, but this would be plan B.

Best Answer

The "ex height" is a length that has a weak relation to the height of an "x"; for example in cmr10 the two are equal, while in TeX Gyre Pagella they don't: an "x" is 4.42pt high an 1ex is 4.845pt.

It's generally impossible to predict the height of the uppercase letters based on the "type size", as barbara beeton underlines in her comment. One should also remember that TeX know only the "bounding box" of the characters, which often protrude from it: for example the upper vertex of an A can overshoot a bit. But for the problem of adapting a built symbol to the general shape of a font this overshoot is not important.

The value of 1ex is font dependent and resides in "fontdimen 5" and is directly available as \fontdimen5\font:

\dimen0=\fontdimen5\font
\dimen0=1ex

are equivalent assignments (which are immediately translated in points).

With e-TeX it's not necessary to box a character in order to measure it:

\dimen0=\fontcharht\font`B
\dimen2=\fontchardp\font`q

which in LaTeX-speak are

\setlength{\dimen0}{\fontcharht\font`B}
\setlength{\dimen2}{\fontchardp\font`q}

assign to \dimen0 and \dimen2 the height of an uppercase B and the depth of a lowercase q in the current font. There are also \fontcharwd and \fontcharic for retrieving the width and the italic correction. Again, these lengths are those of the bounding box, not necessarily of the character itself.

Related Question