BegemoT
BegemoT

Reputation: 3856

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]]

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:

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

Upvotes: 8

Views: 2375

Answers (2)

Daniel Doherty
Daniel Doherty

Reputation: 245

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

Upvotes: 2

Tobias
Tobias

Reputation: 5198

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)

Upvotes: 4

Related Questions