reddit / emacs

Help using lualatex to preview latex in org-mode

chuckie219 · 2022-05-06 10:12
Hi there. I have recently re-installed emacs to use org-mode again, and I am trying to get nice previews or latex equations and fragments in org-mode (on macOS using emacs-mac). The default `dvisvgm` works nicely, however it seems lualatex cannot easily be used to create dvi images, as such I have to try something else. I wish to use lualatex for nice fonts and for drawing diagrams (some of the packages I wish to sue require the use of lualatex to compile). My current process is to compile to pdf using the following preamble ```latex \documentclass[preview, border=1pt]{standalone} \usepackage{usenames}[color] [PACKAGES] [DEFAULT-PACKAGES] \pagestyle{empty} ``` In above, `[DEFAULT-PACKAGES]` is defined based on `org-latex-default-packages-alist` and `[PACKAGES]` is defined based on `org-latex-packages-alist` which right now only contains the `physics` package and the `firamath-otf` package. I then use the utility `pdf2svg` to convert the resulting pdf into a svg. Everything works beautifully, except the svg images created from inline latex fragments have additional whitespace to the right of them, as if they were display equations (although the styling is inline and the are not centre justified). This does not seem to be the case if I create a tex file with the same equation, compile it with the same preamble and then convert to an svg, so I am struggling to figure out what might be changing within emacs to include the additional whitespace in the svg. I struggling to read elisp, and nothing seems obvious to me. Perhaps someone could give me some insight, or at least give me some help with debugging? How could I say, alter the functions that create the preview such that generated latex file is also saved somewhere? That would help me a lot in figuring out what is going on. Thanks in advance. P.S. this is the exact process I am using: ```elisp (setq luasvg '(luasvg :programs ("lualatex" "pdf2svg") :description "pdf > svg" :message "you need to install lualatex and imagemagick." :use-xcolor t :image-input-type "pdf" :image-output-type "svg" :image-size-adjust (1.0 . 1.0) :latex-compiler ("lualatex -shell-escape -interaction nonstopmode -output-directory %o %f") :image-converter ("pdf2svg %f %O"))) (setq org-preview-latex-default-process 'luasvg) ```
usaoc · 2022-05-06 11:34
It could’ve been solved by reading [the manual](https://manpages.debian.org/testing/texlive-latex-base/dvilualatex.1.en.html). Noticed `dvilualatex`?
chuckie219 · 2022-05-06 11:34
Found the culprit: The tex file generated by org-mode would look like ```latex \begin{document} ... \pagecolor{bg} \color{fg} { <latex-frag> }% \end{document} ``` Note the blank line after the latex fragment, this was causing the pdf to be output as an entire line. To fix this I just change ```elisp (with-temp-file texfile (insert latex-header) (insert "\n\\begin{document}\n" "\\definecolor{fg}{rgb}{" fg "}%\n" (if bg (concat "\\definecolor{bg}{rgb}{" bg "}%\n" "\n\\pagecolor{bg}%\n") "") "\n{\\color{fg}\n" string "\n}\n" "\n\\end{document}\n")) ``` to not have the `\n` preceding the `\\end{document}` part of the string on line the last line (this is in the` org-create-formula-image` function).
chuckie219 · 2022-05-06 11:41
Well it seems DVI doesn't handle fancy fonts particularly well, this is why I avoided it. I could be wrong in that aspect though. Anyway, I fixed the problem---see my comment.
usaoc · 2022-05-06 11:44
*Standard* DVI doesn’t, but `dvilualatex` doesn’t generate standard DVI either. `dvisvgm` supports what `dvilualatex` generates perfectly fine, which is one of its selling points.