Math & ScienceBest package/environment for calculations

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
PMouse
Posts: 3
Joined: Wed Apr 27, 2016 8:51 am

Best package/environment for calculations

Post by PMouse »

I'm not brand new to TeX, but I want do start doing more complicated things. I've been looking at techniques for having TeX do calculations, rather than me having to do them in an external tool and then type in the result. But there are too many different ways to do it. I don't know which to choose or how to choose one to start learning.

My current application is just doing Physics/Math homework where I typeset the equations in TeX and then, sometimes, have actual numbers to plug into the final equation. It would be great if I could just copy the equation environment line, tell LaTeX the value of variables, and have LaTeX do the final calculation. But, if I have to rewrite the line, that's okay.

What do you suggest I start learning if I want to be able to do this and more complicated things down the road?

I've tried fp, xint, and l3fp, already, in fact. fp worked okay, but is poorly documented and I actually gave the wrong result in once case and I couldn't figure out why. xint is pretty nice, and seems to be well documented, but latex crashed when I tried to create a constant value in my personal .sty file, like pi, and it seems to be unable to calculate exponents < 1. l3fp sounds like a good one, but I couldn't getting it to work at all, although I gave it the least effort, to be fair. I haven't tried pgfmath, but I think I will, next. I love TikZ.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Best package/environment for calculations

Post by Johannes_B »

I am not really a fand of letting LaTeX do rather complex calculations. It just isn't meant to do the job.
Lua might be more suited and can be combined with LaTeX, see LuaLaTeX.

Letting a program calculate and input the result on the spot can be dangerous. Consider the following example
Test series B showed good result, only 17.000 mice died which is 170\,% of the whole population, whereas series B showed the disappointing amount of 13 dead mice. That is an incredible .13\,% of the population.
If you are looking for a cure to some desease ... Looking for a weapon of mass destruction? You achieved to kill 170\,% of something, congratulations, special badge crazy scientist unlocked. Typos can happen to everyone, leading to crazy results.


As you may notice, no, i am really not a fan of that stuff.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Best package/environment for calculations

Post by Stefan Kottwitz »

Besides pgfmath, there's tikzmath.

I would also consider LuaTeX for calculating.

Stefan
LaTeX.org admin
PMouse
Posts: 3
Joined: Wed Apr 27, 2016 8:51 am

Re: Best package/environment for calculations

Post by PMouse »

Interesting. Thanks for the tip. I'll read up on Lua.

If I end up agreeing with you, that the TeX build env is the wrong tool, how might I construct an alternative scheme? Say I do the calculation externally. Then, what is the best way to give TeX the results for type setting? Do others have something like this working, already?

For argument's sake, let's take the case of making exams with randomly chosen problem parameters and solutions with calculated values. This is one scenario I already know I want to tackle. How should I do this?

I can imagine having each calculated value in a separate file, somewhere, and then importing each file in TeX. That seems a little clunky; I'd have to design and maintain the filename scheme and import them, individually, at the right place, which would make eq environments ugly, if I understand how that would work. Or, maybe I could have one imported file with a macro for each calculated value; but this seems even worse since I'd have to make the calculation program output TeX syntax. (This is why I figure TeX should do it.)
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Best package/environment for calculations

Post by Stefan Kottwitz »

Here's a heavy math example with Lua: Weierstrass function with pgfplots.

In your case it can be much simpler.

There's also Python-TeX which could be used.

Stefan
LaTeX.org admin
Post Reply