reddit / emacs

LiTeX mode: Do calculations in Lisp, convert them to LaTeX

thevoidzero · 2022-02-13 16:30
Hello everyone, I have been using emacs and writing simple functions for a while, I had also tried to make minor modes previously, and have made things that were super useful to me, but those were so niche that I didn't feel like it's be useful. But this time I made something that I think might be useful to others so I'm sharing it here. This is a minor mode that can do well with LaTeX or org-mode. What it does is: convert valid lisp expressions to LaTeX expressions. As you know we can evaluate lisp expressions directly in emacs (C-x C-e), so I had this thought why not use emacs as a calculator while typing the text without the need to move to another buffer to calculate things. But the problem was I couldn't leave the lisp expressions as it is if I have to submit the paper somewhere. So I had been manually converting them to mainstream expressions. But now I have LiTeX mode for that. It can convert expressions like: (+ 2 3 (* 6 x)) ⇒ 2 + 3 + 6 x (setq x 5) ⇒ x = 5 (setq time-0 1) ⇒ time_{0} = 1 (defun fun (x) x+2) ⇒ \mathrm{fun}(x):x+2 (sin (* 2 θ)) ⇒ \sin(2θ) (sin (* 2 time)) ⇒ \sin(2 \times time) (setq y (+ x (expt x 2) (fun x))) ⇒ y = x + x^{2} + \mathrm{fun}(x) It also has helper functions besides the main conversion like: 2.12e34 ⇒ 2.12 \times 10^{34} Even do step by step solve for homeworks like: (setq y (+ 2 3 (* 6 x))) ⇒ \begin{eqnarray*} y &=& 2 + 3 + 6 x \\ &=& 2 + 3 + 6 \times 5 \\ &=& 2 + 3 + 30\\ &=& 35 \end{eqnarray*} You can do step by step solve and get results in lisp expressions, or equation environment (steps rendered in same line) or eqnarray environment (steps rendered in different lines). NOTE: the steps by steps evaluation needs to get the values of variables to solve things, so be sure to evaluate all expressions before running it. All the activities of functions are controlled by variables you can customize. Like the format for float is "%.3f", you can change those and many more. Demo Image: https://imgur.com/bRKFsSy Github: https://github.com/Atreyagaurav/litex-mode Video demo: https://youtu.be/esEf63gSSPg
[deleted] · 2022-02-13 17:22
Maybe you'd be interested in this, the name is almost the same ;) https://alejandrogallo.github.io/listex/
thevoidzero · 2022-02-13 18:02
I saw this but it was more of a writing latex in lisp. So I made my own. Like that example with frac command. Listex had something like (-frac 1 2), and so on. Which does convert to latex but I can't do calculations on it. Did you make this?
thevoidzero · 2022-02-13 18:31
I found out it is yours from your profile, it's really impressive. I found it a while back when I was searching stackexchange for converting tools, but the reason I wrote my own after finding listex was because it was a bit different than what I wanted. listex seems to focus on getting the equations in latex, so you write expressions while thinking of how you want it to look on latex. While in litex I want to forget latex, and just do normal calculations in lisp. While I'm writing thing down I'll just write a lisp expression and eval it to use emacs as a calculator. So, I found myself writing lisp expressions to calculate in comments and then normal expressions to show it in document. then though I don't wanna do that, and wrote a python based templating system to calculate python expressions, then 6 months later came up with this. If i didn't have to evaluate expressions to get numerical values and just write complex expressions in latex, then I'm very much likely to use your package.
[deleted] · 2022-02-13 19:10
sure, it is nevertheless cool!, I get where you're coming from, that is also useful for many people I'd say. Good luck !