Atreyagaurav / litex-mode

LiTeX mode for emacs; A minor mode to convert valid lisp expressions to LaTeX
GNU General Public License v3.0
43 stars 4 forks source link

Add support for Greek letter Variables like: \alpha, \beta, etc. #9

Closed Bu156 closed 2 years ago

Bu156 commented 2 years ago

Hi! I use Greek letters a lot in my Physics notes, the problem is that I have to modify the LiTeX output to get it to render properly, as LaTeX, by default does not seem to render Unicode in Math Mode. So I want to use the LaTeX defaults for greek letter like \rho, \alpha, etc and LiTeX mode doesn't seem to support that.

This can be observed with the following example:

If I write:

(setq F-l (* ρ V g))

LiTeX returns:

F_{l} = ρVg

But if I write:

(setq F-l (* \rho V g))

LiTeX returns:

F_{l} = rho \times Vg

So, a way to be able to use greek alphabets would be nice.

Since the ρ character does not render in math mode in org-mode. One way I've fixed this for myself is to use a function to replace all Greek letters with LaTeX counterparts (eg. α => \alpha), but I do not know if there are edge cases where this would be bad or if there's a better way to fix it. But not everyone can input unicode directly, so a way to use it without having to use unicode, and if possible a way to convert the unicode into LaTeX alternative while converting the lisp expression to LaTeX would be helpful.

Atreyagaurav commented 2 years ago

Hi I edited the description and title to explain the issue better. Since I think (* ρ V g) being converted to ρVg is as expected of the function, problem is we have to use the ρ unicode and no way to use \rho or convert it to \rho for now.

Bu156 commented 2 years ago

I have implemented it and it seems to work, should I initialize a pull request and maybe you could take a look at it?

Atreyagaurav commented 2 years ago

Sure. You don't have to ask, just commit your changes and make a pull request I'll let you know if it's ok as it is or needs some changes.

9viz commented 2 years ago

(setq F-l (* \\rho V g)) works okayish... "F_{l} = \\rho \times V g" so you need to "unescape" the symbol name so-to-speak.

P.S., suppose you have a symbol named V? it gets transliterated as V\? which is probably not what you want.

EDIT: If I pass t as the second (optional) argument to the prin1-to-string form in litex-format-variable, the problem is solved.

Atreyagaurav commented 2 years ago

@vizs did you look at the branch 9-greek-letters? I have tried to solve this issue by using a customizable variable to have option to just convert normal word with rho to \rho, user can turn it off of course, so it might be useful for people who doesn't want that.

I also found from reddit (prob you ?) we can suggest people to use C-u C-\ TeX RET for TeX input method, since my code works with unicode properly, we can use it without problem.

EDIT: I'll look for the unquoting though, if it can be implemented naturally it'll be good.

Bu156 commented 2 years ago

Maybe you could use single quotes to turn off conversion?

For example, you want the variable V? to appear as exactly that, maybe you could use 'V? to have it not convert. I think this behavior could fit quite naturally into elisp.

9viz commented 2 years ago

did you look at the branch 9-greek-letters? I have tried to solve this issue by using a customizable variable to have option to just convert normal word with rho to \rho, user can turn it off of course, so it might be useful for people who doesn't want that.

Yes, I did but the solution felt like a tape over the problem. I am not sure if special casing is scalable. Its your call tho.

Maybe you could use single quotes to turn off conversion?

For example, you want the variable V? to appear as exactly that, maybe you could use 'V? to have it not convert. I think this behavior could fit quite naturally into elisp.

Not sure what you mean here, but if you meant to say that unescaping should remain optional, then that would discourage users from using the package IMO. If you want to have "V\?" in the buffer for some reason, then naming the symbol "V\\?" instead of "V?" is sufficient.

Atreyagaurav commented 2 years ago

@vizs Wait, was adding optional t to princ1-to-string all I had to do? I added that now in branch 9, and it seems to work. I haven't done extended testings but it did pass some tests.

@Bu156 @vizs These are the tests I ran for the current version:

(ert-deftest litex-format-variable-test ()
  (let ((litex-make-unicode-to-latex t)
    (litex-make-name-to-latex-glyph t))
    (should (string= (litex-format-variable 'α) "{\\alpha}"))
    (should (string= (litex-format-variable 'α-β) "{\\alpha}_{{\\beta}}"))
    (should (string= (litex-format-variable 'α-Δ/t-2)
             "{\\alpha}_{{\\Delta}t}_{2}"))
    (should (string= (litex-format-variable '\\alpha) "\\alpha"))
    (should (string= (litex-format-variable '\\alpha-\\beta) "\\alpha_{\\beta}"))
    (should (string= (litex-format-variable '\\alpha-\\Delta\ t-2)
             "\\alpha_{\\Delta t}_{2}"))
    (should (string= (litex-format-variable 'alpha) "{\\alpha}"))
    (should (string= (litex-format-variable 'Delta/alpha)
             "{\\Delta}{\\alpha}"))
    (should (string= (litex-format-variable 'alpha/beta)
             "{\\alpha}{\\beta}"))))

If it looks like I missed something let me know, but it seems to work as intended at least for me. I also changed the two config variables to nil by default so, on default only \\alpha type variables work, but if you want to convert unicode or convert normal names then you have to set those config variables true. That makes sense for me now, let me know what you think.

9viz commented 2 years ago

Yes, all you had to do was set the optional argument to t, and everything looks okay to me (I admit that I did fewer tests than you).