reddit / emacs
LiTeX mode now available in melpa. For people who like lisp and like LaTeX do give it a look, you might like it.
thevoidzero · 2022-02-21 03:43
thevoidzero · 2022-02-21 03:46
I'm proud to finally contribute to the emacs community, after a year or so of learning elisp. I can't explain how much I love emacs and how I can make things like this.
Here is a little description:
LiTeX mode: converts lisp expressions to LaTeX expressions. Please look through the Github readme for more examples than the ones in the picture.
- Github: https://github.com/Atreyagaurav/litex-mode
- Melpa: https://melpa.org/#/litex-mode
- Youtube Demo: https://youtu.be/esEf63gSSPg
EcstaticBill2638 · 2022-02-21 04:15
This looks really cool, well done. I'm a bit confused by using `setq` as your syntax though, is that actually evaluated?
Would that overwrite my variable `Temp`?
tecosaur · 2022-02-21 04:21
Looks cool! One pet peeve: please use `\left(` / `\right)` for properly sized parens.
thevoidzero · 2022-02-21 04:25
Yes. Unfortunately. (I guess you can use local-setq with slightly less impact)
It's because I'm using emacs as a whole as an calculator.
I'm working on a option to make it all run on inferior lisp process, because there is also a problem of integer division along with variables definition. But I've not yet been able to make this, communication with slime seems to be hard looking at its code, I'll try to go through ob-lisp code next.
If I can somehow use a function equivalent of eval but does the eval in inferior process and gives the results then I can integrate it easily.
Contributions are welcome too.
thevoidzero · 2022-02-21 04:29
For exp function? I can modify that. Thanks.
Though it might make the latex expression hard to look at if I use it everywhere, I'll keep it as customization variable in next release.
EcstaticBill2638 · 2022-02-21 04:34
I wonder if you could use an arbitrary label for latex, convert that to a symbol and `let`-bind it internally.
thevoidzero · 2022-02-21 04:40
That sounds interesting, let binding things might be hard though, but i can probably try some things to make sure the variables are contained. It'll also help with the problem of someone doing (setq t 10) for t as time or sth. Which i don't think will run.
At least I can add prefix to variable names internally like 'litex-var-' and then make a get variable function to get values. This should not be hard to implement. I'll put this as next item in my todo list.
m0r1tz_1337 · 2022-02-21 04:54
Tan Q
AuroraDraco · 2022-02-21 05:28
Oh yeah, thats a good call
tecosaur · 2022-02-21 06:36
It occurs to me that it would be fantastic if you could somehow leverage calc for more advanced operations (e.g. symbolic integration).
LinkHimself · 2022-02-21 08:49
On the other hand, if it's not used everywhere it is needed, then the pdf output is hard to read.
miraunpajaro · 2022-02-21 09:32
Dang it. Now I have to learn Emacs just to use this wizardry!
wenoc · 2022-02-21 12:15
No matter how many people love LaTeX, this number will still be zero as there isn’t a person alive who likes Lisp.
thevoidzero · 2022-02-21 12:22
Just means you don't know them. Coz I know people who love lisp more than any other language. And it's my 3rd fav language.
thevoidzero · 2022-02-21 12:24
Just do it. Org mode alone is worth it. Only thing I didn't have in org-mode to help me with was this so I made it. The spreadsheet like table calculations and running codes is super useful.
thevoidzero · 2022-02-21 12:24
True. I'll implement it. Since the latex is just intermediate and I don't have to type it out.
wenoc · 2022-02-21 12:24
Yes I guess I don’t. Back in the 90’s our introduction to programming in the university was Scheme for those of us in computer science, apparently to give everyone an equally miserable time. The assistants probably liked it. Everyone else at uni had Java.
I mean I guess that was fair to all as half the fresh CS kids had written tons of C or Pascal but nobody had heard of Lisp.
Still felt like a punch in the face at the time. Welcome select few, to our prestigious university. Mobile phones are the new hot thing so let’s learn a bizarrely backwards language that you will never have any use for at Nokia.
Taught me to think differently though. Maybe it was good for me.
thevoidzero · 2022-02-21 12:30
I've been thinking that for a while too. Don't know how I can do that. We do have the minimal symbolic function in that (* 2 x) is converted to 2x instead of 2 \times x, but it only works if first arg is numeric, and I only made it work if variable is single character (to not cause problems) so it's not in this image.
Calc uses normal expressions, if it would somehow return me a lisp sexp after symbolic operation with variables intact I could definitely make it. And I'd probably also need a way to know if sexp can be solved further or not.
thevoidzero · 2022-02-21 12:31
You're welcome.
TheBB · 2022-02-21 13:15
Nice idea, but the LaTeX generated is quite poor.
- You're just writing 'Temp' as 'T' multiplied by 'e' etc. Variables in equations should be typeset as a word, and not italicized. Same with 'vapor' and 'sat'.
- 'exp' should be `\exp`, not `\mathrm{exp}`. What if I want to use non-roman font?
- The parentheses need to be large enough fit the expression inside. Use `\begin(` and `\end)` and so on.
- Why is the denominator of that fraction parenthesised?
- Eqnarray has been on the shame list for a while now. Use the `align` environment instead.
- Why is the equation numbered but the eqnarray isn't?
In general I might appreciate something like this for typesetting expressions. I wouldn't want it to actually *evaluate* anything for me step-by-step. I can't think of any writing I've done in LaTeX where I would want that. I'd prefer if, by default, only the expression were rendered, and there were another command to expand its evaluation.
Bu156 · 2022-02-21 14:17
Hey! I couldn't find anything about using greek characters, how would I go about, say using \rho to represent density? Thank you for making this cool package!
thevoidzero · 2022-02-21 15:12
Oh hi, yeah that seems to be a weak point, I didn't consider that as I just directly type ρ,α,β and such in unicode and it works fine in latex. Are you in linux? if that's the case you can setup compose key to easily to be able to type unicode greek characters.
but you definitely raised a big issue, maybe I should introduce some variables or functions to be able to use greek letters, being able to type it in unicode would be best though.
thevoidzero · 2022-02-21 15:44
Thank you for the feedbacks. If you look at the links to the stackexchange in readme, I'm adding more features as I need it, and thought I'd make it available since others might find this useful and might help me develop it. Just these feedback are also super helpful, some of them I've worked on, some I couldn't solve yet and some are completely new.
> You're just writing 'Temp' as 'T' multiplied by 'e' etc. Variables in equations should be typeset as a word, and not italicized. Same with 'vapor' and 'sat'.
so if it's multicharacter variable I should use \mathrm? I can add that.
> 'exp' should be \exp, not \mathrm{exp}. What if I want to use non-roman font?
Oh yeah, \mathrm{func} is general and default format, as we add more formatting function for each functions it'll start to use their formatting. I think I added function for expt but I didn't do exp, mostly because I wasn't sure I should do it `e^{}` or `exp()`, but I'll add it soon. There is also a variable containing functions like `sin`, `cos` that map 1:1 into latex, I'll add exp there. If you know more of such functions or where I can find them please let me know.
> The parentheses need to be large enough fit the expression inside. Use \begin( and \end) and so on.
Someone else also mentioned that, I'll do that.
> Why is the denominator of that fraction parenthesised?
The logic to whether to put parenthesis or not has been hard to develop so I'm keeping it on the safe side for now.
> Eqnarray has been on the shame list for a while now. Use the align environment instead.
It's already in dev, I'll merge that to main soon.
> Why is the equation numbered but the eqnarray isn't?
The variables are customizable, I'm using following values as default:
- litex-math-equation-start “\begin{equation}\n”
- litex-math-equation-end “\n\end{equation}\n”
- litex-math-eqnarray-start “\begin{eqnarray*}\n”
- litex-math-eqnarray-end “\n\end{eqnarray*}\n”
TheBB · 2022-02-21 15:52
> so if it's multicharacter variable I should use \mathrm? I can add that.
I prefer `\text{}`, so that it uses the normal font of the document. Mathrm enforces roman type. Or maybe `\operatorname` is better.
> If you know more of such functions or where I can find them please let me know.
https://www.overleaf.com/learn/latex/Operators#Reference_guide
Also note https://www.overleaf.com/learn/latex/Operators#Defining_your_own_operators
thevoidzero · 2022-02-21 16:31
Thank you I'll look through it and implement it.
thevoidzero · 2022-02-21 16:39
Sorry I didn't see it.
> I'd prefer if, by default, only the expression were rendered, and there were another command to expand its evaluation.
It's already there, I was only showing this as a demo. If you look through the README in github you can see that the function `litex-sexp-to-latex-exp` (https://github.com/Atreyagaurav/litex-mode#litex-sexp-to-latex-exp) does exactly that. It doesn't evaluate or solve anything but converts the last sexp into latex expression, doesn't even put it in any environment so you can be free to use it anywhere.
The command I'm using for this examples are: `litex-sexp-solve-all-steps-equation` and `litex-sexp-solve-all-steps-eqnarray` which solve it step by step in the mentioned environment.
thevoidzero · 2022-02-21 17:16
The benefits of having a community :)
cueball404 · 2022-02-21 17:50
Have you looked at https://github.com/johnbcoughlin/calctex?
Bu156 · 2022-02-21 18:13
Yeah, that's what I did to fix it, but the problem is that in inline rendering of LaTeX in org mode, \\rho gets rendered correctly, while ρ (unicode) doesn't render at all in math mode. If there's anything I can do to fix this, that would definitely be a good solution.
EDIT: ~~As~~ u/TheBB ~~mentioned, typesetting variables as words would fix my issue with it not displaying correctly~~ tried this and it ended up not working, I have a temporary solution, but an officially supported one would be welcome :D
thevoidzero · 2022-02-21 19:09
Wait how can ρ not be rendered? like it's it already rendered? Though I don't use inline render for org so I might not have seen it before.
I tried `org-latex-preview` and yeah it seems to not show the unicode characters at all. character × works though, idk why.
I personally don't put latex snippets in org nowadays, I actually write my expressions in lisp src block, and let it be there so I can get the values. Then after I export it to latex, I use my minor mode functions to convert all the lisp expressions to latex ones, fine tune it there and finalize the pdf. I also compare the final value to the value of lisp block output.
Bu156 · 2022-02-21 19:13
I use Emacs to both write and view my notes, so that's why it's good to have it work with previews. I have fixed the issue for myself by replacing all instances of α-ω/Α-Ω with their LaTeX counterparts inside inline LaTeX.
thevoidzero · 2022-02-21 19:17
This is indeed tricky, would you like to make it as a issue in the github? Maybe someone can help, or I'll try to think about how to make this later, for now I'm a bit busy with irl stuffs and just fixing other things for this, and it looks like it'll need some real hard thinking.
Bu156 · 2022-02-21 19:18
Sure! I think this project is really interesting and if I have time I might try implementing a proper solution and contributing code.
thevoidzero · 2022-02-21 19:31
Thank you for that.
One idea I have currently is to have an option to convert unicode greeks like (α,β,..) to their latex conterpart, so you can have unicode in lisp and latex \alpha in converted latex expressions. But it'll only work for people like us who input direct unicode, the greek letters do need some thought as people are likely to want to use it.
> by replacing all instances of α-ω/Α-Ω with their LaTeX counterparts inside inline LaTeX.
If you have a elisp function for this, then we could use that for this implementation at least.
thevoidzero · 2022-02-21 20:35
On a cursory glance it looks a bit slow in that gif idk if it's just the gif. But I'll look at it later.
miraunpajaro · 2022-02-21 22:54
I tried spacemacs, because I couldn't get any lisp plugin to work with vim. I didn't like it that much, but Emacs "vanilla" I like it better.
Also it's really cool that you can use Emacs binding in Jupyter notebook, it's a small thing but you can code a lot faster.
_viz_ · 2022-02-22 06:47
> Oh hi, yeah that seems to be a weak point, I didn't consider that as I just directly type ρ,α,β and such in unicode and it works fine in latex. Are you in linux? if that's the case you can setup compose key to easily to be able to type unicode greek characters.
>
>
C-u C-\ TeX RET
sunnyata · 2022-02-22 08:40
No harder than handwritten LaTeX if it's properly formed, because that's how the parens should be done.
thevoidzero · 2022-02-22 13:15
A bit if explanation would have been nice.
If it's to enter greek letters's latex command i have cdlatex or something like that which makes it easier. I just prefer unicode.
If it's for entering in unicode then it'll be useful.
_viz_ · 2022-02-22 13:23
Its for entering unicode. C-h I TeX RET for more info
thevoidzero · 2022-02-22 14:38
Thanks it's definitely useful, I have it setup with compose key, but this method is universal as long as you're in emacs so it's best to recommend it to others too I guess.
thevoidzero · 2022-02-22 16:42
True, I implemented it, and made it a customizable variable just in case. except for few extra brackets sometimes due to bracketing algorithm not being perfect it looks way nicer.
thevoidzero · 2022-02-22 16:43
Hmm never tried it, thanks for that info. I've to use jupyter notebook to demo some things in class next and I possibly can't use emacs coz students need to follow the demo, so that helps.