J.y B.y
J.y B.y

Reputation: 605

How to configure Emacs to use variable fonts to display the previsualisation of LaTeX formulas?

I would like to use Emacs 24.2.1 in orgmode 7.9.3 on a data projector in class.

The problem is that the math fonts are not increasing along the text fonts.

Is there a way to configure Emacs so that the math fonts are variable too? Was this solved in a later version of orgmode (I was planning to wait for the end of the term to upgrade to version 8+, being afraid of having to invest a lot of time in updating my configuration).

Upvotes: 2

Views: 1328

Answers (1)

Tobias
Tobias

Reputation: 5198

A poor man's solution that works with advices and hooks is shown below. Paste the code into your .emacs configuration file and re-start emacs. Afterwards, the equation images should be scaled at along with the text if you press C-+/-/0. They are just previews and their The bad thing is that the resolution is not enlarged. To get equation images with a good resolution hit C-c C-x C-l to regenerate the images. Note, that this sets the :scale property of the customization variable org-format-latex-options. The customization of this variable will be lost for the current emacs session.

You need a gnu-emacs with built-in imagemagick. Should be standard by now.

(defvar text-scale-mode-hook nil
"Hook run at end of command `text-scale-mode'.")

(defadvice text-scale-mode (after text-scale-mode-hooks nil activate)
  "Run `text-scale-mode-hook' at end of command `text-scale-mode'."
  (if (functionp text-scale-mode-hook)
      (funcall text-scale-mode-hook)
    (loop for hook in text-scale-mode-hook do
      (if (eq hook 't)
          (run-hooks (default-value text-scale-mode-hook))
        (run-hooks hook)))))

(defun org-text-scale-eye ()
  "Scale equation images according to text-scale-mode-amount."
  (when (boundp 'text-scale-mode-amount)
    (let ((relwidth (* (expt text-scale-mode-step text-scale-mode-amount))))
    (loop for ol in (overlays-in (point-min) (point-max)) do
      (when (eq (overlay-get ol 'org-overlay-type) 'org-latex-overlay)
        (unless (overlay-get ol 'org-image-original-width)
          (overlay-put ol 'org-image-original-width (car (image-size (overlay-get ol 'display) t))))
        (let ((ol-disp-plist (cdr (overlay-get ol 'display))))
          (setq ol-disp-plist (plist-put ol-disp-plist :type 'imagemagick))
          (setq ol-disp-plist (plist-put ol-disp-plist :width (round (* relwidth (overlay-get ol 'org-image-original-width)))))
          (overlay-put ol 'display (append '(image) ol-disp-plist))
          ))))
    (force-window-update)
    ))

(add-hook 'org-mode-hook '(lambda () (add-hook 'text-scale-mode-hook 'org-text-scale-eye)))

(defadvice org-format-latex (before set-scale activate)
  "Set :scale in `org-format-latex-options' to the scaling factor resulting from `text-scale-mode' and clear cache."
  (let ((relwidth (expt text-scale-mode-step text-scale-mode-amount)))
    (unless (= (plist-get org-format-latex-options :scale) relwidth)
      (plist-put org-format-latex-options :scale relwidth))))

Actually, I had a look at the code in org.el: The font height is regarded in the function org-create-formula-image-with-dvipng and the resolution option -D is set. Maybe, there goes something wrong. Furthermore, pityingly, in org-format-latex the images are cached and not reproduced after font size changes.

If you want to increase the size of the latex-formulas for one session in general with good quality you can set the :scale value of the customization option "Org Format Latex Options" in the group "org-latex" to a higher value.

Edit 2013-10-10 (marked as italic throughout the answer except the code, naturally): The :scale property of the option org-format-latex-options is now set automagically. Therefore, the above hack should be quite usable (such as the latex version mentioned below).


The same problem as you describe in the original-post exists for latex-preview. But, for that case there is a better solution:

(require 'face-remap)
(defadvice preview-inherited-face-attribute (after preview-inherit-local-face nil activate)
  "Scale preview images with respect to buffer-local face"
  (when (and text-scale-mode (eq attribute :height))
    (setq ad-return-value (* (expt text-scale-mode-step text-scale-mode-amount) ad-return-value))))

After C-+/-/0 type C-c C-p C-d to re-generate all formulas of the document. Afterwards, the formulas have just the right size and the right resolution.

If switching to latex documents with preview is an option for you I would recommend it. I use org only for notes and time-tables with easy links.

For more descriptive documents I use latex documents with auctex/preview. But, I actually compile them only very seldom to a final pdf document. The preview is good enough. See the following Figure. enter image description here

Upvotes: 4

Related Questions