It seems that one can't add \global before a \pgfmathsetmacro as that yields:
You can't use a prefix with `\begingroup'.
But that is the functionality I need here.
I am trying to compute the average of the integers in a list. In the MWE the list I have is
\newcommand*{\MyData}{3,7,X,Y,8,9,Z}%
and the \typeout messages show:
**** NumberOfValidEnteries=1, \AverageValue=3.0
**** NumberOfValidEnteries=2, \AverageValue=3.5
**** NumberOfValidEnteries=3, \AverageValue=2.66666
**** NumberOfValidEnteries=4, \AverageValue=2.25
So it seems as if the \pgfmathsetmacro is working except it is not saving its state between successive iterations of the \foreach, which is exactly what the \global was there for in the first place.
Code:
\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}
% http://tex.stackexchange.com/questions/50111/how-to-check-if-the-value-of-a-parameter-is-a-number
\newcommand*{\IfIsInteger}[3]{%
\IfStrEq{#1}{ }{%
#3% is a blank string
}{%
\IfInteger{#1}{#2}{#3}%
}%
}%
\newcommand*{\MyData}{3,7,X,Y,8,9,Z}%
\newcommand{\AverageValue}{0}
\newcounter{NumberOfValidEnteries}
\newcommand{\ComputeAverage}[1]{%
%% Initialize (in case this is used more than once)
\renewcommand{\AverageValue}{0}
\setcounter{NumberOfValidEnteries}{0}%
%
\edef\ExpandedDataEnteries{#1}% So that #1 can be a list or a macro defined list.
\foreach \NewData in \ExpandedDataEnteries {%
\IfIsInteger{\NewData}{%
%\global% <---- I need this ????
\pgfmathsetmacro{\AverageValue}{%
(\AverageValue \arabic{NumberOfValidEnteries} + \NewData) /
(\arabic{NumberOfValidEnteries} + 1)
}%
\stepcounter{NumberOfValidEnteries}%
\typeout{**** NumberOfValidEnteries=\arabic{NumberOfValidEnteries}, \string\AverageValue=\AverageValue}%
}{%
% Not integer data so skip it
}%
}%
}
\begin{document}
\ComputeAverage{\MyData}%
Average of integers in \MyData" is\AverageValue".
\end{document}
\pgfmathsetmacro\temp{...}\global\let\AverageValue\temp– egreg Mar 08 '14 at 22:47