4

I am trying to calculate real numbers within an environment. Unfortunately, I'll have to display the result of the calculations outside the environment.

This is my minimal example.

\documentclass{scrartcl}
\usepackage{fp}
\begin{document}
\FPset{\result}{0.0}
Result was globaly set to \result.
\FPset{\adda}{6.224}
\FPset{\addb}{5.113}
\begin{itemize}
\item Result \result
\item \FPadd{\result}{\result}{\addb} Result2: \result
\item \FPadd{\result}{\result}{\addb} Result3: \result
\item \FPadd{\result}{\result}{\addb} Result4: \result
\end{itemize}
The result afterwards: \result
(I'd expect 15.339 instead!)
\end{document}

Is there any way, to use the global variable, instead of the local variable?

I already tried to use

\FPadd{\global\result}{\result}{\adda}

in different combinations, but without any effect (besides of error messages).

Has anybody a hint to me?

A LuaTeX-solution would also be appreciated.

Thanks in advance!

Bye

Jan

  • Will you be working with one variable only, and will it always be initialized to 0? – Mico Dec 6 '16 at 18:16
  • @Mico the above code is just an example. I normally will define a variable and also set its value to something useful and defined status. In my actual project, I do have indeed two different variables, that need to be manipulated in similar environments and do therefore be exported to global values. – Jan Dec 7 '16 at 13:33
4

Since you've asked for a LuaLaTeX-based solution...

I'm interpreting your objective as wanting to operate on and display just one variable. (Let me know if this is not the case.) The code sets up three LaTeX macros named \inicalc, \addto, and showcalc. They work by invoking \directlua to execute some appropriate Lua code. Observe that the argument of \addto can be a number or a macro, which is assumed to evaluate to a number. (No input sanity checking is actually performed on the argument of \addto.)

enter image description here

\documentclass{scrartcl} 
\newcommand\inicalc{ \directlua{x=0}}
\newcommand\addto[1]{\directlua{x=x+tonumber(#1)}}
\newcommand\showcalc{\directlua{tex.sprint(x)}}

\newcommand\adda{6.224}
\newcommand\addb{5.113}

\begin{document}
\inicalc
\noindent
The varialble is initialized globally to \showcalc. After adding \adda, we have \addto{\adda}\showcalc.
\begin{itemize}
\item \inicalc      Result0: \showcalc\ (Re-initialize the variable inside the itemize environment.)
\item \addto{\addb} Result1: \showcalc % argument is a LaTeX macro
\item \addto{\addb} Result2: \showcalc
\item \addto{5.113} Result3: \showcalc % argument is a numeric constant
\end{itemize}
The result after exiting from the list: \showcalc---as expected.
\end{document}
  • thank you very much. This is exactly, what I was looking for! I really want to calculate the resulting $x$ within an environment, but use it as a global variable. Perfect! – Jan Dec 7 '16 at 13:21
2

Perhaps the easiest way is to simply copy the value to a global variable

\documentclass{scrartcl}
\usepackage{fp}
\begin{document}
\FPset{\result}{0.0}
Result was globally set to \result.
\FPset{\adda}{6.224}
\FPset{\addb}{5.113}
\begin{itemize}
\item Result \result
\item \FPadd{\result}{\result}{\addb} Result2: \result
\item \FPadd{\result}{\result}{\addb} Result3: \result
\item \FPadd{\result}{\result}{\addb} Result4: \result
\global\let\gresult\result
\end{itemize}
The result afterwards: \gresult % was \result before! edited by Jan
(I'd expect 15.339 instead!)
\end{document}

I'd probably go with something from expl3, which has a global variant of a 'set fp' command:

\documentclass{scrartcl}
\usepackage{expl3,xparse}
\ExplSyntaxOn
\NewDocumentCommand \addtototal { m m }
  { \fp_gset:Nn #1 { #1 + #2 } }
\NewDocumentCommand \fpset { m m } { \fp_set:Nn #1 {#2} }
\NewDocumentCommand \fpprint m { \fp_use:N #1 }
\ExplSyntaxOff
\begin{document}
\fpset\result{0.0}
Result was set to \fpprint\result.
\fpset\adda{6.224}
\fpset\addb{5.113}
\begin{itemize}
\item Result \fpprint\result
\item \addtototal\result{\addb} Result2: \fpprint\result
\item \addtototal\result{\addb} Result3: \fpprint\result
\item \addtototal\result{\addb} Result4: \fpprint\result
\end{itemize}
The result afterwards: \fpprint\result
(I'd expect 15.339 instead!)
\end{document}

(Internally these are not directly printable values, hence the need for an accessor function.)

  • Some misuse of variables? ;-) – egreg Dec 6 '16 at 20:46
  • Hi Joseph, thank you very much. I think, the first example, using \gloabl\let\gresult... should read The result afterwards: \gresult, in order to use the newly created global variable. At least, with that correction, it worked to me. – Jan Dec 7 '16 at 13:28
  • @egreg Can you explain your comment? – Jan Dec 7 '16 at 13:29
  • @jan Variables are not declared and are used both locally and globally. – egreg Dec 7 '16 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.