gpt4 book ai didi

emacs - org 模式下的内联 PDF 图像

转载 作者:行者123 更新时间:2023-12-03 14:11:18 33 4
gpt4 key购买 nike

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

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

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

最佳答案

让我完成这个问题。

首先,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 文件更改了我需要同时更改拇指文件......等等!

希望它可以帮助你。

关于emacs - org 模式下的内联 PDF 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15407485/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com