reddit / emacs

Why LaTeX preview doesn't work in org-mode for mhchem snippets?

sewageseller · 2022-11-26 18:01
I'm facing a very similar issue as per question [Make "org-latex-preview" load package so that it properly renders "tcolorbox" environments](https://emacs.stackexchange.com/q/60696/39427), using the `mhchem` latex package (version 4.09). I'm using Emacs 28.1, org-mode 9.5.5, pdfTeX 3.141592653-2.6-1.40.22, ImageMagick 6.9.12 running on Fedora 37. `org-latex-packages-alistis` set to `'(("version=4" "mhchem" t))` I have tried setting `org-preview-latex-default-process` to `dvipng`, `dvisvgmor`, or `imagemagick`. When the org file is exported to latex and PDF, the `mhchem` syntax is correctly interpreted and exported. And when calling `org-latex-preview` on `\(2 + 3\)` a preview is correctly generated. However, when calling `org-latex-preview` on `\ce{Na2CO4}` nothing happens. ​ Linked from [https://emacs.stackexchange.com/questions/74719/why-latex-preview-doesnt-work-in-org-mode-for-mhchem-snippets](https://emacs.stackexchange.com/questions/74719/why-latex-preview-doesnt-work-in-org-mode-for-mhchem-snippets)
halewyn · 2022-11-26 22:20
Works for me under 28.2. I ran `emacs -Q` and set just one option: `(setq org-latex-packages-alist '(("version=4" "mhchem" t)))`. Did you wrap `\ce{Na2CO4}` in `\( ... \)`?
sewageseller · 2022-11-29 12:55
u/halewyn, thank you for your answer! Indeed, wrapping with `\(…\)` solves the issue! I thought that was for inline math, as `\alpha{}` is correctly displayed without wrapping. Would you know why this is the behavior?
halewyn · 2022-11-29 14:44
You need to wrap `\ce{Na2CO4}` in `\(...\)` to make Org recognize it as an embedded LaTeX fragment. A LaTeX fragment is recognized by Org if it is wrapped in one of `\(...\)`, `\[...\]`, `$...$`, `$$...$$`, or `\begin{...}...\end{...}`. The `\alpha{}` syntax, although it uses LaTeX-like notation, is not processed as a LaTeX fragment. Instead, Org directly converts it to the corresponding UTF-8 character (see https://orgmode.org/manual/Special-Symbols.html).
sewageseller · 2022-11-29 15:32
That’s very clear, thank you! And have a lovely day.