# How to detect if expansion takes place while evaluating a \pgfmathparse-expression/if inside a \pgfmathparse-expression?

I intend to write a macro which behaves differently depending on whether used within a \pgfmathparse-expression or used outside a \pgfmathparse-expression.

For example a commend \MyNicePi which expands to the sequence pi within a \pgfmathparse-expressions not nested between "..." and to the control-word-token \textpi otherwise.

(The sequence pi is a recognized function inside \pgfmathparse-expressions.
When loading the package textgreek, then\textpi in horizontal and vertical modes delivers the symbol π.
" is the quote-operator in \pgfmathparse-expressions, i.e., things in quotes are expanded but not evaluated by \pgfmathparse.)

So the questions are:

Does the mathematical engine of PGF bring along a test or a switch for finding out if currently evaluation of a \pgfmathparse-expression is taking place?

If not:

• What is the best way to implement such a test oneself?
• What criteria can be used for determining whether currently evaluation of a \pgfmathparse-expression is taking place?

Something like:

\documentclass{article}
\usepackage{textgreek}
\usepackage{tikz}

\newcommand\MyNicePi{%
% A test for checking if pgfmathparse-evaluation takes place and either
% pi or \textpi
}%

\begin{document}

\leavevmode\MyNicePi{} is approximately
\pgfmathparse{\MyNicePi}\pgfmathprintnumber[fixed, precision=3]{\pgfmathresult}\,.

\end{document}


delivering the same as

\documentclass{article}
\usepackage{textgreek}
\usepackage{tikz}

\begin{document}

\leavevmode\textpi{} is approximately
\pgfmathparse{pi}\pgfmathprintnumber[fixed, precision=3]{\pgfmathresult}\,.

\end{document}


• I think you can check whether \pgfmath@parse@next is defined. \pgfmathparse works inside a group and one of the first things it does is to define \pgfmath@parse@next, so it should be reliable in both cases. With your example \ifdefined\pgfmath@parse@next seems to work. Nov 6 '20 at 11:55
• @PhelypeOleinik \ifdefined\pgfmath@parse@next doses not take PGF's "-operator into account. I.e., with \makeatletter\newcommand\MyNicePi{\ifdefined\pgfmath@parse@next\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi{pi}{\textpi}}\makeatother the expression \pgfmathparse{"\MyNicePi"}\pgfmathresult yields pi instead of \textpi. Nov 6 '20 at 13:10
• Oh, I overlooked that one (rather important :) requirement. Indeed \pgfmath@parse@next or egreg's method answers only “am I in \pgfmathparse?”. It's a lot trickier, as \pgfmathparse pre-expands its argument, so when it knows it's inside quotes, \MyNicePi has long been expanded. I can hack something, but it won't be pretty Nov 6 '20 at 13:29
• @PhelypeOleinik Thank you, but don't spend time on that. 1) I can do the hack myself, I just didn't know if something was available already and I wanted to avoid reinventing the wheel. ;-) 2) I posted a feature-request to the developers of TikZ/pgf. Nov 6 '20 at 13:51

I see no particular way to recognize whether TeX is in the middle of performing \pgfmathparse, but it's not difficult to add a test, exploiting the fact that the definition of \pgfmathparse begins with \begingroup.

\documentclass{article}
\usepackage{textgreek}
\usepackage{tikz}
\usepackage{etoolbox}

\newif\ifudparse
\patchcmd{\pgfmathparse}
{\begingroup}
{\begingroup\udparsetrue}
{}{}

\newcommand\MyNicePi{\ifudparse pi\else\textpi\fi}

\begin{document}

\MyNicePi{} is approximately
\pgfmathparse{\MyNicePi}\pgfmathprintnumber[fixed, precision=3]{\pgfmathresult}\,.

\MyNicePi{} is approximately half of
\pgfmathsetmacro\foo{2*\MyNicePi}\pgfmathprintnumber[fixed, precision=3]{\foo}\,.

\end{document}


The example shows that also \pgfmathsetmacro works (because it calls \pgfmathparse internally).

Also Phelype Oleinik's idea to test for \pgfmath@parse@next is good, but has the defect of depending on the particular name. Instead, \begingroup has to be performed by \pgfmathparse. Of course, if the maintainers decide to change the top level definition and to hide \begingroup in another macro, we're doomed.

I think this should be a feature request to the developers of TikZ/PGF.

• Thanks for your suggestion. As the if-switch is set to true by \pgfmathparse no matter what will parsed, PGF's quote-operator " is not taken into account. I.e., \pgfmathparse{"\MyNicePi"} yields pi instead of \textpi. I will send a feature request. Nov 6 '20 at 13:15
• @UlrichDiez Sorry, but one should delve deep in the parser, I'm afraid. Perhaps there's a way to do \udparsefalse there, but there's full expansion lurking around… Nov 6 '20 at 15:21