0

I want to perform simple arithmetic operations for a document in a table in the tabularx environment. There is a column for the quantity, a column for the unit price and a column for the total price, which is the result of the quantity and the unit price. At the end of the table, the total prices per row are to be summed up.

Is there a simple way how I can implement this requirement within the Tex document? Is it possible to access single values (e.q. the total sum) in a later part of the document?

Example-Code:

\begin{tabularx}{\textwidth}{c|X|r|r}
\hline
\tiny {Quantity} & \tiny {Description} & \tiny {Unit price} & \tiny {Quantity * Unit Price} \\ \hline
1  & Product 1 & 2,00 EUR &2,00 EUR \\
\hline
2 &  Product 2 & 5,00 EUR & 10,00 EUR \\
\hline
3 & Product 3 & 4,00 EUR & 12,00 EUR \\
\hline
\multicolumn{ 3}{l}{ all round price } &24,00 EUR \\ \hline
\end{tabularx}

Example result: enter image description here

Thanks a lot in advance,

BR

Christoph

2
  • 3
    spreadtab will be a good choice. Jan 29 at 14:30
  • 2
    Off-topic: \tiny is a switch and does not take an argument. You might as well replace \tiny {Quantity} with \tiny Quantity, etc.
    – Mico
    Jan 29 at 14:55

1 Answer 1

3

I think this will do the basic jobs:

\documentclass{article}
\usepackage{spreadtab}
\usepackage{numprint}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{siunitx}
\renewcommand{\familydefault}{\sfdefault}
\nprounddigits{2}
\STsetdecimalsep{{,}}
\newcommand{\Eur}{{\,EUR}}

\begin{document}
\begin{table}[htbp]
\begin{spreadtab}{{tabularx}{\textwidth}{c|X|N{4}{2}<\Eur|N{4}{2}}}
  \hline
@\tiny Quantity & @\tiny Description & @\multicolumn{1}{r|}{\tiny Unit price} & @\multicolumn{1}{r}{\tiny Quantity * Unit Price} \\ \hline
1 & @Product 1 & 2 & :={a2*c2} \Eur \\
\hline
2 & @Product 2 & 5 & :={a3*c3} \Eur \\
\hline
3 & @Product 3 & 4 & :={a4*c4} \Eur \\
\hline
\multicolumn{3}{l}{@all round price } & :={sum(d2:d4)} \Eur \\ \hline
\end{spreadtab}
\end{table}
\end{document}

Edited:

  • to make the code simpler;
  • Let the quantity column be an integer.

result

2
  • Thanks a lot Teddy van Jerry, one additional question: Can I also implement this with relative references? So I can copy the rows without changing the formulars and always have the total sum for all rows? Jan 29 at 17:39
  • 1
    I found a solution by myself. Use 27 & @Product 1 & 0.35 & :={[-3,0]*[-1,0]} \Eur\\ for the single line, for the sum you can use \multicolumn{3}{l}{@total price } & :={sum(d1:[0,-1])} \Eur \\ Thanks a lot for your help! Jan 29 at 18:32

You must log in to answer this question.

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