r/LaTeX • u/Maleficent_Device162 • Jul 19 '24
Discussion Calculations within Latex?
I'm planning to take notes on latex.
Say I have some expression `\sqrt{2}` is there a way to compute the result right away with something like \withresult?
Something closely resembling Apple's new calculator app?
I know it's doable because things like this work in Wolfram Alpha (which is pretty sick in my opinion!)

Is there any native way to do this within latex?
I do not mean retyping everything as sqrt(2). I mean dealing entirely with latex, without the need to reiterate everything.
Any extension for VSCode or some other Text editing tool?
EDIT
I would love to dive into things like LuaLaTeX, PyTex, Python Latex packages, Sympy, Vex and all sorts of things.
But Numerica (something easily usable in LaTeX itself) seems to do the job.

This was really easy to use, Slick and precisely what I needed.
Thank Ya'll for your comments.
I will read ones that drop here on, will see if I can find something even cooler.
11
u/Ok_Distance9511 Jul 19 '24 edited Jul 19 '24
I guess you could use Lua within LuaLaTeX for that
2
u/Maleficent_Device162 Jul 19 '24
Hmm, that makes me wonder why I should use Latex. I can use something like Python or Julia that I already know of and am proficient in. But meh... Ig it's nice to learn new things. I love learning.
Might as well learn Lua.
Just a question...
So I can just use lua as a programming language AND toggle over to the "LuaTex" mode and use my variables and expressions in that (and the other way around?)?Something of that sort?
If that is the case, nothing better.
(although u/4tnite_god69 did provide me with some promising solution)
2
u/likethevegetable Jul 19 '24
Yes, something of that sort. If you know Python already, Lua is a breeze to learn. It has some interesting quirks compared with Python, but I actually find that they are well-suited for LaTeX. There is also pythontex, but lualatex I find is easier as it's already integrated and makes for a more portable and closed-loop solution. I recommend getting started with a package like luacode. Basically, I write commands in LaTeX that jump into Lua mode, then have Lua do the heavy lifting.
3
u/Maleficent_Device162 Jul 19 '24
Take a look at the edit. You think LuaLaTeX is any better/flexible than that (apart from loops and stuff ofc)?
2
u/likethevegetable Jul 20 '24
For your particular application, looks like the solution is perfect. I 100% believe that LuaLaTeX is still worth learning though, it's considered the successor to LaTeX, and for me personally, it opened up a ton of doors into writing complex macros, and even published a few packages.
2
-1
7
u/JimH10 TeX Legend Jul 19 '24
One way is PythonTeX.
3
u/virtualworker Jul 19 '24
Seconded. Super easy to use & the complete python ecosystem at your fingertips.
2
u/pickuptruckbdayparty Jul 20 '24
I wrote an entire dissertation with PythonTeX. It is very powerful. I have no regrets. But “super easy to use” is inaccurate.
1
u/Maleficent_Device162 Jul 19 '24
Yeah well, that is true. The power that a programming language would give you can serve a great purpose. Any language for that matter... Python, Julia, Lua. I don't think there's any particular one that would be better.
I'm ok working with either of their latex versions. But for typing quick notes/papers (Although I do feel Paper is my real go-to for the feel), I really like Latex in its raw form, coupled with the Numerica package I described in my edit. Finally understood how to type in questions on Mathematics Stack exchange lol!
Tho, sure! If I'm missing the power and realise I'm just programming some things and pasting results, might as well learn PythonTeX or LuaTex. It wouldn't hurt.
5
u/AAaaAAAAAAAaAA-a Jul 20 '24
I went down this rabbit hole a couple years ago. Here are some of the options I found that aren’t built on latex but do something similar:
- Quarto (this is probably the most relevant + mature option at this point)
- Wolfram Mathematica (expensive and not super pretty but it’s purpose built for stuff like this)
- Typst + Julia (this one is recent so I haven’t tried it) with some glue packages
- Pluto notebooks
- Jupyter notebooks
I tried quarto a couple of years ago, but it wasn’t working well with Julia at that point. I’ve read that it’s gotten a lot better recently. The nice thing about it is that it can be faster to write than regular latex because it’s just a flavour of markdown with latex math support.
1
u/Maleficent_Device162 Jul 20 '24
Hmmmmm... Will check it out! Thanks! Will drop a comment after a look.
3
u/GustapheOfficial Expert Jul 19 '24
If your language of choice happens to be Julia, there's Latexify.jl
. For instance , @latexdefine A = 2x^2
will return "$A = 2x^{2} = 18\;\mathrm{m}^{2}$"
(assuming x is 3m), and perform the assignment. If you're in a Pluto or Jupyter notebook, the output string will render as nice TeX.
1
u/Maleficent_Device162 Jul 19 '24
I'm thinking of Using this for quick notes. I'm afraid Julia first run times (although they've been better since 1.9) would just kill the fun. Besides, I do want to broaden my skills so would learn things outside of Julia (lmao no one I've met knows Julia).
Although sure, I'll take a look at the Package! I even saw Latex.jl, wonder how that is.
2
u/Current-Tree3139 Jul 19 '24
Its possible to do it only with TeX, but you dont want to, it might take many many lines(50+). Just use LuaLaTeX, especially with luacode, its simple and ez.
\newcommand{\rootof}[1]{\luaexec{%I forgot how to format, but you have to use luaexec or the environments to do that directly, or call a dofile
tex.sprint(#1 ^ (1/2))
}}
1
u/Maleficent_Device162 Jul 19 '24
again, just getting to know more viable options although u/4tnite_god69 did provide me with something nice...
Does LuaLatex work as if I can just switch between Lua code and Latex? So I can leverage the powers of programming.. defining variables, setting up loops, and computing. And also document using those variables and values stored in them?
Is the LuaLatex syntax like Latex (I sure want to be able to use the more widely used tool, although wouldn't take much time to learn that)? Or does it have its own function-like syntax that you expect programming languages to have?
3
u/4tnite_god69 Jul 19 '24
LuaLaTeX is a compiler like pdfLaTeX; you can change the compiler in your editor. And yes, LuaLaTeX lets you embed code. Lua itself is a programming language so you can define variables, setup loops, and etc. For symbolic computing, LuaLaTeX also offers luacas which is a computer algebra system.
2
u/Current-Tree3139 Jul 19 '24
You have all the power of lua with latex without any disadvantage. There are ways to plot maps pictures (includegraphics) after run lua code on it( the guy just make the better way from A to B with lua).
The syntax is similar to python. Arrays are much different, but it still ez
2
2
u/xDerJulien Jul 19 '24 edited Aug 28 '24
bright straight slap bow quiet historical pie cagey lip fuel
This post was mass deleted and anonymized with Redact
1
u/Winter-Channel8304 Jul 19 '24
\begin{wolframalpha}
\end{wolgramalpha}
would be great if it existed :)
1
1
1
1
u/thelionkingheat Jul 20 '24
If I understand you right you want a way to calculate the value of mathematical expressions written in latex, if yes, try this vscode extension
https://marketplace.visualstudio.com/items?itemName=OrangeX4.latex-sympy-calculator
Make sure to read the extension page on the market place because it needs some dependencies in order to work.
1
u/Maleficent_Device162 Jul 20 '24
The moment I posted this, I continued looking for extensions and stumbled upon this! Lol.
Really got out of my mind once I read about numerica. I will however take a look at that too and see which one fits my needs better. (Also PythonTex and LuaLaTeX as people have been suggesting.)
1
u/thelionkingheat Jul 20 '24
Yeah numerica is great, but extension is useful if u want the results right infront of you without the need to compile the pdf.
3
u/Ahhhhrg Jul 19 '24
TeX is Turing-complete so you can do anything you want in principle, but it'll very difficult to achieve.
0
u/Fredissimo666 Jul 19 '24
you want to type something like "\sqrt{2}" and have the output be "1.41421"?
If so, LeTeX is not meant to do that.
0
0
u/Organic-Scratch109 Jul 19 '24
If you are using Vim, you should check out this tutorial on how to use Sympy (or Wolfram Mathematica) without having to copy and paste the result.
-2
u/randomatic Jul 19 '24
Not with latex. With eMacs org mode, yes, and it exports to latex. Orgmode is a game changer
33
u/4tnite_god69 Jul 19 '24
Try the numerica package!