Using macros as the accepted answer is a great option, which I also used following this question. But there is one alternative that I started using and that some may prefer: define a
src block with emacs-lisp and a call it with
#+CALL for each figure we want to include.
It works like this, add the block below to your org-mode document (it can be inside a heading with the
:noexport: tag) and change it according with your preferences.
#+begin_src emacs-lisp :var filename="default" :exports none :results raw
(html (concat filename ".svg"))
(twbs (concat filename ".svg"))
(latex (concat filename ".pdf"))
(t (concat filename ".png")))
Now, whenever you want to include a figure just add something like
and execute the block with
C-c C-c. This will add the result as
Now you can add
#+CAPTION: The figure caption.,
#+ATTR_LATEX: ..., etc above the
The trick here is that we get a
png image when we are not exporting, which can be nicely displayed with
org-toggle-inline-images. However, during the export the
#+CALL: line is re-evaluated and we get a
pdf image for latex export and an
svg image for HTML export.
The advantage of this solution, compared with a macro solution, goes beyond just displaying the image inline. Since you can pass a variable to a
src block that receives the result of another
src block it means that you can have a
src block using your preferred language that generates "backend dependent" export code.