3

This works for

\test{500}

but not for let say

\test{50000}

\documentclass[10pt,german]{article}

\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel} 
\usepackage[utf8]{inputenc}  
\usepackage{pgffor} %foreach    


\newcommand{\Beleg}[2]{%   
\expandafter\xdef\csname BelegKontozweiSoll#1\endcsname{#2} 
}


\newcommand{\test}[1]{%
\pgfmathsetmacro{\saldoo}{#1}  
        \foreach \x in {10,20,30}{%
                \pgfmathsetmacro{\saldo}{\csname BelegKontozweiSoll\x\endcsname+\saldoo} 
                \global\let\saldoo=\saldo 
        }   
\textbf{\saldoo}
}



\begin{document}
\Beleg{10}{50.60}
\Beleg{20}{145.05}
\Beleg{30}{603.28}


\test{500}
\end{document}

how can i pass this problem? Note: in general my tex file is a little bit longer, so i want to keep

\csname .... \endcsname

vice versa, i have to change a lot of things in my original tex file, which i wont do that.

Someone have an idea?

3 Answers 3

4

Don't use pgf for such large numbers. As it internally uses dimensions its range is a bit restricted. With expl3 and its fp-library there is no problem:

\documentclass[10pt,german]{article}

\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgffor} %foreach
\usepackage{siunitx} %loads expl3
\sisetup{locale=DE}

\newcommand{\Beleg}[2]{%
\expandafter\xdef\csname BelegKontozweiSoll#1\endcsname{#2}
}

\ExplSyntaxOn
\newcommand{\test}[1]
 {
  \fp_set:Nn\l_tmpa_fp{#1}
  \clist_map_inline:nn {10,20,30}
  {
   \fp_add:Nn \l_tmpa_fp { \use:c{ BelegKontozweiSoll##1} }
  }
  \textbf{\num[detect-weight]{\fp_eval:n{\l_tmpa_fp}}}
 }
\ExplSyntaxOff


\begin{document}
\Beleg{10}{50.60}
\Beleg{20}{145.05}
\Beleg{30}{603.28}


\test{50000}
\end{document}
5
  • Thanks a lot! But i dont like to work with this. Is it possible to combine FPeval with csname...endcsname? If it is possible, then i also can use let say round(..) and a lot of other stuff which is easy to use. expl3 is a little bit weird... Commented Apr 16, 2019 at 8:33
  • Sure replace \use:c {...} by \csname ... \endcsname. \use:c is only a wrapper around this primitive (and shorter to type ...). Commented Apr 16, 2019 at 8:36
  • could you please adapt the codes to fpeval so i can see explicit how do you mean that. I will be very very apretiate Commented Apr 16, 2019 at 8:37
  • Hm. I think you should better get used to expl3. The colons and underscore may look a bit weird at the first glance, but you get quite fast used to it - better than \csname, \xdef and \expandafter. round() and so works there fine too. You can e.g. replace \fp_add:Nn by \fp_set:Nn . Commented Apr 16, 2019 at 8:53
  • can you please read my new answer. I have written down what im looking for. Commented Apr 16, 2019 at 9:07
0

I mean something like this:

\documentclass[10pt,german]{article}

\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel} 
\usepackage[utf8]{inputenc}  
\usepackage{pgffor} %foreach    
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp   -> FPeval
\usepackage{fp} 

\newcommand{\Beleg}[2]{%   
\expandafter\xdef\csname BelegKontozweiSoll#1\endcsname{#2} 
}


\newcommand{\test}[1]{%
\pgfmathsetmacro{\saldoo}{#1}  
        \foreach \x in {10,20,30}{%
                \FPeval{\saldo}{ {\csname BelegKontozweiSoll\x \endcsname} + \saldoo} 
                \FPeval{\saldoo}{\saldo} 
        }   
\textbf{\saldoo}
}



\begin{document}
\Beleg{10}{50.60}
\Beleg{20}{145.05}
\Beleg{30}{603.28}


\test{50000}
\end{document}

This works also fine for 50000 but FPeval doesnt take the value

{\csname BelegKontozweiSoll\x \endcsname}

Can you please adapt this part of the code? So then i have not to use expl3. I like FPeval....

2
  • You are forgetting that \foreach builds a group, the calculated value can't escape. Commented Apr 16, 2019 at 9:22
  • I got it! Solution below :) Finally, puhh! Thanks a lot Ulrike Commented Apr 16, 2019 at 9:22
0

Now i got it :) - its possible to combine FPeval with csname ... endcsname - its possible that the calculated value in foreach is global (xdef....)

\documentclass[10pt,german]{article}

\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel} 
\usepackage[utf8]{inputenc}  
\usepackage{pgffor} %foreach    
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp   -> FPeval
\usepackage{fp} 

\newcommand{\Beleg}[2]{%   
\expandafter\xdef\csname BelegKontozweiSoll#1\endcsname{#2} 
}


\newcommand{\test}[1]{%
\FPeval{\saldoo}{#1}  
        \foreach \x in {1,...,5}{% 
                \FPeval{\saldo}{ \csname BelegKontozweiSoll\x \endcsname + \saldoo}  
                \FPeval{\saldoo}{\saldo} 
                \xdef\saldoo{\saldo}
        }    
\saldoo
}



\begin{document}
\Beleg{1}{25}
\Beleg{2}{25}
\Beleg{3}{25}
\Beleg{4}{100}
\Beleg{5}{100}

\test{50000}
\end{document}

Finish :)

2
  • You shouldn't assign \saldoo both locally and globally, it will use up your stack. (fp does it wrong too in quite some places, and that's why e.g. this exploded: tex.stackexchange.com/questions/459626/…. One reason why I don't use fp). Commented Apr 16, 2019 at 9:36
  • ok thanks! i changed it to something else. Commented Apr 16, 2019 at 9:40

You must log in to answer this question.

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