5

I wish to embed the svg file output of gnuplot as inline svg code in the html export. Currently I do the following: 1. Manually remove the RESULTS block. 2. INCLUDE the svg file as shown in the MWE below.

How can i configure the RESULTS block to do what I want automatically?

#+BEGIN_SRC gnuplot :file example-plot.svg :exports code
plot sin(x)
#+END_SRC

#+RESULTS:
[[file:example-plot.svg]]

# I manually remove the above RESULTS block
# and insert the following INCLUDE

#+INCLUDE: "example-plot.svg" export html
  • Has either of the answers answered your question? One of them seems to have worked for another person, so maybe it worked for you as well? If so, please [accept](https://emacs.stackexchange.com/help/someone-answers) one of the answers. If not, can you provide feedback on what you tried and what didn't work? Thanks! – NickD May 19 '23 at 12:02

2 Answers2

7

The cleanest solution would be if the babel authors would include it as a option for blocks that can produce SVG output. The solution I give here uses a :post option that invokes another src code block filtering the results of the current block. This is a powerful technique, but it can also make the code more difficult to read and debug.

First I define the postFileToIclude filter function to use in the later code block, and I test it by giving it a file link string (someplot.svg) explicitely.

  #+NAME: postFileToInclude
  #+HEADER: :var text="[[file:someplot.svg]]"
  #+BEGIN_SRC elisp :results value raw drawer
    (with-temp-buffer
      (erase-buffer)
      (cl-assert text nil "PostFileToInclude received nil instead of text ")
      (insert text)
      (beginning-of-buffer)
      (if (re-search-forward org-any-link-re nil t)
    (progn (let ((fname (match-string 2)))
         (replace-match
          (format "#+INCLUDE: \"%s\" export html" fname))
         ))
        (error "PostFileToInclude: Was not able to find link in output"))
      (buffer-string)
      )
  #+END_SRC

  #+RESULTS: postFileToInclude
  :RESULTS:
  #+INCLUDE: "file:someplot.svg" export html
  :END:

Now we use this function in a :post argument for filtering your src block. One also needs to add a :results option, so that the output is not in an example environment. I also add a drawer, so that repeated executions will be able to correctly replace the results of previous evaluations.

  #+BEGIN_SRC gnuplot :file example-plot.svg :exports code :post postFileToInclude :results raw drawer
    plot sin(x)
  #+END_SRC  

  #+RESULTS:
  :RESULTS:
  #+INCLUDE: "file:example-plot.svg" export html
  :END:
dfeich
  • 1,844
  • 16
  • 16
2

The following code adds the option html-embed-svg to the html export backend of Orgmode. You can also set this option with the meta-comment #+HTML_EMBED_SVG: t.

If you set this option to t SVG images are embedded into the exported HTML document.

The default value is nil which stands for the standard behavior to link svg images.

(require 'ox-html)
(require 'nxml-mode)

(defcustom org+-html-embed-svg nil
  "Embed SVG images.
You can set this variable in Org files with
#+HTML_EMBED_SVG: t
or
#+OPTIONS: html-embed-svg:t"
  :type 'boolean
  :group 'org-export-html)

(cl-pushnew
 '(:html-embed-svg "HTML_EMBED_SVG" "html-embed-svg" org+-html-embed-svg)
 (org-export-backend-options (org-export-get-backend 'html)))

(defun org+-html-svg-image-embed (fun source attributes info)
  "Make embedding of SVG images possible in org HTML export.
SVG images are embedded if :html-embed-svg is non-nil in the plist INFO.
Otherwise FUN called with SOURCE, ATTRIBUTES, and INFO as arguments.
SOURCE is the file name of the SVG file.
This is an around advice for `org-html--svg-image' as FUN."
  (if (member (plist-get info :html-embed-svg) '("yes" "t" t))
      (with-temp-buffer
    (insert-file-contents source)
    (with-syntax-table nxml-mode-syntax-table
      (while (and (search-forward "<svg") ;; barfs if a "<svg" is not found in code
              (nth 8 (syntax-ppss)))))
    (delete-region (point-min) (match-beginning 0))
    (buffer-string))
    (funcall fun source attributes info)))

(advice-add 'org-html--svg-image :around #'org+-html-svg-image-embed)
Tobias
  • 32,569
  • 1
  • 34
  • 75