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:
and a bunch of options files, one for each possible export backend:
#+OPTIONS: toc:2 num:nil H:4
#+OPTIONS: toc:nil num:t H:7
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
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.