15

I'm using org-mode in emacs, but I think the question is also relevant for auctex users.

In org-mode you can show latex fragments, as the manual demonstrates.

But: these fragments (mostly math formulae) seem to have constant size. When I type C-x C-+ or C-x C-- only the buffer text size changes. How can I change the fragments size from the buffer?

(I know I can make the fragments larger or smaller with org-format-latex-options, but this is permanent. I want to change the size of the fragments (the png files) like the text.)

5 Answers 5

5

Updated answer: (thanks for the feedback)

(defun update-org-latex-fragments ()
  (org-latex-preview '(64))
  (plist-put org-format-latex-options :scale text-scale-mode-amount)
  (org-latex-preview '(16)))
(add-hook 'text-scale-mode-hook 'update-org-latex-fragments)

Old answer:

Here is a start: hook into text-scale-mode-hook and update org-format-latex-options accordingly.

(defun update-org-latex-fragments ()
  (org-toggle-latex-fragment '(16))
  (plist-put org-format-latex-options :scale text-scale-mode-amount)
  (org-toggle-latex-fragment '(16)))
(add-hook 'text-scale-mode-hook 'update-org-latex-fragments)

A scaling factor might be needed before text-scale-mode-amount though...

6
  • No org-toggle-latex-fragment function in latest org-mode.
    – Leu_Grady
    Jan 30, 2015 at 1:46
  • @user3173715 Your latest is not my latest then...
    – thisirs
    Feb 1, 2015 at 0:37
  • you are using development org-mode?
    – Leu_Grady
    Feb 1, 2015 at 0:38
  • @user3173715 Use org-preview-latex-fragment (obsolete since Aug 2014)
    – thisirs
    Feb 1, 2015 at 10:46
  • org-toggle-latex-fragment is obsolete since org 9.3. Use org-latex-preview instead. Moreover, at least at my side, the first org-toggle-latex-fragment should take '(64) instead. '(64) clear images in the buffer, and '(16) display them (cf. the doc). Feb 2, 2020 at 1:15
3

Based on the answer from thisirs, I am using the following with success. The scale factor in this example is 2.3:

(defun update-org-latex-fragment-scale ()
  (let ((text-scale-factor (expt text-scale-mode-step text-scale-mode-amount)))
    (plist-put org-format-latex-options :scale (* 2.3 text-scale-factor)))
)
(add-hook 'text-scale-mode-hook 'update-org-latex-fragment-scale)

Note that I have removed the code that toggles the preview, since I prefer to do that manually, and it can take a while if there are many fragments to preview. This code only adjusts the scale of previewed fragments, so that the next time they are displayed using C-c C-x C-l, they will have an appropriate size that matches the text.

3
  • Is hardcoding the factor necessary?
    – T. Verron
    Jun 10, 2015 at 8:52
  • @T.Verron the scale factor is something that affects how large or small the previewed fragment is with respect to the text size. The ratio between those two probably depends on the font you're using, and possibly the dpi of your screen. It may be possible to automate the computation of a scale-factor, but I prefer to just fiddle with it until it looks to my liking. Note that the "hard-coded scale factor" is not the same as the text-scale-factor, which does automatically change as you zoom your text.
    – Mark
    Jun 11, 2015 at 15:54
  • Oh, ok, indeed I thought that it would be the zoom factor. Sorry!
    – T. Verron
    Jun 11, 2015 at 15:56
0

That is already built-in if you use texfrag-mode in Orgmode instead of the built-in LaTeX fragment support of Orgmode.

Just regenerate the LaTeX fragments with C-c C-p C-d after changing the size with C-x C-+ C-+ .... The size of the LaTeX fragments will fit the size of the surrounding text.

Normal text size:

Normal text size

After pressing C-x C-+ C-+ C-x and C-c C-p C-d:

enter image description here

Sidenote: With the last version of texfrag you can also activate texfrag-show-last-mode. With that minor mode activated Texfrag shows the last rendered LaTeX-fragment as handle if you edit the formula:

texfrag-show-last-mode

0

Code in other answers is helpful, but it re-renders latex preview on every change of text scale, even if not asked to do so, and even when C-x C-+ is pressed multiple times in a row, which is slow.

I'd rather do re-render manually. And I'd like org-format-latex-options to be left unmodified.

(setq my-org-latex-preview-scale 1.0)   ; depends on the font used in emacs or just on user preference
(defun org-latex-preview-advice (orig-func &rest args)
  (let ((old-val (copy-tree org-format-latex-options)))     ; plist-put is maybe-destructive, weird. So, we have to restore old value ourselves
    (setq org-format-latex-options (plist-put org-format-latex-options
                                              :scale
                                              (* my-org-latex-preview-scale (expt text-scale-mode-step text-scale-mode-amount))))
    (apply orig-func args)
    (setq org-format-latex-options old-val)))
(advice-add 'org-latex-preview :around #'org-latex-preview-advice)
0

A different, modern solution is to preview latex fragments in Org mode using MathJax instead of LaTeX. The package math-preview implements this and with it the font size in the previews is adapted to the font size of the buffer.

Your Answer

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

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