Selectively exporting headlines in org-mode

| categories: org-mode | tags: | View Comments

I have on several occasions needed to selectively export only portions of an org document. For example, I may write a problem set or exam, and have some headlines that are the problems, and others that are the solutions. Or, I may have done some analysis in a headline, e.g. statistics of problem scores that I do not want in exported content, or I have a manuscript with a supporting information section that does not go in the exported manuscript, and the manuscript cannot go in the supporting information file.

org-mode supports selective export through use of tags. However, this is inflexible if you want to export multiple different documents with different selective exports, unless you don't mind changing the settings, or commenting and uncommenting lines all the time.

Enter dynamic binding of variables in emacs-lisp. We can temporarily define variables, especially org-export-select-tags and org-export-exclude-tags, and write emacs-lisp code blocks to export the current document the way we want. First, let us create some headlines.

1 problem 1   problem

description of a hard problem

1.1 solution to problem 1   solution

it is an easy solution

2 problem 2   problem

what is the square root of 100?

2.1 solution to problem 2   solution

why it's 10 of course.

3 Code to export   code

3.1 Export just the problems

First, let us output this document with just the problems. The code block does that, just put your cursor in in block and press C-c C-c (in emacs of course).

(let ((org-export-exclude-tags '("solution" "code"))
      (org-latex-default-packages-alist
       '(("" "lmodern" nil)
         ("linktocpage,
  pdfstartview=FitH,
  colorlinks,
  linkcolor=blue,
  anchorcolor=blue,
  citecolor=blue,
  filecolor=blue,
  menucolor=blue,
  urlcolor=blue" "hyperref" t)))
      (async nil)
      (subtreep nil)
      (visible-only nil)
      (body-only nil)
      (ext-plist '()))
  (org-latex-export-to-pdf async subtreep visible-only body-only ext-plist))
(rename-file "blog.pdf" "blog-1.pdf")

You get this: blog-1.pdf which only has the problems in it.

3.2 Problems and solutions

Next, we consider the problems and the solutions. We cannot just get solutions in this document because solutions are nested in the problems.

(let ((org-export-exclude-tags '("code"))
      (org-latex-default-packages-alist
       '(("" "lmodern" nil)
         ("linktocpage,
  pdfstartview=FitH,
  colorlinks,
  linkcolor=blue,
  anchorcolor=blue,
  citecolor=blue,
  filecolor=blue,
  menucolor=blue,
  urlcolor=blue" "hyperref" t)))
      (async nil)
      (subtreep nil)
      (visible-only nil)
      (body-only nil)
      (ext-plist '()))
  (org-latex-export-to-pdf async subtreep visible-only body-only ext-plist))
(rename-file "blog.pdf" "blog-2.pdf" t)

This document (blog-2.pdf ) now has problems and solutions. Note that neither of these documents has the section we labeled :code:.

4 Summary

Tags are a nice way to label sections. In combination with dynamic binding, you can build code blocks that selectively export pieces of an org-file by temporarily defining the values of variables. In this example, there are four versions of this document: 1) the org-file that contains everything, 2) the html blog post, 3) a pdf with the problems, and 4) a pdf with problems and solutions. Good luck getting that out of a Word file ;)

Copyright (C) 2013 by John Kitchin. See the License for information about copying.

org-mode source

blog comments powered by Disqus