2

I am taking mathematical notes using org mode and would like to have inline previews for commutative diagrams. For mathematical equations this works perfectly with dvipng. However, dvipng doesn't play well with tikz-cd based commutative diagrams therefore I changed to using imagemagick by setting org-preview-latex-default-process. However, when using imagemagick my latex previews (equations, diagrams etc) come out huge -- the text itself is appropriately sized, but the "frame" of the image is huge. See the below for examples:

With Imagemagick

enter image description here

With dvipng

enter image description here

As you can see, in the first example the image is so large the lorem ipsum paragraph isn't even in view.

How can I fix this? I would really like to use imagemagick for commutative diagrams, but having such huge images is unwieldy.

1 Answer 1

1


Hi!
I had the exact same problem yesterday and I think I've found a (somewhat hacky) solution today.

Explanation

The command that gets executed depending on what you set for org-preview-latex-default-process (in our case 'imagemagick) can be seen in the variable org-preview-latex-process-alist under :image-converter. Its default value is the following:

((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")
         :transparent-image-converter
         ("dvipng -D %D -T tight -bg Transparent -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 --no-fonts --exact-bbox --scale=%S --output=%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")))

Here we see that preview runs convert -density %D -trim -antialias %f -quality 100 %O". Further down in the Documentation there is an explanation of the place-holders used:

Place-holders used by :image-converter and :latex-compiler:

  %f    input file name
  %b    base name of input file
  %o    base directory of input file
  %O    absolute output file name

Place-holders only used by :image-converter:

  %D    dpi, which is used to adjust image size by some processing commands.
  %S    the image size scale ratio, which is used to adjust image size by some
        processing commands.

I don't know much about ImageMagick but what I think is happening is %D (intended by emacs as a placeholder for DPI) is already in use by Imagemagick (as 'image GIF dispose method', source).

Hacky Solution

I simply set the DPI in the above :image-converter command of imagemagick explicitly. i.e. the -density flag to something like 150:

...

(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 150 -trim -antialias %f -quality 100 %O")))

Addendum

Remember that preview caches the generated images by default (I think). To test if this works remember to either (1) use a new latex fragment to preview or (2) delete the image cache (cache location is defined by org-preview-latex-image-directory)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.