2

I have a spreadtab with a rather complex header and footer that is used at several places in my document. The actual content consists of only a few simple lines. I would like to define a new environment (or macro), so that I do not have to repeat the header and footer for each table.

The only working approach I found is the following, based on https://tex.stackexchange.com/a/312860/102921 and https://tex.stackexchange.com/a/207313/102921.

\documentclass{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[main=english]{babel}

\usepackage{array, environ, spreadtab, xpatch}

\makeatletter
\def\spreadtab@ii{\IfSubStr\ST@tab{\noexpand\input}{\expandafter\spreadtab@iii\ST@tab\@nil}\relax}
\def\spreadtab@iii#1\input#2#3\@nil{%
    \long\def\spreadtab@iv##1\spreadtab@iv{\endgroup\def\ST@tab{#1##1#3}\spreadtab@ii}%
    \begingroup
    \everyeof{\spreadtab@iv\noexpand}%
    \expandafter\spreadtab@iv\@@input#2
}
\xpretocmd\spreadtab@i\spreadtab@ii{}{}
\makeatother

\newwrite\mytabularwrite

\NewEnviron{mytable}{%
    \immediate\openout\mytabularwrite=mytabularfile.tex%
    \immediate\write\mytabularwrite{\unexpanded\expandafter{\BODY}}%
    \immediate\closeout\mytabularwrite
    \begin{spreadtab}{{tabular}{|l|ccc|}}
        \hline
        & @Heading 1 & @Heading 1 & @Heading 1 \\\hline % here is a complex header
        \input{mytabularfile}
        @ Total & \STcopy{>}{sum(b2:[0,-1])} &  & \\\hline % end a complex footer
    \end{spreadtab}
}

\begin{document}
    \begin{mytable}
        @ Label 1 & 1000 & 500 & 800 \\\hline 
        @ Label 2 & 2000 & 1500 & 1800 \\\hline
        % At most 10 of such lines per table
    \end{mytable}
\end{document}

Is there a way without using temporary files? Because the final document will have of the order of 100 of such tables, I am a bit worried about speed.

0

You must log in to answer this question.

Browse other questions tagged .