I'm using sparse trees to find tasks I need to work on in my buffer. The matching headlines are shown, without siblings but with their parents. For example, imagine I have the following tree:

* A
** A1
* B
** B1

And I use sparse trees to match and highlight B1, so emacs shows me the tree:

* A
* B
** B1

I find it very useful to print the whole buffer (export to PDF). However, when I set Visibile only to True and Export scope to buffer, top-level headlines that were not matched are also printed (in this case, A).

I tried to create a top-level headline that includes everything:

* Top
** A
*** A1
** B
*** B1

That way, the sparse tree is:

* Top
** B
*** B1

And, therefore, unmatched (now) second-level headlines, like A, won't be printed.

However, at this point, a second issue emerges: Most of my tasks are 7-levels deep in the tree. This is far too deep for emacs by default, so anything below level 5 is printed at the same level (meaning that tasks and subtasks are printed as if they were siblings).

If I use export options to include deeper headlines (H: 10), then they are printed as paragraphs instead of lists.

Any suggestions?

  • You address two questions in this article. 1st: How to avoid the export of non-matching headlines of an org buffer with sparse tree. 2nd: How to properly print a tree with more than 5 levels. AFAIK on this site one question per article is preferred.
    – Tobias
    Commented Mar 24, 2019 at 0:33

2 Answers 2


Once in the sparse tree you can either:

org-tree-to-indirect-buffer bound to the key sequence C-c C-x b

then you get separately a narrowed buffer with the tree down from where you point, expanding all headings, while keeping the highlighting.

or: org-narrow-to-subtree bound to the key sequence C-x n s

which will narrow while keeping the highlights without expanding any of the folded headings.

Whatever you edit in this buffer will of course affect the original document.

  • Thanks for answering, but I don't see how this really answers my question. I'm trying to export to PDF... Please, see the details of the question, and let me know if anything is confusing.
    – Ivan Perez
    Commented Jun 18, 2018 at 13:20
  • @IvanPerez I am trying to be more specific. This answer has two issues: 1st: This command can only narrow/clone a single top-level headline. But it may be that there are more matching top-level headlines. 2nd: You manually have to go to the matching top-level headline at first. For an instance with the command next-match.
    – Tobias
    Commented Mar 23, 2019 at 22:38
  • Tobias's export filter specifically answers your question. my solution was meant as a work around to get the levels that you wish to export into a separate buffer, in order to export. Commented Mar 24, 2019 at 7:34

I only address your 1st question which is in my own words:

How to avoid the export of non-matching headlines of an org buffer with sparse tree.

The following Elisp code defines an export filter that has only an effect if there are org-occur overlays in the exported org buffer. Those are the overlays highlighting headings in org sparse trees.

If there are any org-occur overlays in the exported buffer only matching headlines are let through by the filter.

When you have installed the Elisp code you do not need to set the Visible Only option anymore to export the sparse tree only. Just having a sparse tree view with at least one org-occur overlay is sufficient.

(require 'org)

(defun my-org-export-only-occur (tree _back-end info)
  "Export TREE only headings with org-occur overlays if there are any.
Don't filter out anything if there are no org-occur overlays.
This is a function for `org-export-filter-parse-tree-functions'.
INFO is the plist of export infos.
We are using :input-buffer of INFO."
  (let ((input-buffer (plist-get info :input-buffer)))
    (if (and input-buffer
         (with-current-buffer input-buffer
        tree 'headline
      (lambda (el)
        (and (org-element-map
             el 'headline
           (lambda (hl)
              (lambda (ol)
            (eq (overlay-get ol 'org-type) 'org-occur))
              (with-current-buffer input-buffer
            (overlays-at (org-element-property :begin hl)))))
           nil t)
      nil nil 'headline)

(cl-pushnew #'my-org-export-only-occur org-export-filter-parse-tree-functions)

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.