18

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!

Adam
  • 2,137
  • 16
  • 29
  • Preview is done with png images and Ghostscript, which is notorious for poor display quality. For truly good display quality, you will need to use a separate pdf program that opens in an entirely different frame. There is at least one thread where a user made efforts to find the most legible font for his / her own needs (using preview), but that will ultimately boil down to a matter of taste. – lawlist May 10 '15 at 14:54
  • The pngs looked fine on my old mac (pre retina display). I'm can only guess that the issue has something to do with the high resolution and automatic scaling of the retina display...? – Adam May 11 '15 at 15:40
  • If you do a few Google word searches for **ghostscript png emacs pdf** and the like, you will find a few threads with inquiries regarding how to get better image quality of pdf inside Emacs. The poor quality is simply more noticeable with a beautiful display. I have my setup with an external viewer (Skim) for tex generated documents; and, to view pdf documents from an Emacs file manager (e.g., Dired or a custom tree view manager that I use), I have programmed an echo area set of choices to open the pdf: doc-view (the in-Emacs option), Preview, Adobe, Skim, Omnipage. – lawlist May 11 '15 at 16:16
  • I'm pretty sure this is related to the retina technology and compatibility/settings for how these images are rendered... I have other applications that are not "retina ready" like QGIS and MS Office 2011, and they have the same issue. All the images are rendered at like 1/4 resolution, so that they maintain the correct size but with a lower resolution image. I also notice this issue with screenshots that I took on my old mac. But there must be some way to specify the correct resolution on the settings...? – Adam May 12 '15 at 09:47
  • 2
    Here is a link to some interesting commentary regarding doc-view and retina support, with reference to a particular Emacs build that may support better image quality. https://github.com/politza/pdf-tools/issues/51 Here is the relevant snippet: **If you are using macports, you can port install emacs-mac-app which installs a version with retina support. Now to view a pdf in retina mode you should name the file "foo@2x.pdf". I picked this up from** https://github.com/railwaycat/emacs-mac-port/blob/7be6d9e8dbd122b060b058c0a345091debc52975/doc/emacs/macport.texi#L401 – lawlist May 12 '15 at 17:07

5 Answers5

25

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-latex-create-formula-image-program 'dvisvgm)

And presto!

enter image description here

Adam
  • 2,137
  • 16
  • 29
  • 2
    Thanks for your answer! I did what you suggested, but the generated svg are not being displayed by emacs. The images are generated (as they appear in the folder `ltximg`), but the latex equations `$...$` are not substituted by the images. Any ideas? – Guilherme Salomé Jul 06 '17 at 23:10
  • Try adding this to your init.el file: https://www.dropbox.com/s/tcmva6outuwmq7f/latex.el?dl=0 – Adam Jul 26 '17 at 10:18
  • Are you using emacs-mac-port or GNU Emacs? I can't achieve that density in GNU Emacs :( `dvisvgm` is far better than `dvipng`, but still ugly :( – d12frosted Apr 22 '18 at 18:45
  • I'm using the railwaycat version, which has built-in support for svgs. Looks absolutely razer sharp on my screen. Here is the download link to the mac binaries: https://s3.amazonaws.com/emacs-mac-port/emacs-26.1-rc1-mac-6.90.zip – Adam Apr 23 '18 at 00:04
  • @Adam I see. Thanks. – d12frosted Apr 25 '18 at 19:02
  • @Adam Is there a way to do this for preview-latex in auctex, outside of org-mode? – 5fec Oct 20 '19 at 17:57
  • 1
    Note that as of Emacs 26.1: "This variable is obsolete since Org 9.0; use ‘org-preview-latex-default-process’ instead." – nivekuil Nov 28 '19 at 18:40
  • 1
    That init file is no longer available in Dropbox. Can you add the relevant bits to your answer? – HappyFace Sep 28 '20 at 11:15
  • `(setq org-startup-with-latex-preview t)` solved my problem. – HappyFace Sep 28 '20 at 12:12
  • 1
    @HappyFace Glad that worked! If you need any additional help, let me know and I will find a way to repost my init file in a more permanent fashion! – Adam Sep 28 '20 at 17:22
  • @5fec Same question. I found this variable that might be relavent: `preview-LaTeX-command`, which is by default ("%`%l \"\\nonstopmode\\nofiles\\PassOptionsToPackage{" ("," . preview-required-option-list) "}{preview}\\AtBeginDocument{\\ifx\\ifPreview\\undefined" preview-default-preamble "\\fi}\"%' \"\\detokenize{\" %t \"}\""). Any pro emacs programmer can get any insight on how to make this work – Jordan He Feb 21 '21 at 01:14
  • @JordanHe I ended up writing a preview-latex replacement... maybe of interest? https://github.com/dandavison/xenops – 5fec Feb 21 '21 at 21:17
  • @5fec That's absolutely amazing. I'll look into it. – Jordan He Feb 21 '21 at 21:23
  • @Adam what do you mean by "update org-mode"? I'm running emacs-doom, so I assumed running "doom sync" maybe might fix the problem, but unfortunately even with dvisvgm the latex fragments are still fuzzy afterwards. – pfincent Jan 09 '22 at 14:12
  • @pfincent do you have this somewhere in your init.el file? `(setq org-preview-latex-default-process 'dvisvgm)` – Adam Jan 09 '22 at 22:29
  • @Adam Thank you for your answer, I did in fact not have it included in the init.el! However, now that I added it, the latex fragments are not being compiled at all anymore. Do you still remember what you had posted in the linked dropbox file regarding Guilherme Salomé's comment on the same issue? Unfortunately the file is not there anymore. – pfincent Jan 10 '22 at 01:54
  • @pfincent If your `org-mode` predates 9.0, the command is different. You'll want to use`(setq org-latex-create-formula-image-program 'dvisvgm)` instead. You can check this from Emacs with `M-x` `org-version`. But I suspect you may have an issue with your LaTeX installation. If you are on a Mac or linux, open a Terminal and type`dvisvgm --version`. If this is not coming up, you need to install it (or it could be that you have an issue with your PATH variable). On the Mac, this is bundled with MacTeX and can be installed with Homebrew `brew install --cask mactex`. What OS are you running? – Adam Jan 10 '22 at 09:52
  • My org-version is 9.6, and dvisvgm is installed (version 2.22.1). The latex images are being created and saved as svg to the ltximg/ directory, it's just the preview in org-mode that does not work. I suspect I'm having the exact same problem as @Guilherme Salomé had, no idea how to solve it though... – pfincent Jan 10 '22 at 11:25
  • @pfincent @GuilhermeSalomé it's possible that your flavor of Emacs does not support SVGs. I'm on a Mac and can confirm that the latest version of Mitsuharu's Emacs works. I would recommend installing the Homebrew package from [Railwaycat](https://github.com/railwaycat/homebrew-emacsmacport). Otherwise, I would try starting with a blank `init.el` to see if you can isolate an issue with your config. – Adam Jan 11 '22 at 10:54
  • If you confirm that the issue is with your `init.el`, debugging is fairly straight-forward. Take your `init.el` and back it up to `init-old.el`. Then delete the bottom 1/2 of your config and restart. If it works, you know the error is in the bottom 1/2. Otherwise, delete 1/2 of whatever is left over and repeat. Continue until you find the offending line of code. Good luck! – Adam Jan 11 '22 at 11:03
  • Thanks for the answer, it works well in generating high-resolution equation previews. However, the response of the org file became super slow after I switched from `dvipng` to `dvisvgm`. Does any of you suffer a similar problem? – Song Wang Dec 06 '22 at 16:38
2

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.

Erki der Loony
  • 621
  • 5
  • 7
  • For me, this results in an "Error: void-variable org-format-latex-options" – pfincent Jan 10 '22 at 11:32
  • This works perfectly for me on macOS, and avoids the need for installing `dvisvgm` (which you need build from source on macOS). – gsgx Feb 15 '23 at 23:54
1

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.

galaxy
  • 26
  • 1
0

I have tried the emacs-mac-port

If I create 2 files foo.png foo@2x.png on the same dir ,this will work.

algking
  • 367
  • 3
  • 7
0

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

Wei Leng
  • 1
  • 1