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}
7
  • 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? Dec 12, 2016 at 15:18
  • 4
    Why not \calcsum{\one+\two+\three}?
    – egreg
    Dec 12, 2016 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, 2016 at 15:26
  • 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, 2016 at 15:34
  • 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. Dec 12, 2016 at 16:14

1 Answer 1

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

0

You must log in to answer this question.

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