[Tex/LaTex] Measuring the height of a box

calccalculationsheightunit-of-measure

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}}}
%\setlength{\headheight}{\tbheight + 0.055\textwidth}
}

%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}}}
\setlength{\headheight}{ 0.055\textheight}
}

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}}}
    \setlength{\headheight}{\tbheight + 0.055\textheight}
    }

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}{$_{}$}}
        \newlength\headspace
        \settoheight\headspace{\temp}

    \setlength{\headheight}{\tbheight + \headspace}
}

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}{$_{}$}}
        \newlength\headspace
        \settoheight\headspace{\temp}

    \setlength{\headheight}{\tbheight + \headspace}
}

Best Answer

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.

enter image description here

Related Question