reddit / emacs

math-preview updated to MathJax3

seregaxvm · 2022-05-12 19:04
[math-preview](https://gitlab.com/matsievskiysv/math-preview) got updated to MathJax3. It now supports TeX, MathML and AsciiMath equations.
T_Verron · 2022-05-13 12:07
Cool! Is my understanding correct that the use of mathjax allows this to be faster than preview-latex for typesetting only a few equations? For a whole document on the other hand, it seems that the performances of both are quite comparable. math-preview does a good job at picking up user-defined commands, but it doesn't always recognize commands defined in packages (I'm not even sure if that would be even ossible: are all latex packages compatible with mathjax?). It opens up dreams of a hybrid solution, e.g. with preview-latex typesetting previews in the background for the entire buffer, and math-preview quickly regenerating previews at point...
seregaxvm · 2022-05-13 14:54
> For a whole document on the other hand, it seems that the performances of both are quite comparable. `math-preview` renders [this 1800 line document](https://gitlab.com/matsievskiysv/bi2d/-/raw/master/doc/derivation_real.md) in about 3 seconds. It probably could be optimized to be faster, but IMHO this is fast enough. > math-preview does a good job at picking up user-defined commands, but it doesn't always recognize commands defined in packages MathJax only knows commands, that are listed in [this page](http://docs.mathjax.org/en/latest/input/tex/macros/index.html), it does not speak TeX. MathJax3 has more [extensions](http://docs.mathjax.org/en/latest/input/tex/extensions/index.html) available than MathJax2, so some previously missing functionality may be available now.
T_Verron · 2022-05-13 16:38
> It probably could be optimized to be faster, but IMHO this is fast enough. Your document is not a latex document, so preview-latex cannot deal with it. That's a use-case I was not thinking of. On the latex document of mine where I tried the mode (1300 lines, some of them long [I use 1 line per sentence], tons of maths fragments and unrecognized macros), preview-latex takes 8s (async) and math-preview 15s (sync). I wouldn't say that one is worse than the other, but they are both too long to wait for. preview-latex offers more accurate previewing, since it does actually render the document in the background. So all user macros, packages, even references... are correctly expanded. On the other hand, even for previewing a single fragment, rendering a document leads to significant overhead, while math-preview is instantaneous. That's why I imagine that there would be room for a best-of-both-worlds hybrid solutions (at least for latex and org documents). At my humble user level, I will start by rebinding local preview-latex commands with math-preview, and leaving the global previews untouched. > MathJax only knows commands, that are listed in this page, it does not speak TeX. I see. Those commands include \newcommand, \let, \def and so on, so in that sense it does speak a little bit of tex, and I guess that's why it could pick up some of my user macros. Why some and not others is a bit of a mystery though. For example `\newcommand{\Wnb}[1][]{\mathbf{W}_{\mathbf{n}}^{\rm b#1}}` (I'm pretty sure to be the only user of that command with that definition, so no way it was hardcoded anywhere :) ) is later correctly expanded, but `\newcommand{\NN}{\mathbb{N}}` is not. Given that the elisp side also supports explicitly supplying commands and packages, maybe it would be possible to parse the preamble (or even read off auctex's parsing) and extract such declarations automatically?
seregaxvm · 2022-05-13 17:06
> Given that the elisp side also supports explicitly supplying commands and packages, maybe it would be possible to parse the preamble (or even read off auctex's parsing) and extract such declarations automatically? I don't see any reason this could not be done with the built-in facilities. You just add marks for preamble, so it would get into MathJax, maybe preprocess it if needed and then use your declarations until background process is stopped.
AdjectivePronoun · 2022-06-08 23:53
Can a user configure beginning end ending tags to recognize as math? In Markdown/Org-mode/LaTeX, I use $ and \\\[\\\]'s, but for PreText, I wrap math in <m> and <md> tags.
seregaxvm · 2022-06-09 04:34
Yes, it is done from the customize menu.
AdjectivePronoun · 2022-06-09 13:33
Thanks! For anyone finding this later, after using `customize` the variables and moving them to my config outside the customize menu are: ```lisp (setq math-preview-tex-macros '(("ddx" "\\frac{d#2}{d#1}" 2 "t") ("and" . "\\mbox{ and }")) math-preview-tex-marks '(("\\begin{equation}" "\\end{equation}") ("\\begin{equation*}" "\\end{equation*}") ("\\[" "\\]") ("$$" "$$") ("<m>" "</m>") ("<me>" "</me>") ("<mrow>" "</mrow>"))) ```
wakatara · 2022-12-01 09:29
Weirdly, I am having trouble getting this to work. I actually want to \*disable\* things like a $ triggering math-preview since it messes up my pages where I am discussing money or other things. So, this seems to get the $$ working (thanks!) but my bigger concern is how to get \*only\* a few marks to render math rather than adding more to the math-preview rendering (mostly because I do a math-preview-all org-mode-hook rather than manually triggering it on and off. &#x200B; Handy customize snippet though. Thank you!
wakatara · 2022-12-02 06:36
For followup,and for others in similar positions I received a note from the author on gitlab and used the following setup for others who may need it: ```elisp (use-package math-preview :straight t :custom (math-preview-command "/opt/homebrew/bin/math-preview")) (add-hook 'org-mode-hook #'math-preview-all) (setq math-preview-scale 2.0) (setq math-preview-tex-marks-inline '(("$$" "$$"))) (setq math-preview-tex-marks '(("\\begin{equation}" "\\end{equation}") ("\\begin{equation*}" "\\end{equation*}") ("$$" "$$") )) ``` The key seems to be explictly setting the `math-preview-tex-marks-inline` variable. Works like a charm now. ❤️