【问题标题】:Inline PDF images in org-modeorg 模式下的内联 PDF 图像
【发布时间】:2013-03-02 16:43:01
【问题描述】:

emacs+org-mode中,查看org-mode缓冲区时,可以内联 使用org-toggle-inline-images 命令链接图像。这 包括开箱即用的各种格式,但显然是 PDF 图像 尚未包含在内。

鉴于 emacs 完全能够渲染 PDF 文件,是吗? 可以像处理图像一样制作 org-mode 内联 PDF 文件 (png、jpeg 等)?

一些背景:PDF图像对我来说更方便一些 原因,最大的原因是它们可以很好地扩展并且可以很好地配合使用 乳胶,从小论文到大海报。

【问题讨论】:

  • 更正:Emacs 无法渲染 PDF 文件(我想你可以在 Elisp 中编写这样的东西,但它会做很多工作,因为它会很慢,结果非常令人失望) .它可以使用外部工具显示 PDF 文件,所以确实应该可以让 Org 内联显示这些链接的文档。
  • @Stefan 你是对的。外部工具进行实际渲染。我的意思是说它开箱即用地显示 PDF 文件(至少在 linux 上)。

标签: emacs org-mode


【解决方案1】:

我遇到了同样的问题,经过一番摸索后,通过将以下内容添加到我的 .emacs 文件中使其工作:

(add-to-list 'image-type-file-name-regexps '("\\.pdf\\'" . imagemagick))
(add-to-list 'image-file-name-extensions "pdf")
(setq imagemagick-types-inhibit (remove 'PDF imagemagick-types-inhibit))
(setq org-image-actual-width 600)

尚不确定,这些更改是否对其他模式有任何问题。 我在 org 模式下的 pdf 是由 python src 代码块创建的,并且上述工作正常,但是当我在 python 模式下更改某些内容时,图像不会更新,我需要手动运行 org-display-inline-images。

这假设您有一个支持 imagemagick 的 emacs,类似的东西也应该适用于使用 ghostscript(可能在 org.el 中进行更多编辑)。

【讨论】:

  • 此解决方案现在似乎不起作用.. pdf 被“显示”为没有内容的空白区域。
【解决方案2】:

让我完成这个问题。

首先,Org-mode 本身不支持任何 pdf 内联显示功能。但是,可以修改org-display-inline-images 以实现您想要的。首先你需要参考这个答案:Configuring emacs for showing fixed width inline images,这对我启发很大。然后我稍微修改了函数,使其支持在org-mode下显示pdf、bmp。我的功能在下面。

(setq image-file-name-extensions
   (quote
    ("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm" "xpm" "pbm" "pgm" "ppm" "pnm" "svg" "pdf" "bmp")))

(setq org-image-actual-width 600)

(setq org-imagemagick-display-command "convert -density 600 \"%s\" -thumbnail \"%sx%s>\" \"%s\"")
(defun org-display-inline-images (&optional include-linked refresh beg end)
  "Display inline images.
Normally only links without a description part are inlined, because this
is how it will work for export.  When INCLUDE-LINKED is set, also links
with a description part will be inlined.  This
can be nice for a quick
look at those images, but it does not reflect what exported files will look
like.
When REFRESH is set, refresh existing images between BEG and END.
This will create new image displays only if necessary.
BEG and END default to the buffer boundaries."
  (interactive "P")
  (unless refresh
    (org-remove-inline-images)
    (if (fboundp 'clear-image-cache) (clear-image-cache)))
  (save-excursion
    (save-restriction
      (widen)
      (setq beg (or beg (point-min)) end (or end (point-max)))
      (goto-char beg)
      (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
                        (substring (org-image-file-name-regexp) 0 -2)
                        "\\)\\]" (if include-linked "" "\\]")))
            old file ov img)
        (while (re-search-forward re end t)
          (setq old (get-char-property-and-overlay (match-beginning 1)
                                                   'org-image-overlay)
        file (expand-file-name
                      (concat (or (match-string 3) "") (match-string 4))))
          (when (file-exists-p file)
            (let ((file-thumb (format "%s%s_thumb.png" (file-name-directory file) (file-name-base file))))
              (if (file-exists-p file-thumb)
                  (let ((thumb-time (nth 5 (file-attributes file-thumb 'string)))
                        (file-time (nth 5 (file-attributes file 'string))))
                    (if (time-less-p thumb-time file-time)
            (shell-command (format org-imagemagick-display-command
                           file org-image-actual-width org-image-actual-width file-thumb) nil nil)))
                (shell-command (format org-imagemagick-display-command
                                         file org-image-actual-width org-image-actual-width file-thumb) nil nil))
              (if (and (car-safe old) refresh)
                  (image-refresh (overlay-get (cdr old) 'display))
                (setq img (save-match-data (create-image file-thumb)))
                (when img
                  (setq ov (make-overlay (match-beginning 0) (match-end 0)))
                  (overlay-put ov 'display img)
                  (overlay-put ov 'face 'default)
                  (overlay-put ov 'org-image-overlay t)
                  (overlay-put ov 'modification-hooks
                               (list 'org-display-inline-remove-overlay))
                  (push ov org-inline-image-overlays))))))))))

该函数使用convert file.pdf -thumbnail "400x400>" file_thumb.png在您的文件夹中生成一个名为thumbnail的file_thumb来代替pdf的覆盖,并强制org-mode显示带有file_thumb的pdf而不对org文件进行任何修改。

此外,因为我使用 babel 用 python 生成图像。它总是需要我更新 _thumb 文件,所以我添加了一个 if 条件来说明这个拇指文件是否存在,如果 pdf 文件更改了我需要同时更改拇指文件......等等!

希望对你有帮助。

【讨论】:

  • 很好的解决方案!这应该是默认行为。
  • 我在一个与 org 文件不在同一文件夹中的图像文件上尝试了此解决方案。但它不会生成 png 文件。我的问题是什么?
  • 你能详细说明一下吗?
  • 我的 org 文件在某个文件夹中。以及另一个文件夹中的 pdf(图像)文件。我插入文件的链接,当我切换内联图像时,我看到一个黑色的小方块。当我检查消息时,我发现它找不到 png 文件。顺便说一句,我正在寻找一种查看大图像的方法 - 这对我有帮助吗?
  • 你能检查(executable-find "convert")的结果吗?这将检查您的执行路径中是否存在“转换”命令。顺便问一下,你能说出你在哪个系统上吗?
【解决方案3】:

简答:不支持 PDF 内嵌图像。

函数org-image-file-name-regexp 具有硬编码的图像文件扩展名(不包括PDF)。这个函数被org-display-inline-images使用,它又调用create-image

我尝试将 PDF 添加到 org-image-file-name-regexp,并从 imagemagik-types-inhibit 删除 PDF,但没有成功。

您可以尝试进一步挖掘,或向 org-mode 邮件列表请求该功能。

【讨论】:

  • 只是更新一下:现在(2020)可以在变量image-file-name-extensions中自定义扩展列表。
猜你喜欢
  • 2016-07-27
  • 2013-04-17
  • 1970-01-01
  • 2015-01-23
  • 2012-10-11
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多