# Org-mode LaTeX previews image size too large

SOLVED: REMOVE #+SETUPFILE: HEADER. SORRY FOR WASTING YOUR TIME

I am having the same issue as described in this Reddit post from a few weeks ago: basically the preview images generated for a given latex fragment in the file are too large vertically and make the file unreadable.

I started out using dvipng (the default) to generate the images. I've played around with adding

(setq org-format-latex-options (plist-put org-format-latex-options :scale 2.0))


to my config: this made the rendered latex larger (which was helpful) but unfortunately did not cut down the size of the images (which in fact got larger).

I've also tried switching form dvipng to dvisvgm, the latter just gave the error

Latex-preview not working; Please adjust ‘dvisvgm’ part of ‘org-preview-latex-process-alist’


like in this post.

Finally, I have also tried the texfrag package, which kind of works, but only after setting the scale and generating previews, getting an error, and then generating previews again. It doesn't work with #+STARTUP: latexpreview.

EDIT:

As requested, here is a brief snippet of an org-roam file that produces unwieldy previews:

#+title: Kullback-Leibler divergence
#+roam_tags: "machine learning" "statistics" "probability"
* Kullback-Leibler Divergence
For two continuous probability distributions of one dimension $P$ and $Q$, the Kullback-Leibler (KL) divergence, also known as the relative entropy, gives a measure of the difference between them. The definition is as follows:

\begin{eqnarray}
\label{eq:KL}
D_Q(P) = \int_{\infty}^{\infty} dx ~ P(x) \log \left(frac{P(x)}{Q(x)} \right) \,,
\end{eqnarray}

from which we can see that the KL divergence is not symmetric - $D_Q(P)$ is a measure of the divergence of $P$ from $Q$, while $D_P(Q)$ measures the divergence of $Q$ from $P$, and these are in general different.


(I don't think org-roam has anything to do with this, just a handy example)

EDIT2: Emacs 27.2 and Org mode version 9.4.4

GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.27, cairo version 1.17.4) of 2021-03-26

Org mode version 9.4.4 (release_9.4.4 @ /usr/share/emacs/27.2/lisp/org/)

EDIT3:

Here is the current setup of org-preview-latex-process-alist:

Value:
((dvipng :programs
("latex" "dvipng")
:description "dvi > png" :message "you need to install the programs: latex and dvipng." :image-input-type "dvi" :image-output-type "png" :image-size-adjust
(1.0 . 1.0)
:latex-compiler
("latex -interaction nonstopmode -output-directory %o %f")
:image-converter
("dvipng -D %D -T tight -o %O %f"))
(dvisvgm :programs
("latex" "dvisvgm")
:description "dvi > svg" :message "you need to install the programs: latex and dvisvgm." :image-input-type "dvi" :image-output-type "svg" :image-size-adjust
(1.7 . 1.5)
:latex-compiler
("latex -interaction nonstopmode -output-directory %o %f")
:image-converter
("dvisvgm %f -n -b min -c %S -o %O"))
(imagemagick :programs
("latex" "convert")
:description "pdf > png" :message "you need to install the programs: latex and imagemagick." :image-input-type "pdf" :image-output-type "png" :image-size-adjust
(1.0 . 1.0)
:latex-compiler
("pdflatex -interaction nonstopmode -output-directory %o %f")
:image-converter
("convert -density %D -trim -antialias %f -quality 100 %O")))

• Can you add to your question an Org mode file with some LaTeX that produces such large images? WIthout that, I'm afraid there is not enough information to help you. Jul 23 at 15:33
• @NickD Done, but the issue is pretty generic. Basically put up the most minimal org headers, then in the body type in $x=1$ or some longer $$...$$ block, and the result persists. See also the example given in the linked Reddit post. Jul 23 at 16:06
• I cannot reproduce it (either with dvipng or imagemagick): the previews are reasonably small. Please try with emacs -q (i.e. without your init file) and see if you can reproduce it. If not, then some setting in your init file is causing this. Also post the version of emacs and Org mode you are using (M-x emacs-version and M-x org-version resp). Jul 23 at 17:07
• @NickD emacs -q still gives the previews that are oversized (again, the text is correctly sized but the image dimensions are too large) Jul 23 at 17:23
• @NickD GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.27, cairo version 1.17.4) of 2021-03-26 Org mode version 9.4.4 (release_9.4.4 @ /usr/share/emacs/27.2/lisp/org/) Jul 23 at 17:23

1. Test 1
2. Test 2
3. Resolution

The issue described here is a partial resolution of this post I raised yesterday

The problem is that LaTeX previews generated from org files I was creating were too large, took very long to process and made the text unreadable due to their size.

After some help from @NickD, I realized that the problem was that I was using a setupfile for LaTeX export for some of the other files in the same folder! But it took a long time to figure out why this was a problem.

I created two files, test1.org and test2.org. Initially, both contain the same pieces of LaTeX terms, and I placed both in a separate folder. test1.org was a standard .org file, while test2.org included the header for the setup file.

# Test 1

Here is what test1.org looked like in raw form:

and here is what it looks like after applying previews with C-c C-x C-l:

The previews are actually good! They are appropriately scaled in font, and the size of the preview images means they fit inside sentences.

# Test 2

Here is test2.org, where we include the setup file header #+SETUPFILE: ~/org/latex/latex<sub>template.org</sub>:

Now we apply the previews:

This looks exactly the same as test1.org! I am not sure, but I believe that org-latex-preview must be coded to reuse already generated previews – in this case, I think the previews from test1.org, which were previously created, are added to this file.

I added some new LaTeX fragments to test2.org:

Now, applying the previews, I find that the previous paragraph reuses the same previews as before and appears perfectly fine. But the newly added LaTeX now has previews which are as bad as the initial issue I was having: oversized images which disrupt sentences:

# Resolution

So I guess what I have to do is to delete all the generated latex previews in my main org-roam folder, and remove #+SETUPFILE: ~/org/latex/latex<sub>template.org</sub>.

I think the main problem with the size arises from the template requiring \usepackage{fullpate} - after removing this the size seems OK, but I haven't done a full check. The other problem of it taking way too long to generate previews still remains.

This is slightly unsatisfactory, as I would like to have the option of both having reasonably sized LaTeX previews, as well as be able to export my notes to a .pdf via LaTeX, in the format that I specified. I also have some nice \newcommand redefinitions that I am used to using.

• That's why I asked you to post a file that shows the problem - but the file you posted did not contain anything about #+SETUPFILE. How do you expect people to help you when you don't give them all the necessary information? Jul 24 at 17:36
• You can include different LaTeX packages when exporting than when previewing (although the set used for preview is always a subset of the set used for export): use #+LATEX_HEADER for packages that you want to include for both previewing and export; use #LATEX_HEADER_EXTRA for packages that you want to include for export only. Jul 24 at 17:41
• @NickD Sorry, but that file was giving me a problem! I didn't realize it was connected to the #+SETUPFILE issue until I created a new org file in a new folder. I didn't realize that if you create an preview image from one file, another file could use the same preview image if it has the same symbol! Jul 24 at 17:42
• But you elided the #+SETUPFILE from what you posted: what you posted was NOT what you had problems with. Don't do that. Jul 24 at 17:46
• @NickD It was! This is the key point: What I should have done better I think was delete all the preview images from the folder as you said. I have another file with the same formula in that I had previously generated formulae for (on Autoencoders). This got reused for the preview in the file I posted. Jul 24 at 17:51