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)


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

#+INCLUDE: "example-plot.svg" export html

2 Answers 2


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
      (cl-assert text nil "PostFileToInclude received nil instead of text ")
      (insert text)
      (if (re-search-forward org-any-link-re nil t)
    (progn (let ((fname (match-string 2)))
          (format "#+INCLUDE: \"%s\" export html" fname))
        (error "PostFileToInclude: Was not able to find link in output"))

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

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)

  #+INCLUDE: "file:example-plot.svg" export html

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
#+OPTIONS: html-embed-svg:t"
  :type 'boolean
  :group 'org-export-html)

 '(: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))
    (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))
    (funcall fun source attributes info)))

(advice-add 'org-html--svg-image :around #'org+-html-svg-image-embed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.