Calculating a real inside an environment

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.
\begin{itemize}
\item Result \result
\end{itemize}
The result afterwards: \result
\end{document}


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

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

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

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.)

\documentclass{scrartcl}
\newcommand\inicalc{ \directlua{x=0}}
\newcommand\showcalc{\directlua{tex.sprint(x)}}

\begin{document}
\inicalc
\noindent
\begin{itemize}
\item \inicalc      Result0: \showcalc\ (Re-initialize the variable inside the itemize environment.)
\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

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.
\begin{itemize}
\item Result \result
\global\let\gresult\result
\end{itemize}
The result afterwards: \gresult % was \result before! edited by Jan
\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.
\begin{itemize}
\item Result \fpprint\result

• 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