I would like emacs lisp to evaluate a code block like the following, but variablevalue should take on different values depending on whether I'm exporting to html or latex:

#+begin_src emacs-lisp :exports none
(setq variablename variablevalue)

I assume the best way to do this would be to have two src blocks (where the assigned value is different in each) and evaluate one if exporting to html and the other when exporting to latex. How to accomplish this?

1 Answer 1


Following the DRY principle ("Don't repeat yourself"), it's better to have a single source block and to parametrize it differently in the two situations.

You can assign the value of a lisp expression to a variable and use it in the code block as follows:

#+begin_src elisp :exports none :var vvalue=(if (eq org-export-current-backend 'html) 4 3)
(setq vname vvalue)

Here the value is going to be 4 during HTML export, 3 in all other situations (including when pressing C-c C-c in the buffer - org-export-current-backend is nil in that case).

You can use a cond for more complicated situations (e.g. in the following, the value is 4 during HTML export, 2 during LaTeX export and 1 in all other situations):

#+begin_src elisp :exports none :var vvalue=(cond ((eq org-export-current-backend 'html) 4) ((eq org-export-current-backend 'latex) 2) (t 1))
(setq vname vvalue)

but the idea is exactly the same: you use a lisp expression to calculate the value that you want to assign to the variable and you use org-export-current-backend to affect it differently during export.

  • Great idea - is there any benefit to defining that in the header than using the expression in the source block? (setq vname (if (eq org-export-current-backend 'html) 4 3))
    – hatmatrix
    Mar 16, 2022 at 10:35
  • 1
    Perhaps not in this case, but in general, I think of a source block as a computation that is independent of details like what export I am doing. Also source blocks can be written in any language, so getting access to org-export-current-backend from e.g. the shell would complicate things quite a bit. Putting the computation in the header (or in another source block that can be used in the header - see Environment of a code block in the manual) serves to separate these concerns.
    – NickD
    Mar 16, 2022 at 13:47
  • Impeccable rationale - thanks.
    – hatmatrix
    Mar 17, 2022 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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