Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to always show inline images?

I'm trying to work with inline images (e.g. for plotting data by gnuplot), and have the problem: images always inserted as links by default. I need to do some keypresses to "force" emacs to show actual image inline, instead of just file link.

E.g. I start with gnuplot code:

#+BEGIN_SRC gnuplot :file plot.png
plot sin(x)
#+END_SRC

When I press C-c C-c on this code block, it runs, and shows me results as link to image file:

#+RESULTS:
[[file:plot.png]]
  • If I press C-c C-x C-v (org-toggle-inline-images) twice -- link does replaced with inline image
  • If I run M-x org-redisplay-inline-images -- again, link does replaced with image
  • If I run (org-display-inline-images t t) -- again, image is shown

and so on (those options were taken from Emacs org-display-inline-images and Inline images in org-mode questions)

But I don't want to press anything special: I want images to be displayed inline by default. I've found and tried following variables:

  • (setq org-startup-with-inline-images t) in .emacs config
  • #+STARTUP: inlineimages header
  • (setq org-display-inline-images t)

But neither got me the behavior I want. I'm puzzled -- do I want something so unnatural?

P.S. Im' using GNU Emacs v26.1 on MacOS X, org mode v9.1.9-65, if it matters

P.P.S. Although it seems like a bug in my emacs/orgmode version, and I'm yet to report it, but meanwhile I've found following trick: (add-hook 'org-babel-after-execute-hook 'org-display-inline-images 'append) (thanks to ob-ipython authors) -- it fixes issue for me right now. Maybe will be useful for somebody else

like image 953
BegemoT Avatar asked Jan 19 '19 17:01

BegemoT


2 Answers

I can reproduce the problem with:

Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ /home/xyz/.emacs.d/elpa/org-plus-contrib-20190415/)

Reproduction:

  1. Start Emacs 26.3 with emacs -Q.
  2. M-x load-library RET org RET
  3. Add Gnuplot to org-babel-load-languages via M-x customize-option.
  4. Load gnuplot.el
  5. Open the Org file with the following content and press C-c C-c on the source block.
#+STARTUP: inlineimages
Some text.

#+BEGIN_SRC gnuplot :file plot.png :results graphics
plot sin(x)
#+END_SRC

I have a similar solution as you suggested in your question, but a bit more differentiated.

Re-displaying images in large Org documents can take some time. So I do it only if the source block has the results-parameter 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)
like image 171
Tobias Avatar answered Oct 04 '22 22:10

Tobias


@Tobias is the best answer. I have tweaked the @Tobias code to further optimize by bounding the re-display to the current subtree and setting the REFRESH parameter to t to redisplay only if necessary.

  (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)
like image 32
Daniel Doherty Avatar answered Oct 04 '22 22:10

Daniel Doherty