# How can I use a savebox (setbox) within a tabular environment (a table)?

How can I protect the ampersand (tab alignment character within tabular) and possibly the \\ (new row in tabular) when defining a \savebox?

\documentclass{article}
\usepackage{fontspec}% xelatex
\newsavebox{\tabdata}
\savebox{\tabdata}{sto1 & sto2 & sto3}% I'd like to save this.
\begin{document}
\begin{tabular}{lll}
col1 & col2 & col3 \\
\usebox{\tabdata} \\
\end{tabular}
\end{document}


# Background

I have hundreds of thousands of tables. All of the headers start with \mybeginheadrow and end with \myendheadrow. I'd like to utilize these macros to grab the headers and apply them in different ways to longtable's \endhead, \endfirsthead, etc. I am actually using ltxtable, which first writes all data to an external file. I am not sure whether this is relevant.

Intention

1. Store header row (could be multiple rows) tokens in a macro.
2. Expand them in different places in the table multiple times.
• You cannot do it with a \savebox, but you can do it with a \def as in \documentclass{article} \def\tabdata{sto1 & sto2 & sto3} \begin{document} \begin{tabular}{lll} col1 & col2 & col3 \\ \tabdata \\ \end{tabular} \end{document}. Once inside a box, any content is fully isolated from the surrounding code. – Steven B. Segletes Nov 4 '16 at 9:59
• A saved box is a static object; cells in a tabular are not set until all widths are known. So you see that a saved box is not the tool you want. – egreg Nov 4 '16 at 10:03
• a box is a typset fragment it wouldn's make sense to drop it into different sized tables even if it were syntactically allowed. they need to be macros as you say (and as shown by Steven (except of course \def should be \newcommand in a latex docuemnt) – David Carlisle Nov 4 '16 at 10:04
• @PietvanOostrum That information is not relevant to the question. It is just my reasoning for wanting a \savebox in the first place. I provided the answer based on the comments above. – Jonathan Komar Nov 4 '16 at 15:07

tabular does not support \savebox, see comments to question.

After Stevens comment, I switched from \savebox to \gdef and use \myendheadrow as the delimiter for the argument.

%\newcommand\mybeginheadrow[1]{#1\gdef\myheadrowtokens{#1}}

Call \myheadrowtokens wherever in the table.