Determine the height of a box with a rule

\documentclass{scrartcl}

\usepackage{calc}
\usepackage{showframe}

\begin{document}
\setlength\parindent{0pt}
\newsavebox\maintitleBox
\newlength\maintitleHeight
\newcommand{\maintitle}[1]{%
\clearpage%
\savebox\maintitleBox{%
\parbox{\textwidth}{%
#1

\rule{\textwidth}{1mm}\par%
}%
}%
\setlength\maintitleHeight{\ht\maintitleBox + \dp\maintitleBox}%
\vspace*{-\maintitleHeight}\usebox\maintitleBox
}%

\maintitle{Test}
\end{document}


The maintitle is supposed to go above the text area on some pages. What I am trying to do is to save the height of the box and move the box up by exactly the height of the box.

However, I can't get it to work. What am I missing?

Edit: Would I have been more committed to testing, I would have seen that \maintitleHeight is only the height of the text; not of the rule below. Now the question is: why is that and how can I fix this?

• You should always post a complete (but minimal) document that reproduces the problem, not a fragment that no one can run, but \ht\maintitleBox + \dp\maintitleBox is a syntax error unless you have loaded the calc package (it would be clear what packages were in use in a complete document) Mar 14, 2016 at 9:38
• I am sorry, I was in a hurry. Added an MWE. Mar 14, 2016 at 15:47
• Do you just want to move content into the header? For that you can use a header package like fancyhdr (while you're using a KOMA-script class, you're not using any of its functionality, so I doubt it's really in-use).
– Werner
Mar 14, 2016 at 17:00

If your aim is to make the heavy rule sitting at the top of the text area, no measuring is needed:

\documentclass{scrartcl}

\usepackage{showframe}

\newcommand{\maintitle}[1]{%
\clearpage
\vspace*{-\dimexpr\topskip+\baselineskip}
\noindent
\parbox[b][0pt]{\textwidth}{%
#1\par
\rule{\textwidth}{1mm}\par
\vspace{0pt}
}%
}

\begin{document}

\maintitle{Test}

Some text follows

\end{document}


• +1 but as you can see the vertical position of "Some text follows" is shifted. Something like \vspace{\dimexpr-\baselineskip+\topskip}% below the \parbox is needed.
– user4686
Mar 15, 2016 at 8:31
• @jfbu I don't think this should be much of a concern: I hope some vertical space follows the title anyway. Mar 15, 2016 at 8:33
• It the rule was positioned a bit higher, maintaining same vertical position for main text could become more attractive. Particularly if the top rule is only for certain pages.
– user4686
Mar 15, 2016 at 17:12

It isn't really clear what you want to do, \parbox is vertically centred by default and your box has

> \box29=
\hbox(13.27873+7.80376)x418.25555
.\pdfcolorstack 0 push {0 g 0 G}
.\mathon
.\vbox(13.27873+7.80376)x418.25555
..\hbox(7.48248+0.0)x418.25555, glue set 397.81555fil []
..\glue(\parskip) 0.0
..\glue(\baselineskip) 10.75475
..\hbox(2.84526+0.0)x418.25555 []
.\mathoff
.\pdfcolorstack 0 pop


so it has one line of text and then the rule, although only 1mm high adds another 12pt being \baselineskip to the overall height+depth of the box.

At the top of the page you have

...\glue(\topskip) 11.0


% setting for the first baseline in this class

...\rule(0.0+0.0)x*
...\penalty 10000
...\glue -21.08249


% the negative space you added

...\glue 0.0
...\glue(\parskip) 0.0 plus 1.0


% parskip before the \usebox (which might stretch)

...\glue(\baselineskip) 0.32127


...\hbox(13.27873+7.80376)x418.25555

• @chaosflaws \showoutput for the page layout and \showbox for the specific box. Mar 16, 2016 at 14:38