3

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

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.