# Problem to store tabulars in boxes in the preamble when colortbl is loaded

In the following example the second \sbox gives errors when colortbl is loaded.

! Undefined control sequence.
<template> ...@setup \CT@row@color \CT@cell@color
\CT@do@color \endgroup \@t...
l.25     \end{tabular}}


The error message can change depending on the tabular preamble.

There is no problem when the \sbox is moved in the document. But I would like to store the box in the preamble as I need its dimensions to setup the page geometry. So what is causing the error? Is there a workaround? Does it indicate some hidden problem which affects the first box too?

\documentclass[]{article}
\usepackage{colortbl}

\newsavebox\myboxA

\sbox\myboxA{%
\begin{tabular}{l}
ab
\end{tabular}}

\newsavebox\myboxB

\sbox\myboxB{%
\begin{tabular}{l}
ab
\end{tabular}}

\begin{document}
abc
\end{document}


colortbl delays a lot of its processing to check whether other packages such as hhline are loaded, probably this

\AtBeginDocument{%
\expandafter\def\expandafter\@tabarray\expandafter{%
\expandafter\CT@start\@tabarray}}


but if you look in the package you will see other delayed definitions as well.

Generally speaking though I'd be wary of setting boxes with anything other than simple text in the preamble, latex does not set up the fonts or math settings until \begin{document} in case font packages are loaded in the preamble, so typesetting in the preamble is not totally reliable.

so if you want to be brave:

\documentclass[]{article}
\usepackage{colortbl}

\newsavebox\myboxA
\makeatletter
\sbox\myboxA{%
\expandafter\def\expandafter\@tabarray\expandafter{%
\expandafter\CT@start\@tabarray}%
\begin{tabular}{l}
ab
\end{tabular}}

\newsavebox\myboxB

\sbox\myboxB{%
\expandafter\def\expandafter\@tabarray\expandafter{%
\expandafter\CT@start\@tabarray}%
\begin{tabular}{l}
ab
\end{tabular}}

\makeatother

\begin{document}
abc
\end{document}

• Well as I wrote I need the box to calculate the page geometry (and I'm calling the fonts before storing the box) and I don't want to put everything in \AtBeginDocument. So I will be brave. So thank you for the code ;-). But I found the reason why only the second explodes: The first box issues at the end \CT@end whose definition is not delayed to the begin of the document. – Ulrike Fischer Nov 17 '14 at 17:29
• @UlrikeFischer yes but if there is any math in the tables (and actually tabular uses math internally as you'll know) then things are not really set up \begin{document} does \process@table \let\glb@currsize\@empty %% Force math initialization. \normalsize and typesetting anything (especially math) before the font tables are set up is inherently risky – David Carlisle Nov 17 '14 at 17:38
• There is no math in the tables, and I actually don't save them for later typesetting, I only need the height and \geometry doesn't work in \AtBeginDocument. But you are naturally right that it is a bit risky so I made some tests and I think I can save the boxes in \AfterEndPreamble (from etoolbox) and use \newgeometry there instead of \geometry. This looks saner - even if the other code worked. – Ulrike Fischer Nov 18 '14 at 9:29
• @UlrikeFischer if you use a default [c] environment the tabular is centred via vcenter in math mode, that may not matter (I honestly haven't checked how safe it is, but if it was my document I'd check very carefully:-) – David Carlisle Nov 18 '14 at 9:45