emacs - 如何配置 Emacs 使用可变字体来显示 LaTeX 公式的预览?

标签 emacs fonts latex org-mode

我想在类里面的数据投影仪上使用 orgmode 7.9.3 中的 Emacs 24.2.1。

  • 我可以使用 org-preview-latex-fragment (C-c C-x C-l) 预先可视化 latex 公式,并且
  • 我可以使用 text-scale-increase (C-+) 增加用于显示文本的字体。

问题是数学字体没有沿着文本字体增加。

有没有办法配置 Emacs 以便数学字体也可变? 这个问题在更高版本的 orgmode 中解决了吗(我本来打算等期末升级到 8+ 版本,怕要花很多时间更新配置)。

最佳答案

下面显示了一个与建议和 Hook 配合使用的穷人解决方案。 将代码粘贴到 .emacs 配置文件中并重新启动 emacs。之后,如果按 C-+/-/0,方程图像应与文本一起缩放。 它们只是预览, 坏处是分辨率没有放大。要获得具有良好分辨率的方程图像,请按 C-c C-x C-l 重新生成图像。 请注意,这设置了自定义变量 org-format-latex-options 的 :scale 属性。当前 emacs session 将丢失此变量的自定义。

您需要一个内置 imagemagick 的 gnu-emacs。现在应该是标准的。

(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))))

实际上,我查看了org.el中的代码: 在函数 org-create-formula-image-with-dvipng 中考虑字体高度,并设置分辨率选项 -D。也许,出了什么问题。此外,遗憾的是,在 org-format-latex 中,图像会被缓存,并且在字体大小更改后不会重新生成。

如果您想以良好的质量增加一个 session 的 latex 公式的大小,您可以将“org-latex”组中自定义选项“Org Format Latex Options”的 :scale 值设置为更高的值(value)。

编辑 2013-10-10(除了代码之外,整个答案都标记为斜体):选项 org-format-latex-options 的 :scale 属性现在自动设置。因此,上面的 hack 应该是相当好用的(比如下面提到的 Latex 版本)。


latex 预览存在与您在原始帖子中描述的相同问题。但是,对于这种情况有一个更好的解决方案:

(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))))

在 C-+/-/0 后输入 C-c C-p C-d 重新生成文档的所有公式。之后,公式就具有正确的大小和正确的分辨率。

如果您可以选择切换到带有预览功能的 latex 文档,我会推荐它。我仅将 org 用于带有简单链接的注释和时间表。

对于更多描述性文档,我使用带有 auctex/preview 的 latex 文档。但是,我实际上很少将它们编译成最终的 pdf 文档。预览已经够好了。参见下图。 enter image description here

关于emacs - 如何配置 Emacs 使用可变字体来显示 LaTeX 公式的预览?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19274832/

相关文章:

emacs - 需要输入时更改迷你缓冲区的背景颜色

Emacs - 奇怪的空白

compiler-construction - 是否有像 Markdown 这样更简单/更清晰的语法可以编译成 LaTeX?

latex - 如何在 LaTeX 中将一个符号放在另一个符号之上?

android - Xamarin.Forms:自定义字体和 FontAttributes

unicode - 在 Fantasyvrb 的 VerbatimOut 中使用 Unicode

haskell - ghc-mod 仅显示 *GHC info* 中的第一行

emacs - 离开字段的 yasnippet 条件转换

c++ - Windows API 选择粗体

fonts - 组织模式 TODO 字体大小