Resuma todos os custos e mostre o resultado em diferentes locais do documento

Resuma todos os custos e mostre o resultado em diferentes locais do documento

Digamos que haja um documento grande no qual os custos de diferentes despesas estão listados em locais diferentes. Em cada ponto onde estes custos são listados, gostaria de ter não só os custos em si, mas também uma visão geral de quanto orçamento ainda está disponível. Tendo em conta todos os custos indicados no documento, incluindo os que aparecem a seguir no documento.

MWE

\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}

SAÍDA Saída

O resultado é exatamente o que eu esperaria e o que alcançaria. Tenho o custo de algo à esquerda e o orçamento restante à direita.

MAS

Recebo o seguinte aviso!

Rótulo `mylabel:overall' multiplicado definido.

Agora surge a questão se a minha abordagem de uso \labelpara esse fim está realmente correta ou se a mesma funcionalidade pode ser alcançada de uma maneira diferente.

Responder1

Provavelmente um mecanismo onde você pode especificar através de argumento opcional onde colocar a âncora para hiperlinks e o rótulo de referência cruzada faz o que você deseja.

(Parece que você não deseja o símbolo do Euro da Comissão Europeia (isso seria \eurodepois de carregar o eurosym), mas das Fontes Modernas de Computador Europeu.)

\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}

Isto é o que obtenho ao compilar com LuaLaTeX no TeX Live 2020:

insira a descrição da imagem aqui



Caso você queira calcular não apenas com números inteiros, mas com números de ponto flutuante, aqui estão alguns tipos de rotinas muito básicas para uma contabilidade básica:

\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}

Isto é o que obtenho ao compilar com pdfLaTeX no TeX Live 2020:

insira a descrição da imagem aqui

Responder2

Armazene o valor global apenas uma vez no final do documento. Eu não produziria links, não faz muito sentido pois não há localização associada ao valor global. Não aloque um novo contador dentro do seu comando, os contadores devem sempre ser alocados globalmente.

\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}

insira a descrição da imagem aqui

informação relacionada