6

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}
\usepackage{spreadtab,booktabs,xpatch}
\begin{document}

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

\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. Table using raw text

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:

Table after using input

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?

4
  • 1
    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

2 Answers 2

4

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*}
\usepackage{spreadtab,booktabs,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
\begin{document}
\begin{spreadtab}{{tabular}{lcc}}
    \toprule
    @ Product name & @ Price & @ Count \\
    \midrule
    \input{products}
    \midrule
    @ Total & :={sumprod(b2:[0,-1];c2:[1,-1])} & sum(c2:[0,-1])\\
    \bottomrule
\end{spreadtab}
\end{document}
2
  • 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
-2

You have forgotten to include \usepackage{booktabs}.

Your code could be:

\documentclass{article}
\usepackage{booktabs}
\usepackage{spreadtab}
\begin{document}
\begin{spreadtab}{{tabular}{lcc}}
\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
\end{spreadtab}
\end{document}

Result:

spreadtab

3
  • 2
    I don't think,that this is the answer to the question.
    – user31729
    Sep 15, 2014 at 5:28
  • I agree with Christian. Maybe, you can have a look to pgfplotstable, which appears to be able to accomplish to OP requests and correct the answer. Sep 15, 2014 at 12:39
  • I did not gave a complete latex code. Obviously, for booktabs commands to work one need to include this package. This was not the question.
    – emem
    Nov 3, 2014 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .