# Can't split spreadtab longtable using \newcommand

Check this example (using packages longtable and spreadtab) that has zeroed out rows at the beginning and at the end. These two rows are made invisible by \phantom and are moved up by [-6.9mm] at the begininng.

\documentclass[12pt]{article}
\usepackage{longtable}

\begin{document}

@\textbf{ONE} & @\textbf{TWO} & @\textbf{THREE} & @\textbf{FOUR} \\
[-5.6mm]\phantom{:={0}} & \phantom{:={0}} & \phantom{:={0}} & \phantom{:={0 tag(start)}} \\ \hline
5 & 6 & 7 & 8 \\
5 & 6 & 7 & 8 \\
5 & 6 & 7 & 8 \\
[-5.6mm]\phantom{:={0}} & \phantom{:={0}} & \phantom{:={0}} & \phantom{:={0 tag(stop)}} \\ \hline
&&& sum(cell(start):cell(stop))

\end{document}


This example works. But what I want is to split the table by defining three commands somewhere in the preamble of the document.

First one being \tablestart and second one being \tablestop. Then I want to define third one \input_row which would enable users to easily write the rows.

Like this:

\documentclass[12pt]{article}
\usepackage{longtable}

\newcommand{\tablestart}{
@\textbf{ONE} & @\textbf{TWO} & @\textbf{THREE} & @\textbf{FOUR} \\
[-5.6mm]\phantom{:={0}} & \phantom{:={0}} & \phantom{:={0}} & \phantom{:={0 tag(start)}} \\ \hline

}

\newcommand{\tablestop}{
[-5.6mm]\phantom{:={0}} & \phantom{:={0}} & \phantom{:={0}} & \phantom{:={0 tag(stop)}} \\ \hline
&&& sum(cell(start):cell(stop))

\newcommand{\inputrow}[4]{
#1 & #2 & #3 & #4 \\
}

\begin{document}

\tablestart
\inputrow{5}{6}{7}{8}
\inputrow{5}{6}{7}{8}
\inputrow{5}{6}{7}{8}
\tablestop

\end{document}


## QUESTION:

While first approach works second one fails to render using xelatex main.tex returning the error listed below. Why?

Runaway argument?
{ [-5.6mm]\phantom {:={0}} & \phantom {:={0}} & \phantom {:={0}} & \phantom \ET
C.
! File ended while scanning use of \@argdef.
<inserted text>
\par
<*> test-v2.tex

• You can't have underscores in control sequence names. Try changing \input_row to \inputrow – Don Hosek Mar 6 at 0:15
• @DonaldHosek I changed it but it will not compile. I also changed the MWE's to have \inputrow instead of \input_row. – 71GA Mar 6 at 6:20
• You can't hide the spreadtab environment like this. It tries to crab the body and fails if \end{spreadtab} is not visible. spreadtab also doesn't like if you hide the rows in a macros. You would have to write commands that expand your content and build a spreadtab and issue this. – Ulrike Fischer Mar 6 at 10:14

You have to build your table beforehand and not hide rows in macros, because spreadtab needs to “see” them explicitly.

\documentclass[12pt]{article}
\usepackage{longtable}

\newtoks\tablecontents
\newcommand{\tablestart}{%
\tablecontents={}% initialize
}
\newcommand{\inputrow}[4]{%
\tablecontents=\expandafter{\the\tablecontents #1 & #2 & #3 & #4 \\}%
}

\newcommand{\tablestop}{%
\begingroup\edef\x{\endgroup
\unexpanded{%
@\textbf{ONE} & @\textbf{TWO} & @\textbf{THREE} & @\textbf{FOUR} \\
[-5.6mm]\phantom{:={0}} & \phantom{:={0}} & \phantom{:={0}} & \phantom{:={0 tag(start)}} \\
\hline
}%
\the\tablecontents
\unexpanded{%
[-5.6mm]\phantom{:={0}} & \phantom{:={0}} & \phantom{:={0}} & \phantom{:={0 tag(stop)}} \\
\hline
&&& sum(cell(start):cell(stop))
}%
}\x
}

\begin{document}

\tablestart
\inputrow{5}{6}{7}{8}
\inputrow{5}{6}{7}{8}
\inputrow{5}{6}{7}{8}
\tablestop

\end{document}


The new \tablestart command does essentially nothing, except initialize the token register to empty.

The \inputrow commands incrementally add to the token register and, finally, \tablestop ends the business.

Since we need that the contents of spreadtab is not hidden in macros, a temporary macro \x is defined. It will contain \endgroup (it only serves not to leave \x defined after the job has finished), then

1. the table preamble (the \unexpanded makes sure it's not touched by \edef)

2. the table body, obtained from \the\tablecontents

3. the table postamble

Then \x is executed that typesets the table.

• Could you explain a bit more or maybe point to a similar problem where I can read about this? – 71GA Mar 6 at 11:39
• @71GA I added some comments – egreg Mar 6 at 11:53
• Where can I read more about the macros / commands that you used? – 71GA Mar 6 at 12:36