I'm working on a big text in orgmode, which is expected to be finally exported to Latex & PDF, so I made this kind of a master document in orgmode:

#+SETUPFILE: exportoptions.org
#+INCLUDE: "01.org"
#+INCLUDE: "02.org"

so I can work on individual chapters. Each of those also has

#+SETUPFILE: exportoptions.org

at the top, so I can check the final result as I go. Only now I'm unable to export the master document as some of those options block Latex-ing when included more than once.

Is it possible to conditionally process the #+SETUPFILE directive, e.g. to ignore it when it's used in included files? I've thought :only-contents t would help, but no.

  • Not that I know of: you'll have to delete (or comment out) the #+SETUPFILE: lines in all the included files.
    – NickD
    Commented Mar 26 at 19:15
  • I was sort of hoping for some clever elisp trick to do that on the fly. I've seen some in the context of this SE, only I don't understand them.
    – yury10578
    Commented Mar 27 at 4:04
  • If you provide links, I can take a look at them.
    – NickD
    Commented Mar 27 at 14:22
  • You can of course use tools to delete (or comment out) those lines: either on the command line with find and sed (on Linux or other *nix systems) or using Emacs search and replace facitilities
    – NickD
    Commented Mar 28 at 1:43
  • Yes, of course I can construct a Makefile and use GNU text utilities. That remains an option anyway. Only I was thinking about something on the lines of emacs.stackexchange.com/a/64340/30404. Maybe a similar sort of trick could be provided in my case. Me, I can't go anywhere from there. No knowledge of orgmode internals at all.
    – yury10578
    Commented Mar 28 at 5:39

1 Answer 1


As nothing seems to be forthcoming, I'm posting my stopgap solution, which demonstrably works, although not in a manner I'd wished.

Put the code into text file, change the working directory to where your .ORG files are, run the text file with the Bourne shell of your choice (BASH).

It will search for all .ORG files named by the pattern two numbers, not a number, something else, .org, then sort the names and merge the files into one .org file, removing SETUPFILE directive from all of those after the first one, and removing TOC directives from all but the 'real' TOC file (named two numbers, toc, .org).

There's some duplicated AWK code there, but I just didn't want to spend more time on this.

#! /bin/sh
ttt="omg.org" # the final ORG file
mkdir -p "${ddd}"
rm "${nnn}"

# some naming conventions
# two numbers and toc ending with .org 
# signifies TOC-containing file 
# which shouldn't be purged
# the result goes into toc_if_any var
toc_if_any=` find . -maxdepth 1 -name '*.org' \
| grep -E '//?[0-9][0-9][^0-9][^/]+$' \
| grep -E '([0-9][0-9]toc\.org)' \
| sort \
| head -n1 `

find . -maxdepth 1 -name '*.org' \
| grep -E '//?[0-9][0-9][^0-9][^/]+$' \
| sort \
| xargs -I [] \
sh -c "
echo Doing \"[]\"

[ -n \"${toc_if_any}\"  -a  -f \"${toc_if_any}\" ] &&
  ( echo \"[]\" | grep -E --quiet '[0-9][0-9]toc\.org\$' ) && this_is_toc=1 # reference with \$

( 2>/dev/null 1>/dev/null grep -E -i '^#\\+setupfile' \"${nnn}\" && 
  cat \"[]\" \
  | awk -v this_is_toc=\$this_is_toc 'BEGIN {IGNORECASE=1 ; print \"\\n# !!! []\\n\"; } 
      /^#\\+(setupfile)/ {next}  
      /^#\\+(toc)/ { if (1 != this_is_toc) next }  
      // {print \$0} ' \
  >> \"${nnn}\" ; exit 0 
) || 
  cat \"[]\" \
  | awk -v this_is_toc=\$this_is_toc 'BEGIN {IGNORECASE=1 ; print \"# !!! []\"; } 
    /^#\\+(toc)/ { if (1 != this_is_toc) next }  
    // {print \$0} ' \
  >> \"${nnn}\"

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.