Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

\pgfmathparse calls every function *twice* #2482

Open
xworld21 opened this issue Jan 10, 2025 · 1 comment · May be fixed by #2485
Open

\pgfmathparse calls every function *twice* #2482

xworld21 opened this issue Jan 10, 2025 · 1 comment · May be fixed by #2485

Comments

@xworld21
Copy link
Contributor

Tweak any PGF function to have a side effect, for example edit pgfmath.code.tex.ltxml as follows:

    e          => sub { NoteSTDERR('evaluating `e`'); $E; },

Then run the innocuous

\pgfmathparse{e}\pgfmathresult

LaTeXML will print 'evaluating e' twice!

Aside from the performance impact, which is probably negligible, this is making it impossible to replicate PGF's random number generator because rand gets called twice every time.

@xworld21
Copy link
Contributor Author

Quick word: this is actually pretty obvious in hindsight. You are slightly abusing the descent parser by evaluating the fuctions within the subrules. It's normal for a rule to be evaluated multiple times as the parser tries each branch, then backtracks when it fails, causing this behaviour. So this is an incompatibility between the current design and the presence of functions with side effects (rnd, rand). One should either create a parsing tree which is evaluated at the end, or for simplicity create an equivalent valid perl string which can be eval'd. Or, carefully write the grammar so that such failures do not happen, which feels incredibly fragile.

I suppose there might be a way to stash the random generator state within the parser so it can be rolled back on failure but it doesn't sound very maintainable, if it can be done at all.

@dginev dginev added this to the LaTeXML-0.9 milestone Jan 10, 2025
@xworld21 xworld21 linked a pull request Jan 11, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants