5

One can use storebox package to save space.

For example, I will expect that this works as it is similar to the use of savebox. However, it does not. One needs to move the \storebox{\foobox}{FOO} inside the \begin{document} to use it.

\documentclass{article}

\usepackage{storebox}
\newstorebox{\foobox}

% moving this line inside begin{document} works
\storebox{\foobox}{FOO}

\begin{document}
 \usestorebox{\foobox}
\end{document}

Now, consider this

\documentclass{article}

\newsavebox{\foobox}
\savebox{\foobox}{FOO}
\begin{document}
 \usebox{\foobox}
\end{document}

This option with savebox works.

So, how can I make the use of \storebox before \begin{document} work as savebox?

adn
  • 11,233
  • 4
    Neither \savebox nor \storebox should be used before \begin{document}. – egreg May 25 '15 at 22:26
  • If I need to initialize something in the boxes, and use them inside the document, what would be the proper way of do it? For example, I may need to initialize something within a style or package, and use it through a macro in the main document. What would be the correct way then? – adn May 26 '15 at 12:53

1 Answers1

3

The document font setup is expected to be completed when \begin{document} is processed, which includes a \normalfont declaration to finish up all font requests.

Doing \savebox in the preamble provides no guarantee that the fonts will match those of the document: several font packages delay their actions with \AtBeginDocument, so they can take advantage of knowing all loaded packages. The same applies to \storebox.

If you want to have \savebox or \storebox in the preamble or in a package, try with

\usepackage{etoolbox}
\usepackage{storebox}

\newstorebox{\foobox}

\AfterEndPreamble{%
  \storebox{\foobox}{whatever}%
}

or just do \storebox{\foobox}{whatever} after \begin{document} if it's just document code. Note that code delayed with \AfterEndPreamble is executed after any code delayed with \AtBeginDocument. You can have as many \AfterEndPreamble commands as you want, their effect is cumulative.

egreg
  • 1,121,712