Adam
Adam

Reputation: 2253

Org LaTeX preview is fuzzy on retina displays

I have been using Emacs 24.4 for all my math/scientific notes. org-latex-preview is fantastic for this! But recently, I upgraded to a macbook pro with retina display, and I now see that all my equations in org-mode are... fuzzy. Is there a setting I can change to up-res these?

Here is a screenshot:

enter image description here

Thanks!

Upvotes: 18

Views: 5291

Answers (5)

Adam
Adam

Reputation: 2253

A couple of years back, I decided to fix this and wrote a patch to add dvisvgm as a render option for latex previews. While this worked great, I never submitted it (no time or knowledge on how to officially patch org).

Today, I was thrilled to discover that org-mode v9.0.6, now has this feature!

To activate, first check that you have dvisvgm on your system. Then update org-mode and add the following line to your init.el file:

(setq org-preview-latex-default-process 'dvisvgm)

And presto!

enter image description here

Upvotes: 27

Wei Leng
Wei Leng

Reputation: 1

This may not be the answer to the question, but I want to share the experience when using preview-latex in auctex mode.

The blurry image is mainly caused by bitmap image, so it's necessary to use dvisvgm, thus the package preview-dvisvgm is needed, and the environment var LIBGS needed by dvisvgm should be set correctly.

There is still one issue concerns dvisvgm with ghostscript. In OSX 12.6.1 with M1 chip, the ghostscript v10.00.0 has some pdf interpret problem, causing dvisvgm can not generate svg from pdf files. One work around is

  1. Let preview latex generate xdv file instead of pdf files, by changing preview-LaTeX-command to "%`xelatex --no-pdf ..."
  2. Let preview-dvisvgm use the region.svg file from the first step, by changing preview-dvisvgm-pdf-command to "dvisvgm --no-fonts _region_.xdv ..."

Example

Upvotes: 0

Erki der Loony
Erki der Loony

Reputation: 701

I found a solution that works a little more generally for all inline images. First make sure any generated images are created with a scale factor of 2. For example for LaTeX code blocks and inline LaTeX snippets this works by

(plist-put org-format-latex-options :scale 2)

Then you make org scale all inlined images back. For LaTeX code blocks we can advise org--create-inline-image like so:

(defun my/image-scale-advice (image)
  (let* ((factor (image-property image :scale))
         (new-factor (if factor
                         (/ factor 2.0)
                       0.5)))
    (image--set-property image :scale new-factor)
    image))
(advice-add 'org--create-inline-image :filter-return #'my/image-scale-advice)

This divides any already existing scaling-factor by 2 or sets a scaling factor of 0.5 if none is present.

For inline LaTeX snippets we can advise org--make-preview-overlay like so:

(defun my/overlay-scale-advice (beg end image &optional imagetype)
  (mapc (lambda (ov) (if (equal (overlay-get ov 'org-overlay-type) 'org-latex-overlay)
                                (overlay-put ov
                                             'display
                                             (list 'image :type (or (intern imagetype) 'png) :file image :ascent 'center :scale 0.5))))
        (overlays-at beg)))
(advice-add 'org--make-preview-overlay :after #'my/overlay-scale-advice)

This should result in much crispier inline images on Retina displays.

Upvotes: 2

galaxy
galaxy

Reputation: 46

By default orgmode latex preview do not support retina, so on mac with retina screen, latex preview will be fuzzy.
However, we can hack org.el to achieve the function. Just follow steps below:

get the proper version of emacs

To instead use the Yamamoto Mitsuharu version of Emacs 25.1 (with more mac-specific features):

brew tap railwaycat/emacsmacport
brew install emacs-mac

and finally link it to your Applications folder:

brew linkapps emacs-mac

this version emacs will support retina image display.

change org-format-latex-options

change scale from 1.0 to 2.0, to generate 2x size image.

delete org.elc

add function to org.el

(defun galaxy-compose-image-filename-2x(image-file-name)
  (concat (file-name-directory image-file-name) (file-name-base image-file-name) "@2x." (file-name-extension image-file-name)))

and eval the function.

modify function org-format-latex

change fragment:

(unless (file-exists-p movefile)
  (org-create-formula-image value movefile options forbuffer processing-type)

to

(unless (file-exists-p movefile)
  (org-create-formula-image value movefile options forbuffer processing-type)
  (setq filename-2x (galaxy-compose-image-filename-2x movefile))
  (rename-file movefile filename-2x)
  (call-process-shell-command "convert" nil nil nil nil (concat "\"" filename-2x "\" -scale \"50%\" -quality \"100%\"" ) (concat "\"" movefile "\"" )))

and eval the function.

Now, you can preview latex with 2x size image for mac retina screen.

Upvotes: 1

algking
algking

Reputation: 387

I have tried the emacs-mac-port

If I create 2 files foo.png [email protected] on the same dir ,this will work.

Upvotes: 0

Related Questions