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

\usepackage{fontspec}% xelatex
\savebox{\tabdata}{sto1 & sto2 & sto3}% I'd like to save this.
col1 & col2 & col3 \\
\usebox{\tabdata} \\


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.


  1. Store header row (could be multiple rows) tokens in a macro.
  2. Expand them in different places in the table multiple times.
  • 2
    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
  • 1
    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
  • 1
    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
  • @macmadness: Your code does not correspond with your description. You say "All of the headers start with \mybeginheadrow and end with \myendheadrow" but your example code does not contain \mybeginheadrow nor \myendheadrow. – Piet van Oostrum Nov 4 '16 at 15:01
  • @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.

\def\mybeginheadrow#1\myendheadrow{\gdef\myheadrowtokens{#1}‌​}% see comments

Call \myheadrowtokens wherever in the table.

  • Then define it as \def\mybeginheadrow#1\myendheadrow{\gdef\myheadrowtokens{#1}} or something similar. Or give an example that actually shows what you want. – Piet van Oostrum Nov 4 '16 at 15:02
  • @PietvanOostrum Interesting idea! I will try that out. I guess it will read until token \myendheadrow to close the group. – Jonathan Komar Nov 4 '16 at 15:10
  • It is called a delimited argument. – Piet van Oostrum Nov 4 '16 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.