Org LaTeX preview is fuzzy on retina displays
Asked Answered
C

5

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!

Clothilde answered 10/5, 2015 at 12:22 Comment(5)
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.Falla
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...?Clothilde
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.Falla
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...?Clothilde
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. 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 "[email protected]". I picked this up from github.com/railwaycat/emacs-mac-port/blob/…Falla
C
26

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

Clothilde answered 12/5, 2017 at 17:32 Comment(21)
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?Oren
Try adding this to your init.el file: dropbox.com/s/tcmva6outuwmq7f/latex.el?dl=0Clothilde
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 :(Archfiend
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: s3.amazonaws.com/emacs-mac-port/emacs-26.1-rc1-mac-6.90.zipClothilde
@Clothilde I see. Thanks.Archfiend
@Clothilde Is there a way to do this for preview-latex in auctex, outside of org-mode?Cataphoresis
Note that as of Emacs 26.1: "This variable is obsolete since Org 9.0; use ‘org-preview-latex-default-process’ instead."Gossipy
That init file is no longer available in Dropbox. Can you add the relevant bits to your answer?Octastyle
(setq org-startup-with-latex-preview t) solved my problem.Octastyle
@Octastyle 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!Clothilde
@Cataphoresis 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 workBittencourt
@JordanHe I ended up writing a preview-latex replacement... maybe of interest? github.com/dandavison/xenopsCataphoresis
@Cataphoresis That's absolutely amazing. I'll look into it.Bittencourt
@Clothilde 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.Pyroxene
@Pyroxene do you have this somewhere in your init.el file? (setq org-preview-latex-default-process 'dvisvgm)Clothilde
@Clothilde 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.Pyroxene
@Pyroxene 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 typedvisvgm --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?Clothilde
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...Pyroxene
@Pyroxene @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. Otherwise, I would try starting with a blank init.el to see if you can isolate an issue with your config.Clothilde
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!Clothilde
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?Hyaloplasm
C
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.

Chelsea answered 27/1, 2021 at 15:49 Comment(3)
For me, this results in an "Error: void-variable org-format-latex-options"Pyroxene
This works perfectly for me on macOS, and avoids the need for installing dvisvgm (which you need build from source on macOS).Amoy
@Amoy As of writing, dvisvgm does exist on MacPorts and Homebrew, also if one wishes to do a lot of TeX related work MacTeX also has dvisvgm (and other tools) included.Snapback
M
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.

Mackay answered 18/4, 2017 at 1:30 Comment(0)
Q
0

I have tried the emacs-mac-port

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

Quiche answered 1/4, 2016 at 3:34 Comment(0)
Y
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

Yecies answered 3/4, 2023 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.