I want to create a automatically calculating invoice environment.
The table fits to the page perfectly (of course) with tabularx. However, the calculation goes wrong since LaTeX creates the table multiple times to find out the width of the X column.
The calculations work great with tabular but then the table is fully fitting on the page depending on the font settings.
So I tried to manually calculate the width of the one variable column but it does not really work smoothly. Is there a better way to solve this?
\documentclass{scrlttr2}
\usepackage{tgheros}
\renewcommand\familydefault{\sfdefault}
\RequirePackage{xspace}
% =====================================
% correct number displaying
\usepackage[mode=text, reset-text-series = false, separate-uncertainty, group-digits=integer, group-minimum-digits = 4]{siunitx}
\sisetup{group-separator = {,}, input-decimal-markers={,.}, output-decimal-marker = {,}}
\newcommand{\EURsimple}[1]{
\sisetup{minimum-decimal-digits=2, group-separator = {.}, output-decimal-marker = {,}}
#1
\sisetup{minimum-decimal-digits=0, group-separator = {,}, output-decimal-marker = {,}}
}
\newcommand{\EUR}[1]{
\num[minimum-decimal-digits=2, group-separator = {.}, output-decimal-marker = {,}]{#1},€
}
% =====================================
% automatic item numbering
\newcommand{\startnumberedlist}{\newcounter{numberedlistcounter}}
\newcommand{\numberedlistitem}{\stepcounter{numberedlistcounter}\thenumberedlistcounter}
% =====================================
% calculation
\RequirePackage{calculator}
\newcommand{\resetinvoicesum}{\ADD{0}{0}{\invoicesum}}
\newcommand{\addtoinvoicesum}[1]{\ADD{\invoicesum}{#1}{\invoicesum}\GLOBALCOPY{\invoicesum}{\invoicesum}}
% =====================================
% trying to calculate the needed width of the variable width column
\newlength{\invoicedescwidth}
\setlength{\invoicedescwidth}{\textwidth}
\newlength{\invoicerestwidth}
\settowidth{\invoicerestwidth}{\textbf{Pos. Anzahl Rechnungsbetrag Gesamtpreis}}
\addtolength{\invoicedescwidth}{-\invoicerestwidth}
\addtolength{\invoicedescwidth}{-15ex} % <= this is not variable enough for different fonts.
% =====================================
% invoice environment
\RequirePackage{booktabs}
\newenvironment{invoice}[1][]{\startnumberedlist\resetinvoicesum\par\noindent
\centering\begin{tabular}{rp{\invoicedescwidth}S[table-format=5.1]S[table-format=5.4]r}%
\toprule[0.7pt]\textbf{Pos.} & \textbf{Beschreibung} & \textbf{Anzahl} & \textbf{Einzelpreis} & \textbf{Gesamtpreis} \ \toprule[0.7pt]}
{\midrule[0.7pt]
&&& \textbf{Rechnungsbetrag} & \textbf{\EUR{\invoicesum}} \ \cmidrule[0.7pt]{4-5} \end{tabular}%
}
% invoiceitem, #1: description, #2: count, #3: price per item
\newcommand{\invoiceitem}[3]{
\numberedlistitem & #1 & #2 & \EURsimple{#3} & \MULTIPLY{#2}{#3}{\tempsum} \ROUND[2]{\tempsum}{\tempsum} \addtoinvoicesum{\tempsum} \EUR{\tempsum}\} % price calculation and sum up all prices
\usepackage{Blindtext}
\begin{document}
\begin{letter}{recipient}
\opening{Hello}
\Blindtext[1]
\begin{invoice}
\invoiceitem{Arbeitszeit in h}{800}{15}
\invoiceitem{Schrauben}{2134}{0.1}
\invoiceitem{Strommenge in kWh}{303.5}{0.3175}
\invoiceitem{Position C}{2}{1123.1}
\end{invoice}
\end{letter}
\end{document}
P.S.: I also tried tabularray but that does not seem to work with S columns of siunitx to arrange the numbers correctly at the decimal point.

