39

How can I get a length, which reflects the hight of a (flat) capital letter?

While \em gives me a width, \ex refers to a lowercase letter and \f@size refers to the overall fontsize including the ascent and the descent.

2
  • 6
    \em is not the width of a capital letter, it's the point size of the font.
    – Sverre
    Commented Oct 28, 2015 at 12:07
  • @Sverre: \em is for enabling emphasis. The unit em however, is, as far as I know, supposed to be the width of the letter M, but usually a little less. See, for example, here, here and here
    – Bananguin
    Commented May 2, 2016 at 17:43

4 Answers 4

37

The value of 1em is not necessarily the width of any capital letter (its name refers to the width of a capital M, but it's not necessarily so and the value is basically the font designer's decision).

Similarly, 1ex traditionally refers to the height of a lowercase x, but it may not be if the font designer decided otherwise.

LaTeX provides a standard way to get the width and height of a character (or of a bunch of characters):

\settoheight{\mylen}{A}
\settowidth{\mylen}{A}
\settodepth{\mylen}{g}

will assign \mylen (which should have been declared with \newlength{\mylen}) the corresponding dimensions at point of call.

If you want to record the height of a typical capital letter at the default font size for later usage, do

\newlength{\ucht}
\AtBeginDocument{\settoheight{\ucht}{A}}

because some font packages do their set up at begin document and doing \settoheight in the preamble may not reflect the main document font.

If you want to use the height of a capital letter depending on the current font, the construction \fontcharht\font`A can be used wherever a length is needed.

An esteemed community member objects in comments that using A or X could lead to different results. Indeed this can be true for some fonts. In Computer Modern all capital letters are assigned the same height, even if some of them “overshoot” it: typically A, C, G, O, Q. Other fonts may be subject to different decisions by the designer, so a bit of experimentation is needed with the actual document font.

4
  • oh, golly, you too? look at a few fonts. not all "A"s are flat on top, and sometimes that point exceeds the designed cap height. i cant think of any (roman) fonts that have ah "X" or "H" with excrescences at the top. Commented Oct 28, 2015 at 12:26
  • @barbarabeeton In several fonts (CM included), the height of A and of T or X are the same, even if the A glyph slightly overshoots.
    – egreg
    Commented Oct 28, 2015 at 12:32
  • What does the character between \font and A do in this construct? I'm having trouble making Google yield good results on it.
    – Bananguin
    Commented May 2, 2016 at 17:49
  • 1
    @user1129682 <backquote>A is TeX lingo for an “alphabetical constant”: it denotes the ASCII code of the letter A. \fontcharht must be followed by a font specifier (\font stands for the current one) and by a number denoting the slot in the font. In this case, the slot corresponding to an A.
    – egreg
    Commented May 2, 2016 at 17:59
18

The other answers do a great job of explaining how to measure the height of a letter. As egreg added, the terms "em" and "ex" are subject to the interpretation of the font designer and cannot be used as strict measuring units of a letter's dimension.

I just wanted to add that the notions of a letter's height and depth are also equally subjective. It is typical that curved letters will extend past their designated height and depth. Apparently, it is related to the way the brain perceives small "overshoots" as balanced or matched to a large/flat non-protrusion. Discussion of this concept can be found at http://ilovetypography.com/2009/01/14/inconspicuous-vertical-metrics/.

I show some examples in the MWE, just to remind the OP that, if the goal is to use the letter's official height and depth to truncate or encapsulate the letter, it will generally not be sufficient.

\documentclass{article}
\begin{document}
\fboxrule=.1pt
\fboxsep=-\fboxrule
\fbox{e}\fbox{Q}\fbox{A}
\end{document}

enter image description here

6
\documentclass{article}
\newlength{\mylen}
\AtBeginDocument{\setlength{\mylen}{\fontcharht\font`X}}   %% height of X
\begin{document}
  X\rule{5cm}{\mylen}
\end{document}

enter image description here

Thanks to egreg and Barbara.

4
  • Not really so, some font packages do their set up at begin document, so the value of \mylen may not reflect the actual height of capital letters in the document.
    – egreg
    Commented Oct 28, 2015 at 12:00
  • @egreg: Is \AtBeginDocument{\setlength{\mylen}{\fontcharht\fontT}} ` better?
    – user11232
    Commented Oct 28, 2015 at 12:11
  • better letter "X"; the "T" has serifs above the cap height in some fonts, e.g. garamond. Commented Oct 28, 2015 at 12:23
  • @barbarabeeton: Thanks. I have changed the answer accordingly.
    – user11232
    Commented Oct 28, 2015 at 12:29
2
\newsavebox{\mybox}
\savebox{\mybox}{A} % Put your letter here instead of A
\newlength{\uppercaseHeight}
\setlength{\uppercaseHeight}{\ht\mybox}
\newlength{\uppercaseWidth}
\setlength{\uppercaseWidth}{\wd\mybox}
1
  • 1
    \settoheight{\uppercaseHeight}{A}, no need for a save box. There are also \settodepth and \settowidth.
    – egreg
    Commented Oct 28, 2015 at 12:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .