匯總所有成本並在文件的不同位置顯示結果

匯總所有成本並在文件的不同位置顯示結果

假設有一個很大的文檔,其中不同費用的成本列在不同的地方。在列出這些成本的每一點上,我不僅希望了解成本本身,還希望了解仍有多少預算可用。考慮到文件中指出的所有費用,包括文件中出現在其後面的費用。

微量元素

\documentclass{article}
\usepackage{hyperref}

\makeatletter
\newcommand\mylabel[2]{\def\@currentlabel{#2}\phantomsection\label{mylabel:#1}}
\newcommand\myref[1]{\ref{mylabel:#1}}
\makeatother

\newcounter{FixMoney}
\setcounter{FixMoney}{1000}
\newcounter{SaveValue}
\setcounter{SaveValue}{\value{FixMoney}}

\makeatletter
\newcommand{\calcCosts}[1]{%
\bgroup
\newcount\PP
\PP=#1 %Piece Price
\setcounter{SaveValue}{\inteval{\value{SaveValue}-\the\PP}}%
\mylabel{overall}{\the\value{SaveValue}}Piece price:\,\the\PP\,€ / Money left overall \myref{overall}\,€)
\egroup}
\makeatother
\makeatletter

\begin{document}
\calcCosts{3}\\
\calcCosts{30}\\
\calcCosts{400}
\end{document}

輸出 輸出

輸出正是我所期望的和我將實現的。我的左邊是某些東西的成本,右邊是剩餘的預算。

我收到以下警告!

標籤“mylabel:overall”被多重定義。

現在的問題是我\label為此目的使用的方法是否真的正確,或者是否可以透過不同的方式實現相同的功能。

答案1

可能是一種機制,您可以透過可選參數指定在何處放置超連結的錨點,並且交叉引用標籤可以滿足您的需求。

(您似乎不需要來自歐盟委員會的歐元符號(\euro在加載 Eurosym 後),而是來自歐洲計算機現代字體。)

\documentclass{article}

% \euro - euro-symbol which complies to the symbol suggested and
% and copyrighted(!) by the European Commission -
% <https://european-union.europa.eu/institutions-law-budget/euro/design_en> :
\usepackage{eurosym}  

% In correspondence
% <https://chat.stackexchange.com/transcript/message/63127536#63127536>
% David Carlisle revealed:
% Euro-symbol of European Computer Modern Fonts (ec - Computer
% modern fonts in T1 and TS1 encodings <https://www.ctan.org/tex-archive/fonts/ec>);
% in other words: Joerg Knappen's guess of what the euro symbol would be:
\newcommand\CMReuro{{%
  \fontfamily{cmr}\fontencoding{TS1}\selectfont
  \symbol{191}%
  %\texteuro
}}

\usepackage{hyperref}

\makeatletter
\ExplSyntaxOn\@ifundefined{inteval}{\cs_new_eq:NN \inteval \int_eval:n}{}\ExplSyntaxOff
%--Basic Infrastructure----------------------------------------------------
\newcommand\InitializeNewKindOfBudget[2]{%
  \@bsphack
  \expandafter\@ifdefinable\expandafter{\csname(BudgetNameSpace)#1\endcsname}{%
    \expandafter\protected@xdef\csname(BudgetNameSpace)#1\endcsname{\inteval{(#2)}}%
  }%
  \@esphack
}%
\newcommand\ReduceBudget[2]{%
  \@bsphack
  \expandafter\protected@xdef\csname(BudgetNameSpace)#1\endcsname{%
    \inteval{(\csname(BudgetNameSpace)#1\endcsname)-(#2)}%
  }%
  \@esphack
}%
\newcommand\CurrentBudget[1]{\csname(BudgetNameSpace)#1\endcsname}%
\newcommand\Budgetlabel[2]{%
  \@bsphack\begingroup
  \Hy@localanchornametrue
  \phantomsection
  \protected@edef\@currentlabel{\csname(BudgetNameSpace)#1\endcsname}%
  \label{#2}%
  \endgroup\@esphack
}
%--Infrastructure for your scenario----------------------------------------
\InitializeNewKindOfBudget{MoneyForPieces}{1000}%
\NewDocumentCommand{\calcCosts}{om}{%
  % #1 label to create; #2 amount of money to withdraw from MoneyForPieces.
  \ReduceBudget{MoneyForPieces}{#2}%
  \IfNoValueF{#1}{\Budgetlabel{MoneyForPieces}{#1}}%
  Piece price:\,#2\,\CMReuro\ / Money left currently \CurrentBudget{MoneyForPieces}\,\CMReuro\ / Money left overall \ref*{overall}\,\CMReuro)
}%
\makeatother

\begin{document}
\noindent Budget total: \CurrentBudget{MoneyForPieces}\\
\calcCosts{3}\\
\calcCosts{30}\\
\calcCosts[overall]{400}

% Be aware that the outcome both of the command \texteuro and the unicode-
% character € is engine-dependent and differs depending on whether using
% latex, pdflatex, xelatex or  lualatex and and may probably also vary
% between different LaTeX-releases.

\LARGE
\noindent
\verb|\euro|: \euro / \verb|\texteuro|: \texteuro / \verb|\CMReuro|: \CMReuro / \verb|€|: €

\end{document}

這是我在 TeX Live 2020 下使用 LuaLaTeX 編譯時得到的結果:

在此輸入影像描述



如果您不僅希望使用整數進行計算,還希望使用浮點數進行計算,這裡有一些用於非常基本的簿記的非常基本的例程:

\documentclass{article}
\usepackage{zref}
\usepackage{siunitx}
\usepackage{hyperref}

% \euro - euro sign which complies to the symbol suggested and
% and copyrighted(!) by the European Commission -
% <https://european-union.europa.eu/institutions-law-budget/euro/design_en> :
\usepackage{eurosym}  

% Euro sign of European Computer Modern Fonts (ec - Computer
% modern fonts in T1 and TS1 encodings <https://www.ctan.org/tex-archive/fonts/ec>);
% in other words: Joerg Knappen's guess of what the euro sign would be:
\newcommand\CMReuro{{%
  \fontfamily{cmr}\fontencoding{TS1}\selectfont
  \symbol{191}%
  %\texteuro
}}

\makeatletter
% A variant of \zref@setcurrent which sets properties globally
\ZREF@Robust\def\zref@setcurrentglobal#1#2{%
  \zref@propexists{#1}{%
    \expandafter\gdef\csname Z@C@#1\endcsname{#2}%
  }%
}%
\ExplSyntaxOn
%
%--Generic infrastructure--------------------------------------------------
%
\cs_new_protected_nopar:Npn \InitializeNewPostingAccount #1#2{
  \@bsphack
  \zref@newprop{PostingAccount#1}{0}
  \exp_args:Nnf
  \zref@setcurrentglobal{PostingAccount#1}{\fp_to_decimal:n{(#2)}}
  \@esphack
}%
\cs_new_protected_nopar:Npn \WithdrawFromPostingaccount #1#2 {
  \@bsphack
  \exp_args:Nnf
  \zref@setcurrentglobal{PostingAccount#1}{
    \fp_to_decimal:n{(\zref@getcurrent{PostingAccount#1})-(#2)}
  }
  \@esphack
}%
\cs_new_protected_nopar:Npn \SaldoOfPostingAccount #1 {
  \fp_to_decimal:n {(\zref@getcurrent{PostingAccount#1})}
}
\cs_new_protected_nopar:Npn \RecordSaldoOfPostingAccountToLabel #1#2 {
  \zref@labelbyprops{#2}{PostingAccount#1}
}
\cs_new_protected_nopar:Npn \ReferToRecordedSaldoOfPostingAccount #1#2#3 {
  \zref@ifrefundefined{#2}
    {\use_ii:nn}
    {\zref@ifrefcontainsprop{#2}{PostingAccount#1}{\use_i:nn}{\use_ii:nn}}
  {\PrintAmountOfMoney{\zref@extractdefault{#2}{PostingAccount#1}{0}}{#3}}
  {\zref@refused{#2}\textsf{??}}
}
\cs_new_protected_nopar:Npn \PrintAmountOfMoney #1#2 {
  \exp_args:Nne
  \use:n {\SI [{% Adjust this according to your needs and the
                % conventions in your country - refer to the
                % documentation of siunitx:
                 add-decimal-zero,
                 add-integer-zero,
                 detect-mode=true,
                 round-integer-to-decimal=true,
                 round-mode=places,
                 round-half=up,
                 group-digits=true,  
                 group-minimum-digits=2,
                 group-separator=\,,
                 output-decimal-marker={,}, 
                 round-precision={#2}}]}
  {\fp_to_decimal:n{round(((#1)+0),#2,NaN)}}{\CMReuro}
}
%
%--Infrastructure for your scenario----------------------------------------
%
\InitializeNewPostingAccount{MoneyForPieces}{1000.24}%

\cs_new_protected_nopar:Npn \WithdrawFromMoneyForPieces #1 {
  \WithdrawFromPostingaccount{MoneyForPieces}{#1}
  Piece~price:\nobreakspace
  \PrintAmountOfMoney{#1}{2}\nobreakspace/~
  Money~left~currently~\PrintAmountOfMoney{\SaldoOfPostingAccount{MoneyForPieces}}{2}\nobreakspace/~
  Money~left~overall~\ReferToRecordedSaldoOfPostingAccount{MoneyForPieces}{overall}{2})
}
\cs_new_protected_nopar:Npn\RecordFinalMoneyForPiecesSaldoToLabel {
  \RecordSaldoOfPostingAccountToLabel{MoneyForPieces}{overall}
}
\ExplSyntaxOff
\makeatother

\begin{document}

\noindent
\begin{minipage}{1.1\textwidth}
\noindent Budget total: \PrintAmountOfMoney{\SaldoOfPostingAccount{MoneyForPieces}}{2}\\
\WithdrawFromMoneyForPieces{3.24}\\
\WithdrawFromMoneyForPieces{30}\\
\WithdrawFromMoneyForPieces{400}%
\RecordFinalMoneyForPiecesSaldoToLabel
\end{minipage}

% Be aware that the outcome both of the command \texteuro and the unicode-
% character € is engine-dependent and differs depending on whether using
% latex, pdflatex, xelatex or  lualatex and and may probably also vary
% between different LaTeX-releases.

\LARGE

\bigskip

\noindent
Some different euro sign-notations yielding different symbols:

\noindent
\verb|\euro|: \euro/ \verb|\texteuro|: \texteuro/ \verb|\CMReuro|: \CMReuro/ \verb|€|: €

\end{document}

這是我在 TeX Live 2020 下使用 pdfLaTeX 編譯時得到的結果:

在此輸入影像描述

答案2

僅在文件末尾儲存一次整體值。我不會產生鏈接,它沒有多大意義,因為沒有與整體價值相關的位置。不要在命令中分配新的計數器,計數器應始終全域分配。

\documentclass{article}
\usepackage{hyperref}

\newcounter{FixMoney}
\setcounter{FixMoney}{1000}
\newcounter{SaveValue}
\setcounter{SaveValue}{\value{FixMoney}}

\newcommand{\calcCosts}[1]{%
\setcounter{SaveValue}{\inteval{\value{SaveValue}-#1}}%
Piece price:\,#1\,€ / (Money left overall \ref*{mylabel:overall}\,€)}

\makeatletter
\AddToHook{shipout/lastpage}{{\def\@currentlabel{\the\value{SaveValue}}\label{mylabel:overall}}}
\makeatother
\begin{document}
\calcCosts{3}\\
\calcCosts{30}\\
\calcCosts{400}
\end{document}

在此輸入影像描述

相關內容