# Set length to the line height of a particular font

How to define new length using :

\newlength{...}
\setlength{...}{...}


and equal to the line height of a particular font (for example \tiny\ttfamily)

Note : I want a solution which does not involve writing anything in the \begin{document}\end{document} part.

-
\totalheightof{text} from calc package might help. You should use a text with lots of upper case characters and some characters such as p, q... etc. (how are those letters called in English, when parts characters are below the current baseline?) –  Christian Hupfer May 11 '14 at 15:28
@ChristianHupfer, portions of glyphs below the baseline are known as descenders in English. –  Paul Gessler May 11 '14 at 16:29
@PaulGessler: Thank you very much, it is hard to look up such terms in a dictionary! –  Christian Hupfer May 11 '14 at 16:34
@PaulGessler: In German we call it Buchstaben mit Unterlänge, so the English name is definitely shorter ;-) –  Christian Hupfer May 11 '14 at 16:43

This will loop on all characters (from 0 to 255), so it's good only for pdflatex; some different heuristics would be needed for XeLaTeX or LuaLaTeX with fontspec.

The length \fonttotalheight is defined to be the height plus depth, unless the *-variant is used, which will measure only the height, disregarding descenders.

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}

\newlength{\fonttotalheight}

\makeatletter
\newcommand{\setfonttotalheight}{%
\@ifstar{\@tempswafalse\setfont@totalheight}%
{\@tempswatrue\setfont@totalheight}%
}
\newcommand{\setfont@totalheight}[1][]{%
\sbox{\z@}{%
\normalfont
#1% it should be a font selection command
\selectfont
\count@=\z@
\loop
\char\count@
\ifnum\count@<\@cclv
\repeat
}%
\setlength\fonttotalheight{\ht\z@}%
}
\makeatother

\setfonttotalheight*

\begin{document}

Initial value: \texttt{\the\fonttotalheight} (only height)

\bigskip

\begingroup
Fontencoding: OT1
\fontencoding{OT1}\selectfont

\setfonttotalheight*
Normal font (only height): \texttt{\the\fonttotalheight}

\setfonttotalheight
Normal font (height and depth): \texttt{\the\fonttotalheight}

\setfonttotalheight[\ttfamily]
Typewriter type (height and depth): \texttt{\the\fonttotalheight}

\endgroup

\bigskip

Fontencoding: T1

\setfonttotalheight*
Normal font (only height): \texttt{\the\fonttotalheight}

\setfonttotalheight
Normal font (height and depth): \texttt{\the\fonttotalheight}

\setfonttotalheight[\ttfamily]
Typewriter type (height and depth): \texttt{\the\fonttotalheight}

\end{document}


-
fonts designed for use with tex, taking math into account, usually have parentheses with both height and depth equal to the maximum height and depth of all letters, possibly exclusive of diacritics. \mathstrut is set using the parentheses. it would be interesting to see how the height and depth of the \mathstrut compares to the results of this loop algorithm. –  barbara beeton May 12 '14 at 10:40
@barbarabeeton \mathstrut is \vphantom(; so in text it would probably (but not certainly) have the same value we compute; in math it could be different, because the parenthesis is taken from math font in family 0 (that is, usually, the upright text font). –  egreg May 12 '14 at 11:47

I tried the \totalheightof command and wrapped the definition of the length and its setting into a command. Hope, this will help!

\documentclass[12pt]{scrbook}
\usepackage{etoolbox}
\usepackage{ifmtarg}
\usepackage{calc}

\makeatletter
\providecommand{\ProvideLength}[2][]{%
% Check, if the command is already defined, if not, then define it!
\GenericWarning{}{Warning: Length #2 already defined!!!!!!!!} % Optional
}{% Not defined, so define it!
\newlength{#2}%
}%
\@ifmtarg{#1}{%  is 1st argument empty -> do not set the length at all!
}{% Set the length to the value of the 1st argument.
\setlength{#2}{#1}%
}% End of \@ifmtarg
}% End of \providecommand
\makeatother

% Setting the length as 1st parameter and initialize it
% with the value of the second arg. (hopefully a length ;-))
\newrobustcmd{\SetHeightFromTextTemplate}[2]{%
\ProvideLength[\totalheightof{#2}]{#1}%
}%

%\SetHeightFromTextTemplate{} can be done here also

\begin{document}
\begin{flushleft}

\SetHeightFromTextTemplate{\FontHeight}{\FontHeightDummyText}
Height of font is \the\FontHeight

Using $$\backslash$$Large \Large
%%% Recalculate
\SetHeightFromTextTemplate{\FontHeight}{\FontHeightDummyText}
%\setlength{\FontHeight}{\totalheightof{\FontHeightDummyText}}

Height of font is \the\FontHeight

Now with \begin{verbatim}\tiny\ttfamily:\end{verbatim}

\begingroup
\tiny\ttfamily
%%% Recalculate
\SetHeightFromTextTemplate{\FontHeight}{\FontHeightDummyText}

Height of font is \the\FontHeight
\endgroup

\end{flushleft}

\end{document}


-