# Measuring the height of a box

Solution in Edit 3. It works well, but seems to need an optimization. :)

MWE:

\documentclass{scrartcl}
\usepackage{calc}
\usepackage{mathptmx}
\usepackage{anyfontsize}

\newcommand{\Title}[1]{
\newlength\tbheight
\setlength{\tbheight}{\heightof{\phantom{\fontsize{0.04\textheight}   {0.04\textheight}\selectfont \parbox{\titlewidth\textwidth}{\textbf{#1}}\par}}}
}

%it should not matter if I use:
\Title{A longer Text that goes over two or maybe three lines}
\begin{document}
%or I use it in this place:
%\Title{A longer Text that goes over two or maybe three lines}
\end{document}


This was suggested to work on some threads. But it dont. I want to measure the height the manipulated input. My own idea was: manipulate the input, make it invisible, put it in a box, let the box grow by text, measure the height of the box. This sounds easy, but I guess, my skills aren't grown enough.

Got anyone a nice idea for solving this?

# Edit:

If I use this code, I do not get any error:

\newcommand{\Title}[1]{\title{#1}
\def\tbheight{\heightof{\vphantom{\fontsize{0.04\textheight}{0.04\textheight}\selectfont \parbox{\titlewidth\textwidth}{\textbf{#1}}\par}}}
}


but if I do so

\newcommand{\Title}[1]{\title{#1}
\def\tbheight{\heightof{\vphantom{\fontsize{0.04\textheight}{0.04\textheight}\selectfont \parbox{\titlewidth\textwidth}{\textbf{#1}}\par}}}
}


there are 6.

Error 1 Paragraph ended before \calc@textsize was complete. I guess to solve this, there is another command required?

Error 2-4 to many '{' or forgotten endgroup 5,6 missing number, illegal unit

# Edit 2:

If I use this Code, I don't have any errors, but this measures wrong.

\newcommand{\temp}{}
\newcommand{\Title}[1]{\title{#1}
\renewcommand\temp{\parbox{\titlewidth\textwidth}{\fontsize{0.04\textheight}{0.04\textheight}\selectfont \textbf{Titelmasterformat durch Klicken bearbeiten}\par}}
\newlength{\tbheight}
\settoheight\tbheight{\heightof{\temp}}

\renewcommand{\temp}{\parbox[c][0.055\textheight]{1pt}{$_{}$}}

}


When I type in \the\textheight the output is

3383.03267 if I multiply it by 0.055 it is 3383.03267 * 0.055 ~ 186 but \the\headspace ~ 100

# Edit 3:

If I use \settototalheight instead of \settoheight, I've got the right measures, but something went wrong. It is not the "height", as I guessed.

\newcommand{\temp}{}
\newcommand{\Title}[1]{\title{#1}
\renewcommand\temp{\parbox{\titlewidth\textwidth}{\fontsize{0.04\textheight}{0.04\textheight}\selectfont \textbf{Titelmasterformat durch Klicken bearbeiten}\par}}
\newlength{\tbheight}
\settototalheight\tbheight{\temp}

\renewcommand{\temp}{\parbox[c][0.055\textheight]{1pt}{$_{}$}}

}

• What's the \phantom for? – egreg Mar 28 '17 at 16:07
• it makes the text besides invisible and inserts a box of height and width of that text. You could use "vphantom" for a vertical-phantom, with 0 width, and "hphantom" for an horizontal-phantom with 0 height. – Shalec Mar 28 '17 at 16:10
• Do you know about \settoheight? – egreg Mar 28 '17 at 16:51
• I used it in my last edit ^^ – Shalec Mar 28 '17 at 16:54
• but \settoheight measures wrong. If I setup an fbox{parbox} with height "0.055\textheight" and one of height "\headspace" I get two different boxes. – Shalec Mar 28 '17 at 16:55

It's not really clear what you want to achieve. Anyway, here's the code.

\documentclass{scrartcl}
\usepackage{mathptmx}

\newlength\tbheight % should be outside
\newcommand{\titlewidth}{0.3} % ???

\newcommand{\Title}[1]{% <-- don't forget
\settoheight{\tbheight}{%
\parbox[b]{\titlewidth\textwidth}{%
\fontsize{0.04\textheight}{0.04\textheight}\selectfont
\bfseries #1
}%
}%
}

%it should not matter if I use:
\normalfont
\Title{A longer Text that goes over two or maybe three lines}
\edef\THETBHEIGHT{\the\tbheight} % just to save the set value
\begin{document}
%or I use it in this place:
\Title{A longer Text that goes over two or maybe three lines}

\THETBHEIGHT

\the\tbheight

\end{document}


Note that typesetting text in the preamble is not guaranteed to give meaningful results; here I used \normalfont, but in general it's better to delay such things at begin document.

I used \settoheight and \parbox[b], so the height measures the whole box, except for the depth of the last line, which should be ignored anyway.

• Is there a way to save "\textheight" in a variable, without editing it, while compiling? If I do \def\Textheight{\textheight} and \setlength\textheight{.8\textheight} the resut will be: \Textheight = \textheight. – Shalec Mar 28 '17 at 17:58
• @Shalec \newlength\Textheight\setlength\Textheight{\textheight} will save the original value of \textheight in \Textheight – David Carlisle Mar 28 '17 at 20:39

You can create a savebox in the preamble and use it (multiple times) in the body.

\documentclass{scrartcl}
%\usepackage{calc}
\usepackage{mathptmx}
\usepackage{anyfontsize}

\newsavebox{\titlebox}

\newcommand{\Title}[1]{\savebox{\titlebox}%
{\selectfont \parbox[b]{\textwidth}{\fontsize{0.04\textheight}{0.05\textheight}\textbf{#1}}}%
}

\Title{A longer Text that goes over two or maybe three lines}
\begin{document}
height = \the\ht\titlebox\par
depth = \the\dp\titlebox\par
total = \the\dimexpr \ht\titlebox+\dp\titlebox\relax\par
width = \the\wd\titlebox

\usebox{\titlebox}
\end{document}