# Determine height and depth of letters relative to the font size

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.

-
go with plan b. the relative height/depth of letters in a font is dependent on the font designer's concept of how the font should look -- or should be. when fonts were still metal, there were some fonts for which the manufacturer changed the design (usually by shortening the descenders) to fit into "normal" or "standard" type dimensions. – barbara beeton Jul 3 '11 at 19:43
Thanks @barbara, any suggestions about which letter I should use? `X` or `M` seem to be good candidates for the height, but I'm not sure for the depth: `y` or `g` maybe? I could just box the whole alphabet but this might lead to a worse result. Like I said, the `A` is a little bit higher and taking the absolute maximum and minimum would most likely not look good. – Martin Scharrer Jul 3 '11 at 19:54
Just to get an example, I measured 1ex for font `pplr8t` (Palatino), which gives 4.68994pt, while a lowercase x is 4.84497pt high. With `ec-qplr` (TeX Gyre Pagella) the same measurements give 4.48999pt and 4.41998pt respectively. I'm with barbara: go with plan B. For the height a B can be a good choice (I wouldn't use M that suffers the same problem as A); for the depth maybe a q that hasn't fancy curves at the bottom. – egreg Jul 3 '11 at 19:55
@egreg: Thanks. Just one another little question: does TeX get the length of 1ex and 1em from the font or are they only dependent on the official font size? – Martin Scharrer Jul 3 '11 at 20:02
@Martin -- as has been pointed out by others, the x-height may or may not equal 1ex from the tfm metrics. but you wanted the cap height. pack the cap X in a box and measure. a cap A or any rounded letter (O, C, etc.) will usually be just a tad higher as an optical correction. if you want the max height of the font, you can usually get it from the (measured) height of a parenthesis if the font has been tuned to be suitable for math. similarly for the max depth -- however, beware of text-only fonts: parentheses (and slashes) may encompass only ordinary uppercase. it's up to the designer. – barbara beeton Jul 4 '11 at 17:49

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.

-
Thanks! Very good to know. I might use something like this. – Martin Scharrer Jul 3 '11 at 21:06
Just be sure to do the measurements when the current font is the one you want; `\font` can actually be replaced by the symbolic name of any font such as `\OT1/cmr/m/n/10` (which must be built with `\csname...\endcsname`, of course. – egreg Jul 3 '11 at 21:09