# tcolorbox: error with \settototalheight when calculating the header height before passing it to the geometry package

Following up this answer, I would like to know why the following doesn't compile and gives this error

! Undefined control sequence. \tcb@startbox ...x }{\tcb@w@upper }\tcb@lua@color {tcbcolupper}\kvtcb@fontup...

\documentclass{exam}
\usepackage{graphicx}
\usepackage[xparse,breakable,raster,skins]{tcolorbox}
\usepackage{calc}

\newcommand{\exProgram}{3\textsuperscript{rd} Year}
\newcommand{\CourseName}{Engineering}
\newcommand*{\CourseCode}{311}
\newcommand*{\exMidFinal}{Midterm}
\newcommand*{\exSemester}{Fall}

\begin{tcolorbox}[
raster multicolumn = 3
]
\begin{tcbitemize}[
raster force size=false,
raster columns=2,
valign=bottom,
raster equal skip = 0mm,
blankest,
]
\tcbitem[add to width = 2cm, halign = left,]
University\\ Faculty\\
\exProgram
\tcbitem[add to width =-2cm, halign = right]  \includegraphics[height=20 mm]{example-image}
\end{tcbitemize}
\end{tcolorbox}%
%
\vspace{5pt}\hrule\vspace{5pt}%
%
\begin{tcolorbox}[left=0pt,right=0pt]
\exMidFinal\\
\exSemester
\end{tcolorbox}
\begin{tcolorbox}[before upper*=\centering]
Course Code: \textbf{\CourseCode}
\end{tcolorbox}
\begin{tcolorbox}[halign=right]
1\\
2
\end{tcolorbox}%
%
\vspace{5pt}\hrule\vspace{5pt}%
%
\begin{tcolorbox}[
before upper*=\centering, equal height group=A,
interior engine = standard,
colback = gray,
raster multicolumn = 3,
top = 5pt, bottom = 5pt
]
\textbf{\CourseName}
\end{tcolorbox}%
%
\vspace{5pt}\hrule\vspace{5pt}%
%
\begin{tcolorbox}[
before upper*=\centering,
equal height group=A,
raster multicolumn = 3,
]
This exam contains
\end{tcolorbox}%
}

\begin{minipage}{\linewidth}%
\begin{tcbraster}[
raster columns=3, raster rows=3,
raster equal height = rows,
raster every box/.style={minimum for current equal height group = 8 mm},
valign=center,
raster equal skip = 0mm,
top = 0mm, bottom = 0mm,
blankest,
nobeforeafter,
]
\vspace*{5pt}%
\hrule%
\end{tcbraster}%
\end{minipage}%
}

\usepackage[
a4paper,
top = 0mm,
showframe
]{geometry}

\begin{document}
text
\end{document}

• you shouldn't look only at the error as presented by your editor (texstudio?), they often shorten it and don't show the actual undefined command. – Ulrike Fischer Nov 28 '20 at 10:01
• @UlrikeFischer Thanks for the hint. I remember you previously told me about some package or way to quote the errors but I don't remember it now. – Diaa Nov 28 '20 at 12:43

You should never measure things in the preamble. Font settings might be delayed at begin document.

Do the setting at the right place:

\AtBeginDocument{\settototalheight\headerTotalHeight{\headerContents}}


Now, if you want to know why this used to run but now fails? A change in tcolorbox.sty, aimed at exploiting LuaTeX color management if available, has been introduced. This requires defining \tcb@lua@color in a suitable way and the package author chose to do it at begin document.

This means that the meaning of \tcb@lua@color is not yet available in the document preamble, whence the error.

• Now, it works, but the header is printed above the paper top border. What should be done to make it printed as expected just below the paper top border? – Diaa Nov 28 '20 at 12:42
• So, I think \headerTotalHeight was passed to geometry too late. – Diaa Nov 28 '20 at 14:43
• add \geometry{headheight = \headerTotalHeight} to the \AtBeginDocument command. – Ulrike Fischer Nov 30 '20 at 17:16
• @UlrikeFischer I would be grateful if you could provide me with a full-code answer to make sure I do it right. – Diaa Dec 1 '20 at 0:09
• If I would like to have the following \newlength{\headerTotalHeight} \AtBeginDocument{\settototalheight\headerTotalHeight{\headerContents}\geometry{headheight = \headerTotalHeight}} inside the \ExplSyntax code of this question, what should be done? – Diaa Dec 2 '20 at 23:10