# Use input inside a spreadtab environment with calculations

My aim is to create a command for inserting tables, which will format the table, and add calculations on its values, given a file containing the raw data of the rows only.

\documentclass{article}
\begin{document}

\toprule
@ Product name & @ Price & @ Count \\
\midrule
\input{products}
\midrule
@ Total & :={sumprod(b2:[0,-1];c2:[1,-1])} & sum(c2:[0,-1]) \\
\bottomrule

\end{document}


where products.tex includes

@ Plant &  60 &  2 \\
@ Book  &  90 &  4 \\
@ Other & 100 & 10 \\


Pasting the lines of products.tex into the matching place above works smoothly.

I wish to have the above code as a command, so I can use the same code to enter different table values, without repeating the heading and calculation definitions. However, the end result of using input always leaves the summation values as 0, and the @ letter is now part of the text:

I guess the processing by spreadtab is done before the input is inserted - but I wonder if there is a way to solve it.

Following this question: Cannot use \toprule when doing \input inside tabular -- why?, I tried to add primitive tex input into the spreadtab environment. Meaning, I used

\makeatletter
\newcommand\primitiveinput[1]{\@@input #1 }
\makeatother


and replaced the line \input{products} with \primitiveinput{products}. But again no luck - I get the exact same output as using input.

Any ideas?

• Please provide a complete, compilable example (Minimum Working Example) starting with \documentclass and ending with \end{document} as this will make it much easier for people to help you effectively. Have you looked at datatool or csvsimple? Perhaps one of those would help?
– cfr
Sep 14, 2014 at 19:53
• What are the constraints on the file input? Does it need to be in that format or are there other possibilities?
– cfr
Sep 14, 2014 at 19:59
• See the answer by @unbonpetit for the complete (trivial) example and the workaround solution.
– emem
Nov 3, 2014 at 11:31
• Thanks for the reference to datatool and csvsimple packages - they look interesting for future use
– emem
Nov 3, 2014 at 11:38

Here is a workaroud. All \input{<name>} are replaced by the file contents in the spreadtab environment:

\documentclass{article}
\begin{filecontents*}{products.tex}
@ Plant &  60 &  2 \\
@ Book  &  90 &  4 \\
@ Other & 100 & 10 \\
\end{filecontents*}
\makeatletter
\begingroup
}
\makeatother
\begin{document}
\toprule
@ Product name & @ Price & @ Count \\
\midrule
\input{products}
\midrule
@ Total & :={sumprod(b2:[0,-1];c2:[1,-1])} & sum(c2:[0,-1])\\
\bottomrule
\end{document}

• This works perfectly for me, thank you. If you could come up with a similar answer for a command instead of a file feel free to post your answer here Oct 21, 2014 at 11:34
• Thanks. Works great. Although I was hoping to get a more elegant answer (without the use of \makeat... commands).
– emem
Nov 3, 2014 at 11:32

You have forgotten to include \usepackage{booktabs}.

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\toprule
@ Product name & @ Price & @ Count \\
\midrule
@ product & 60 & 2\\
@ book    & 90 & 4 \\
@ pen     & 40 &10\\
@ other   &100 &10\\
\midrule
@ Total & :={sumprod(b2:[0,-1];c2:[1,-1])} & sum(c2:[0,-1])\\
\bottomrule