EDIT 2021-04-05): there is a better solution, based on an idea of Juan Manuel Macías on the Org mode mailing list. It is based on macro expansion and the fact that you can evaluate arbitrary Lisp code in the macro definition. The details are presented in this Emacs SE question: Can org options be applied to specific export modes only?.
Here is one possible solution that uses the #+INCLUDE:
mechanism. Another possible option is (probably) an options filter, but I have not investigated that.
The idea is to have an Org mode file like this:
#+INCLUDE: opts.<SUFFIX>
* Links
https://emacs.stackexchange.com/questions/60841/can-i-setup-the-orgmode-options-differently-for-different-export-targets
* Foo
foo
** bar
bar
*** baz
baz
**** hunoz
hunoz
***** hukerz
hukerz
and a bunch of options files, one for each possible export backend:
opts.html
#+OPTIONS: toc:2 num:nil H:4
opts.latex
:
#+OPTIONS: toc:nil num:t H:7
etc.
The trick is to then replace the <SUFFIX>
with the relevant backend at the time you export.
org-export-as
, the main function that gets called when you are exporting a file, does things in a definite order:
- include file processing
- macro expansion
- babel processing
- options filters
- pruning of tree
- parse-tree filters
- collect properties
- transcode tree
- final output filters
The point is that you cannot use later things to change earlier things, since the earlier things are already done. Since the includes are processed first, we cannot use any of the other mechanisms to change the suffix. Fortunately, there are various hooks that are applied at specific times during the export process. One such hook is org-export-before-processing-hook
, which is the first thing that is done, before the include file processing
step. This hook is a list of functions (as is any hook), but the functions in this hook are expected to take a single argument: the export backend. The hook runs through the list and calls each function with the current export backend.
Perfect! All we need to do is define a function that does the edit of the #+INCLUDE:
line and add it to the hook, like this:
(defun ndk/org-export-edit-suffix (backend)
(replace-string "opts.<SUFFIX>" (format "opts.%s" backend)))
(add-hook 'org-export-before-processing-hook #'ndk/org-export-edit-suffix)
and we are done. When you export to HTML, the backend is "html" and we include opts.html
and when you export to PDF, the backend is "latex" and we include opts.latex
.
One obvious caveat: make sure that you do not have another instance of the replacement string opts.<SUFFIX>
in your file or it will be modified as well. You can make the replace-string
arguments longer to match e.g. the whole #+INCLUDE:
line, if that is a problem.