1

Long story short: I'm trying to come up with a system to generate a LaTeX table dynamically from a YAML file through Pandoc and do some calculations using the spreadtab package.

So far so good, the table gets output as it should and spreadtab does its job wonderfully.

I have a problem now, though. I need to find a way to tell spreadtab the range of cells whose values need to be summed. Since the table gets generated dynamically, this parameter needs to be dynamic as well. And since there is no way with Pandoc to keep track of the number of iterations in a loop with a counter, I need to create said counter with LaTeX.

This is the solution I came up with (spoiler: it doesn't work):

\newcounter{pos}
\setcounter{pos}{0}

\begin{spreadtab}{{tabular}[t t t]{lp{8.2cm}r}}
   @ Pos. & @ Leistung/Beschreibung & @ Preise in EUR \\ \hline
   @ \refstepcounter{pos} \thepos & @ Die erste Leistung   & 750.0 \\ 
   @ \refstepcounter{pos} \thepos & @ Die zweite Leistung  & 180.0 \\ 
   @ \refstepcounter{pos} \thepos & @ Die dritte Leistung  & 55.0 \\ 
   @ \refstepcounter{pos} \thepos & @ Die vierte Leistung  & 55.0 \\ 
   \hline
  @ & @ \multicolumn{1}{r}{Nettobetrag:} & :={sum(c1:c\thepos)} \\
\end{spreadtab}

The tricky bit is in the last line of the table. After incrementing the counter at every line, my plan was to use the same counter in the spreadtab formula to set the range of the cells that need to be summed. Unfortunately, spreadtab doesn't seem to expand the counter when it reads the table, and it returns the error: Invalid range in cell C6.

I thought this through and decided that a solution to this is beyond my powers. So now I'm asking you: How to use a dynamic parameter that spreadtab can recognize, or how to tell spreadtab to expand the counter command before executing the formula?

Thanks for any help.

4
  • You know it would be easier to help you if you provided complete examples. You want sum(c2:[0,-1]) instead of sum(c1:c\thepos), see the spreadtab manual for relative references.
    – cgnieder
    Nov 1, 2015 at 12:20
  • What do you mean with complete? Like, standalone documents you can copy-paste and compile on the fly? Anyway, that's brilliant and simple, I didn't think about relative references. Thanks again, @clemens!
    – mrzool
    Nov 1, 2015 at 12:32
  • By complete I mean that I can copy and paste it without having to add \documentclass, \begin{document} and \end{document} and any \usepackage{...}. In other words: I mean a minimal working example
    – cgnieder
    Nov 1, 2015 at 12:36
  • Gotcha, will do in the future.
    – mrzool
    Nov 1, 2015 at 12:40

1 Answer 1

2

I propose another solution: use relative referencing (as described in the spreadtab manual), i.e., use sum(c2:[0,-1]) instead of sum(c1:c\thepos). (Note I exchanged c1 for c2c1 is the head of the column and the first number to be summed up to is in c2.)

Using the table from your other question:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{spreadtab,array}
\usepackage{arydshln}
\renewcommand{\arraystretch}{1.5}
\usepackage{hhline}
\newcounter{pos}

\begin{document}

\STautoround*{2}
\STsetdecimalsep{,}
\begin{spreadtab}{{tabular}[t t t]{lp{8.2cm}r}}
  \hdashline[1pt/1pt]
  @ \textbf{Pos.} &
  @ \textbf{Leistung/Beschreibung} &
  @ \textbf{Preise in EUR} \\ \hline
  @ \refstepcounter{pos} \thepos & @ Die erste Leistung  & 750.0 \\
  @ \refstepcounter{pos} \thepos & @ Die zweite Leistung & 180.0 \\
  @ \refstepcounter{pos} \thepos & @ Die dritte Leistung & 55.0  \\
  @ \refstepcounter{pos} \thepos & @ Die vierte Leistung & 55.0 \\
  % @ \refstepcounter{pos} \thepos & @ Noch eine Leistung  & 12.0 \\
  @ \refstepcounter{pos} \thepos & @ Noch eine Leistung  & 56.3 \\
  % @ \refstepcounter{pos} \thepos & @ Noch eine Leistung  & 987.0 \\
  % @ \refstepcounter{pos} \thepos & @ Noch eine Leistung  & 45.0 \\
  % @ \refstepcounter{pos} \thepos & @ Noch eine Leistung  & 31.31 \\
  \hline
  @ &
  @ \multicolumn{1}{r}{\textbf{Rechnungsbetrag:}} &
  \textbf{:={sum(c2:[0,-1])}}
  \\ \hhline{~~-}
\end{spreadtab}

\end{document}
1
  • Works perfectly and it's way simpler and more robust than my approach. Also thanks for spotting the "fierte" typo ;)
    – mrzool
    Nov 1, 2015 at 15:28

You must log in to answer this question.

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