我尝试创建一封发票信,自动计算发票金额。使用tabular
一切正常。tabularx
计数器工作正常,但计算不正常。我理解这是因为tabularx
需要多次运行表格代码来估计宽度并设置正确的X
列宽。
为什么这只会影响计算而不会影响计数器?
我尝试通过比较产品编号计数器numberedlistcounter
(显示正确)和计算计数器来解决问题sumcounter
,但没有成功。我做错了什么?
\documentclass{scrlttr2}
\usepackage[ngerman]{babel}
\usepackage[scaled]{helvet}
\renewcommand\familydefault{\sfdefault}
%===================================
% automatic counter for lists
\newcommand{\startnumberedlist}{\newcounter{numberedlistcounter}}
\newcommand{\numberedlistitem}{\stepcounter{numberedlistcounter}\thenumberedlistcounter}
\newcommand{\numberitemsinlist}{\thenumberedlistcounter\xspace}
%===================================
% display numbers according DIN 5008 (normal numbers with space as thousand separator and money amounts with . as separator)
\RequirePackage[detect-all, separate-uncertainty, group-minimum-digits = 4]{siunitx}
\sisetup{locale=DE, group-separator = {\,}, input-decimal-markers={,.}, output-decimal-marker = {,}}
\newcommand{\euro}[1]{
\num[round-precision = 2, round-mode = places, group-separator = {.}, locale=DE]{#1}\,€
}
%===================================
% invoice environment (needs \euro and siunitx)
\RequirePackage{booktabs}
\RequirePackage{calculator}
\RequirePackage{tabularx}
\newcommand{\resetinvoicesum}{\ADD{0}{0}{\invoicesum}
\newcounter{sumcounter}}
\newcommand{\addtoinvoicesum}[1]{
\ifx\numberedlistcounter=\sumcounter
\else
\ADD{\invoicesum}{#1}{\invoicesum}\GLOBALCOPY{\invoicesum}{\invoicesum}
%\thesumcounter
\stepcounter{sumcounter}
\fi
}
\newenvironment{invoice}[1][]
{\startnumberedlist\resetinvoicesum\par\noindent
\centering\tabularx{\textwidth}{cXS[table-format=5.1]rr}%
\toprule[0.7pt]\textbf{Pos.} & \textbf{Beschreibung} & \textbf{Anzahl} & \textbf{Einzelpreis} & \textbf{Gesamtpreis} \\ \toprule[0.7pt]}
{\midrule[0.7pt]
&&& \textbf{Rechnungsbetrag} & \textbf{\euro{\invoicesum}} \\ \cmidrule[0.7pt]{4-5} \endtabularx}
% invoiceitem, #1: description, #2: count, #3: price per item
\newcommand{\invoiceitem}[3]{\numberedlistitem & #1 & #2 & \euro{#3} &
\MULTIPLY{#2}{#3}{\tempsum} \addtoinvoicesum{\tempsum} \euro{\tempsum}\\} % price calculation and sum up all prices
%===================================
\begin{document}
\begin{letter}{}
\begin{invoice}
\invoiceitem{Arbeitszeit in h}{80}{15}
\invoiceitem{Schrauben}{2134}{0,1}
\invoiceitem{Strommenge in kWh}{303,5}{0,3175}
\invoiceitem{Werkzeug}{2}{1123,1}
\end{invoice}
\end{letter}
\end{document}
PS:此外,无论出于何种原因,最后一个包含总数的单元格没有像上面所有其他单元格一样向右对齐。这是为什么?