pgf add tag
4 years ago JeT

I have a list of functions I define for multiple graphs along my course

Since they’re already written, why use PythonTeX (I thought about this interesting solution for calculations but a bazooka for a simple task) while I could use mathematical-function-in-pgf-tikz ?

However, I think the use of \pgfmathparse{int(d1(\x,\y,\KK,\RR,\SIG)/100}\pgfmathresult is quite heavy, so I must be doing someting wrong.

I could use a \newcommand like

but I lose the ability to name \d1 my macro. I also believe what I do here is not elegant (d!mn French with elegance issues 😃)

Hence my question :

I’d like to have a fluid way of calling the functions and get the result (for a set of parameters) in my text.

TAPgfCalulations.png

NB : I don’t mention here the roundings of the results. I know it’s a limitation.

Top Answer
4 years ago user 3.14159

This is slight variation of Rmano’s nice answer, merely for fun. I was wondering whether it is possible to steer the keys of siunitx with pgf keys. It is possible, but unless I am missing something basic one has to be a bit careful. (There are definitely improvements possible.) The command created here is \Formula, which takes one mandatory argument, the function that is to be parsed. And then there are options, which simultaneously allow one to steer siunitx stuff and pgf options. One can add siunitx keys with si+={...}. It does support things like \Formula[si+={round-precision=4,math-rm=\mathtt}]{binom(1,2,3)} which contain formatting macros like \mathtt. This works by copying the token mechanism of pgf keys. The parsed results also get remembered because sometimes one uses random numbers at some stage, and the subsequent results depend on the value of the random number. For instance, StoredResult(2) yields the result of the next-to-last computation. As usual, the rationale of using pgf keys here is to make the macro upgradable without losing backward compatibility.

Screen Shot 2020-12-17 at 5.11.27 PM.png

Answer #2
4 years ago Rmano

I use the declare function a lot for my texts for students (especially exams, in order to easily create variants). The precision and range of the calculation is often a problem, so that I have to pre-scale units (forget about going with 46e-6 or things like that); having the same fuctionality in l3fp would be great). But alas, it’s still very handy!

What I use normally is to get help by the siunitx package for formatting and printing. I have in my preamble:

and then in my text I can use

obviously you can pass additional parameters to control formats for printing, if needed. These macro are not expandable, but well — they do work in most cases.

Answer #3
4 years ago Diaa

I know this answer is not exactly what you are asking for but I think it is worth mentioning and might be useful to you or somebody else in one way or another.

If the supreme WolframScript is more suitable for you than SageMath, then package latexalpha2 is your new friend.

However, it only works on Linux devices. For Windows, I got you covered by having this great answer.

You must make sure first that you have first Wolfram Engine and WolframScript are installed in this order.

Finally, run the command wolframscript once to activate your free account created when downloading WolframScript.

image.png

image.png

image.png

Windows-tailored ‘latexalpha2Win.sty’

Answer #4
4 years ago Diaa

If you don’t mind externalization using a powerful computational engine, you might go with SageMath using sagetext.

image.png


sagetex with arara

For automating the build process, you better use the following arara rule (if you are working on Windows) posted in this answer https://tex.stackexchange.com/a/596828/2288 after making sagetex.sty of SageMath known to your latex distribution.

Answer #5
3 years ago JeT

Thanks to the package of the day, I discovered the powerfull numerica

4 years
JeT — Thursday, 17th Dec 2020 21:42

@marmot, re: your answer, wow 😃 You’ve just solved (another) long time problem for me. I have a full w/e (without kids) to integrate it to my course and I see already extensions to this…
1- Questions of exams that could be a variation in a list of values (so much cheating with covid and remote exams)
2 - Less static examples
3 - plenty more.
\Formula{StoredResult(2) is a great feature !

a day
JeT replying to user 3.14159 — Wednesday, 16th Dec 2020 20:37

More than I even thought about yesterday night !

an hour
user 3.14159 replying to JeT — Wednesday, 16th Dec 2020 19:50

I think one can blend \sisetup into this one so you can control the siunitx and pgf keys with a universal set of options. If I have time I may add something that does that explicitly.

JeT replying to user 3.14159 — Wednesday, 16th Dec 2020 19:48

Another interesting lecture on keys, merci !

JeT — Wednesday, 16th Dec 2020 19:40

@Rmano, re: your answer, I have to say the combination of your answer’s with Marmot’s gives a great result !! It integrates perfectly in my workflow.

2 hours
user 3.14159 — Wednesday, 16th Dec 2020 18:08

@Rmano, re: your answer, maybe a slight variation may help to keep the amount of macros one has to remember smaller:

14 hours
user 3.14159 replying to JeT — Wednesday, 16th Dec 2020 04:10

As for the suggestion to write a macro for every function: it can be done but it would be very unfortunate if you were to overwrite \sin, say, and were restricted to just letters because ordinary macro names cannot contain numbers, underscores and so on. So maybe you can just do something simple like a Parse and Print (PnP) macro:

2 hours
user 3.14159 — Wednesday, 16th Dec 2020 02:00

BTW, this issue is also loosely related. You could in principle construct a macro that just parses some expression and updates \pgfmathresult if it is inside a parse command or otherwise pretty-prints the result. However, as long as this is not officially supported such hacks will presumably always remain somewhat fragile.

an hour
user 3.14159 replying to JeT — Wednesday, 16th Dec 2020 00:53

pgfplots automatically makes use of fpu. (There are some rumors that suggest that it is very imprecise. To the best of my knowledge, apart from one fixable exception in trigonometric functions this is not the case. On the other hand, computations done without fpu can have errors that exceed 13%, and are subject to dimensions too large errors. xfp is rather precise but does not yet allow the user to declare functions, and also is limited to a subset of the functionality that pgf can provide, but on this subset it is rather accurate.)

user 3.14159 replying to JeT — Wednesday, 16th Dec 2020 00:47

I do not quite understand the purpose, but you could use your macros to store the last result somewhere where pgf can find it so that you can use it further. In principle this is the purpose of \pgfmathresult, but I am assuming that you want to keep several of those. This can be done with pgf, i.e. to create something that expands to your last result. If you want to pretty-print things, in general this can not be done with macros that are expandable for further computations, but this is also true for xfp (of course depending on what pretty-print means precisely, rounding you can do and expand).

an hour
JeT — Tuesday, 15th Dec 2020 23:27

Last question though. To simplify things in the text, would it make sense (it’s an opinion based question yes 😃) to have both (I know you don’t likerepeating things in your code)

and for each function its macro equivalent

?

JeT — Tuesday, 15th Dec 2020 23:19
  1. Well you rephrased my thought perfectly. Merci! I don’t see myself digging into LateX3 syntax TBH… I’ll stick to pgf.
JeT replying to user 3.14159 — Tuesday, 15th Dec 2020 23:09

On 1. I am currently reading the pgfmanual ! (back to basics I sometimes forget). Thanks for fpu you’ve just answered a long time question. Reading your cousin’s post maow on fpu.

user 3.14159 — Tuesday, 15th Dec 2020 23:06

@JeT, re: your question, I am not sure I understand the question correctly. There are two things that one can read into it.

  1. You want to format the number, i.e. round it to to digits. This is easy with pgf. See section 97 Number Printing of pgfmanual v3.1.7. You may also use fpu to avoid “dimension too large” errors.
  2. You want to create an expandable macro, i.e. use the result of \dOne in another macro. This is impossible with pgf. (This is not to say that you cannot forward the result of one computation to another function. This is possible.) If you want an expandable macro, you should look into xfp. As of now, this does not allow you to declare functions in a similar way as in pgf (but you can feed \fpeval with macros).

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.