5

I am working on a table template with Latex3 and want to calculate the sum of a column. My Problem is that i can't pass more than 9 parameters, which is necessary since I don't know how big the table will be.

Spreadtab package won't work.

Here a example:

\documentclass[a4paper,12pt]{article}   
\usepackage{xparse}                                                                         
\usepackage{fp}


\def\one{1}
\def\two{2}
\def\three{3}
\def\four{4}
\def\five{5}
\def\six{6}
\def\seven{}
\def\eight{8}
\def\nine{9}
\def\ten{1}

\ExplSyntaxOn
\NewDocumentCommand{\calcsum}{O{0}O{0}O{0}O{0}O{0}O{0}O{0}O{0}O{0}} %Here i need more than 9 arguments
   { \fp_to_decimal:n {#1 + #2 + #3 + #4 + #5 + #6 + #7 + #8 + #9}}             
\ExplSyntaxOff

\begin{document}

Result: \calcsum[\one][\two][\three][\four][\five][\six][\seven][\eight][\nine]

\end{document}
gernot
  • 49,614
ChrisF
  • 95
  • 2
    Is \seven empty on purpose? (Similar question for \ten.) Can you change the syntax to \calcsum[\one\two\three...\ten] or similar with commas or other separator? – Bruno Le Floch Dec 12 '16 at 15:18
  • 4
    Why not \calcsum{\one+\two+\three}? – egreg Dec 12 '16 at 15:18
  • @BrunoLeFloch \seven is empty on purpose because some cells may be empty. By adding 0 as default value, I always get the Sum of the whole table. – ChrisF Dec 12 '16 at 15:26
  • @egreg the macros i defined (one,two,three...) are just for testing. The numbers aren't really interesting. I need to calculate the sum for real with more than 9 arguments – ChrisF Dec 12 '16 at 15:29
  • 1
    @ChrisF No, the argument is given, so 0 is not substituted and you get ++ at the end. If you're able to feed \calcsum[\one][\two]..., then you're surely able to feed \calcsum{\one,\two,...}. In this case the solution is quite simple. – egreg Dec 12 '16 at 15:34
  • @egreg Sorry, but I am not sure I really understand. If I compile the code with \begin{document}

    Result: \calcsum{\one,\two,\three,\four,\five,\six,\seven,\eight,\nine}

    \end{document} I get as Result: 01,2,3,4,5,6,7,8,9. But I want the sum from 1 to 9. So as Result i am expecting 45

    – ChrisF Dec 12 '16 at 15:59
  • 1
    No command should have nine or more arguments. That's bad design. Reconsider how you want to pass your numbers. Use e.g. one argument with a comma list (or some other separator) and loop over it. – Ulrike Fischer Dec 12 '16 at 16:14

1 Answers1

9

Use a comma list separated list of values:

\documentclass[a4paper,12pt]{article}
\usepackage{xparse}
\usepackage{fp}

\def\one{1}
\def\two{2}
\def\three{3}
\def\four{4}
\def\five{5}
\def\six{6}
\def\seven{}
\def\eight{8}
\def\nine{9}
\def\ten{1}

\ExplSyntaxOn
\NewDocumentCommand{\calcsum}{m}
 {
  \clist_set:Nx \l_tmpa_clist { #1 }
  \fp_to_decimal:n { \clist_use:Nn \l_tmpa_clist { + } }
 }
\ExplSyntaxOff

\begin{document}

Result: \calcsum{\one,\two,\three,\four,\five,\six,\seven,\eight,\nine,\ten}

\end{document}

We use the fact that empty items are ignored by \clist_set:Nn.

enter image description here

egreg
  • 1,121,712