如何始终显示内联图像?
How to always show inline images?
我正在尝试使用内联图像(例如,通过 gnuplot 绘制数据),但遇到问题:默认情况下,图像总是插入为 links .我需要对 "force" emacs 进行一些按键操作以显示 实际图像 内联,而不仅仅是文件 link.
例如我从 gnuplot 代码开始:
#+BEGIN_SRC gnuplot :file plot.png
plot sin(x)
#+END_SRC
当我在此代码块上按 C-c C-c
时,它会 运行s,并将结果显示为 link 到图像文件:
#+RESULTS:
[[file:plot.png]]
- 如果我按
C-c C-x C-v
(org-toggle-inline-images) 两次 - link 会替换为内联图像
- 如果我 运行
M-x org-redisplay-inline-images
-- 同样,link 确实替换为图像
- 如果我 运行
(org-display-inline-images t t)
-- 再次显示图像
等等(这些选项取自 Emacs org-display-inline-images and Inline images in org-mode 个问题)
但我不想按任何特殊的按钮:我希望默认情况下内联显示图像。我发现并尝试了以下变量:
(setq org-startup-with-inline-images t)
在 .emacs
配置中
#+STARTUP: inlineimages
header
(setq org-display-inline-images t)
但都没有得到我想要的行为。我很困惑 -- 我想要这么不自然的东西吗?
P.S。我在 MacOS X 上使用 GNU Emacs v26.1,组织模式 v9.1.9-65,如果重要的话
P.P.S。虽然这似乎是我的 emacs/orgmode 版本中的一个错误,而且我还没有报告它,但同时我发现了以下技巧:(add-hook 'org-babel-after-execute-hook 'org-display-inline-images 'append)
(感谢 ob-ipython 作者)--它现在为我解决了问题。也许对其他人有用
我可以重现问题:
Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ /home/xyz/.emacs.d/elpa/org-plus-contrib-20190415/)
复制:
- 使用
emacs -Q
启动 Emacs 26.3。
- M-x
load-library
RET org
RET
- 通过 M-x 添加
Gnuplot
到 org-babel-load-languages
customize-option
.
- 加载
gnuplot.el
- 打开包含以下内容的 Org 文件,然后在源块上按 C-c C-c。
#+STARTUP: inlineimages
Some text.
#+BEGIN_SRC gnuplot :file plot.png :results graphics
plot sin(x)
#+END_SRC
我有一个与您在问题中建议的类似的解决方案,但更加不同。
重新显示大型 Org 文档中的图像可能需要一些时间。所以我只在源块具有结果参数 graphics
:
时才这样做
(require 'subr-x)
(defun org+-babel-after-execute ()
"Redisplay inline images after executing source blocks with graphics results."
(when-let ((info (org-babel-get-src-block-info t))
(params (org-babel-process-params (nth 2 info)))
(result-params (cdr (assq :result-params params)))
((member "graphics" result-params)))
(org-display-inline-images)))
(add-hook 'org-babel-after-execute-hook #'org+-babel-after-execute)
@Tobias 是最佳答案。我调整了@Tobias 代码以进一步优化,方法是将重新显示绑定到当前子树并将 REFRESH 参数设置为 t 以仅在必要时重新显示。
(require 'subr-x)
(defun org+-babel-after-execute ()
"Redisplay inline images in subtree if cursor in source block with :result graphics."
(when (org-in-src-block-p)
(let (beg end)
(save-excursion
(org-mark-subtree)
(setq beg (point))
(setq end (mark)))
(when-let ((info (org-babel-get-src-block-info t))
(params (org-babel-process-params (nth 2 info)))
(result-params (cdr (assq :result-params params)))
((member "graphics" result-params)))
(org-display-inline-images nil t beg end)))))
(add-hook 'org-babel-after-execute-hook #'org+-babel-after-execute)
我正在尝试使用内联图像(例如,通过 gnuplot 绘制数据),但遇到问题:默认情况下,图像总是插入为 links .我需要对 "force" emacs 进行一些按键操作以显示 实际图像 内联,而不仅仅是文件 link.
例如我从 gnuplot 代码开始:
#+BEGIN_SRC gnuplot :file plot.png
plot sin(x)
#+END_SRC
当我在此代码块上按 C-c C-c
时,它会 运行s,并将结果显示为 link 到图像文件:
#+RESULTS:
[[file:plot.png]]
- 如果我按
C-c C-x C-v
(org-toggle-inline-images) 两次 - link 会替换为内联图像 - 如果我 运行
M-x org-redisplay-inline-images
-- 同样,link 确实替换为图像 - 如果我 运行
(org-display-inline-images t t)
-- 再次显示图像
等等(这些选项取自 Emacs org-display-inline-images and Inline images in org-mode 个问题)
但我不想按任何特殊的按钮:我希望默认情况下内联显示图像。我发现并尝试了以下变量:
(setq org-startup-with-inline-images t)
在.emacs
配置中#+STARTUP: inlineimages
header(setq org-display-inline-images t)
但都没有得到我想要的行为。我很困惑 -- 我想要这么不自然的东西吗?
P.S。我在 MacOS X 上使用 GNU Emacs v26.1,组织模式 v9.1.9-65,如果重要的话
P.P.S。虽然这似乎是我的 emacs/orgmode 版本中的一个错误,而且我还没有报告它,但同时我发现了以下技巧:(add-hook 'org-babel-after-execute-hook 'org-display-inline-images 'append)
(感谢 ob-ipython 作者)--它现在为我解决了问题。也许对其他人有用
我可以重现问题:
Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ /home/xyz/.emacs.d/elpa/org-plus-contrib-20190415/)
复制:
- 使用
emacs -Q
启动 Emacs 26.3。 - M-x
load-library
RETorg
RET - 通过 M-x 添加
Gnuplot
到org-babel-load-languages
customize-option
. - 加载
gnuplot.el
- 打开包含以下内容的 Org 文件,然后在源块上按 C-c C-c。
#+STARTUP: inlineimages
Some text.
#+BEGIN_SRC gnuplot :file plot.png :results graphics
plot sin(x)
#+END_SRC
我有一个与您在问题中建议的类似的解决方案,但更加不同。
重新显示大型 Org 文档中的图像可能需要一些时间。所以我只在源块具有结果参数 graphics
:
(require 'subr-x)
(defun org+-babel-after-execute ()
"Redisplay inline images after executing source blocks with graphics results."
(when-let ((info (org-babel-get-src-block-info t))
(params (org-babel-process-params (nth 2 info)))
(result-params (cdr (assq :result-params params)))
((member "graphics" result-params)))
(org-display-inline-images)))
(add-hook 'org-babel-after-execute-hook #'org+-babel-after-execute)
@Tobias 是最佳答案。我调整了@Tobias 代码以进一步优化,方法是将重新显示绑定到当前子树并将 REFRESH 参数设置为 t 以仅在必要时重新显示。
(require 'subr-x)
(defun org+-babel-after-execute ()
"Redisplay inline images in subtree if cursor in source block with :result graphics."
(when (org-in-src-block-p)
(let (beg end)
(save-excursion
(org-mark-subtree)
(setq beg (point))
(setq end (mark)))
(when-let ((info (org-babel-get-src-block-info t))
(params (org-babel-process-params (nth 2 info)))
(result-params (cdr (assq :result-params params)))
((member "graphics" result-params)))
(org-display-inline-images nil t beg end)))))
(add-hook 'org-babel-after-execute-hook #'org+-babel-after-execute)