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.
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.
\font
and A
do in this construct? I'm having trouble making Google yield good results on it.
Commented
May 2, 2016 at 17:49
<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.
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}
\documentclass{article}
\newlength{\mylen}
\AtBeginDocument{\setlength{\mylen}{\fontcharht\font`X}} %% height of X
\begin{document}
X\rule{5cm}{\mylen}
\end{document}
Thanks to egreg and Barbara.
\mylen
may not reflect the actual height of capital letters in the document.
\AtBeginDocument{\setlength{\mylen}{\fontcharht\font
T}} ` better?
\newsavebox{\mybox}
\savebox{\mybox}{A} % Put your letter here instead of A
\newlength{\uppercaseHeight}
\setlength{\uppercaseHeight}{\ht\mybox}
\newlength{\uppercaseWidth}
\setlength{\uppercaseWidth}{\wd\mybox}
\settoheight{\uppercaseHeight}{A}
, no need for a save box. There are also \settodepth
and \settowidth
.
\em
is not the width of a capital letter, it's the point size of the font.\em
is for enabling emphasis. The unit em however, is, as far as I know, supposed to be the width of the letterM
, but usually a little less. See, for example, here, here and here