How do I specify different actions for different export backends in org-mode?

To be more specific, I would like to export an pdf figure in latex export, while using a png in html export (in fact, a png thumbnail linking to the pdf, if that is of relevance):

#+CAPTION: cation of figure
#+NAME:    myfig
# case export to latex: [[file:./figure.pdf]]
# case export to html:  [[file:./figure.pdf][file:./figure.png]]

I tried macros and this answer, which did not work. There are also answers on similar cases for tikz pdf/svg/png export like this thread, which do not seem to be transferable to my case.

Note: I initially posted this question on stackoverflow. It occured to me later that emacs stackexchange is a more appropriate site for this question. So I deleted it on stackoverflow (there were no answers) to repost it here.

  • In some cases you can use things like #+begin_html or by-backend macro (I think I picked it from Org wiki somewhere), example here: raw.githubusercontent.com/wvxvw/introduction-to-data-structures/… . But I'm unaware of general purpose mechanism for this. – wvxvw Dec 12 '16 at 16:32
  • @wvxvw: The #+begin_html/#+begin_latex blocks would be fine for a single figure or so, but I have plenty in my document. However, searching for by-backend macros on google lead me to the solution, thank you! – lccopper Dec 13 '16 at 12:29

I finally found a way to do this, using a macro provided by fniessen:

#+MACRO: if-latex-else (eval (if (org-export-derived-backend-p org-export-current-backend 'latex) "$1" "$2"))

If more/other export-backends than LaTeX and HTML are used, a condition statement can be used instead:

#+MACRO: if-latex-html-else (eval (cond ((org-export-derived-backend-p org-export-current-backend 'latex) "$1") ((org-export-derived-backend-p org-export-current-backend 'html) "$2") (t "$3") ))

Usage would be something like

{{{if-latex-html-else([[file:./figure.pdf]], [[file:./figure.pdf][file:./figure.png]], /Figure *figure.pdf* is supposed to be here/)}}}

Not as short as a simple switch, but substantially better than providing native LaTeX/HTML/whatever code for every figure that is to be included.

| improve this answer | |

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.