33

I draft documents in org-mode and export them to LaTeX, HTML, etc. as needed. I frequently use the tag :no_export: whenever I don't want a certain subtree to show up in the output.

What I also want is want is for the export to completely ignore certain subtree headings, but not to ignore then content of the subtree under that heading. In other words, certain subtrees should be exported as if their content were just a continuation of the parent, instead of having its own subtree.

I'm ideally imagining a tag like :no_title:. I want my org-mode outline to look more or less the way I am used to. But I would consider other approaches with similar results.

To illustrate, here is a MWE of a document drafted in org-mode.

* Chapter 1
** Section 1
*** Paragraph 1      :no_title:
First paragraph of text in the section.
*** Paragraph 2      :no_title:
Second paragraph of text in the section.
* Chapter 2
** Section 1
*** Subsection 1
**** Paragraph 1      :no_title:
First paragraph of text in the subsection.
**** Paragraph 2      :no_title:
Second paragraph of text in the subsection

When I export that currently, I get something like this:

1 Chapter 1

1.1 Section 1

1.1.1 Paragraph 1 :no_title:

First paragraph of text in the section.

1.1.2 Paragraph 2 :no_title:

Second paragraph of text in the section.

2 Chapter 2

2.1 Section 1

2.1.1 Subsection 1

◊ 2.1.1.1 Paragraph 1 :no_title:

First paragraph of text in the subsection.

But what I want is more like this:

1 Chapter 1

1.1 Section 1

First paragraph of text in the section.

Second paragraph of text in the section.

2 Chapter 2

2.1 Section 1

2.1.1 Subsection 1

First paragraph of text in the subsection.

2 Answers 2

34

This may be a preferred way (due to its simplicity) of accomplishing your goal -- it seems to have gotten buried in the comments of another answer:

  1. Add the following to your .emacs file:

     (require 'ox-extra)
     (ox-extras-activate '(ignore-headlines))
    
  2. Use the ignore tag on headlines you'd like to have ignored (while not ignoring their content)

NOTE: currently, add-on ox-extra becomes available after installation of the package org-contrib.

OLDER NOTE: if you are using ELPA to install org-mode, you must add the org-mode repository to your list of ELPA repositories. See here for more details. After this is done, refresh the package list (M-x package-refresh-contents) and install the org-plus-contrib package (e.g. with M-x package-install). Don't forget to get rid of any old org-mode installations.

10
  • 2
    this is the correct answer.
    – rasmus
    Oct 28, 2015 at 22:02
  • I accepted this as the answer even though it will not work for me. I tried loading org-extra.el but some other function was still missing (I am stuck with Org 8.2.4).
    – Brian Z
    Oct 30, 2015 at 2:30
  • I can't find the package ox-extra ...
    – Toothrot
    Oct 15, 2016 at 9:41
  • 1
    @Toothrot it's in the contrib/lisp folder -- see orgmode.org/cgit.cgi/org-mode.git/tree/contrib/lisp/ox-extra.el -- in your emacs config file, you may need to add a line like (add-to-list 'load-path "/path/to/contrib/lisp").
    – Mark
    Oct 17, 2016 at 18:21
  • 1
    In case people are looking for the repository, org-contrib has been moved to its own repository since the time this answer was written.
    – Mark
    Aug 26, 2022 at 18:46
20

Yes, this is entirely possible. Add the following code to your init-file:

(defun org-remove-headlines (backend)
  "Remove headlines with :no_title: tag."
  (org-map-entries (lambda () (delete-region (point-at-bol) (point-at-eol)))
                   "no_title"))

(add-hook 'org-export-before-processing-hook #'org-remove-headlines)

Acknowledgments

This question has been asked on StackOverflow before (with a different focus and wording, which is probably why you weren't able to locate it when you searched the internet before posting here). The code above is a simplified version of the code in the accepted answer; it works for the new exporter introduced in org-mode 8.0 and is backend-agnostic.

Update

As discussed in the comments, the code above causes contents of entries tagged with :no_title: to disappear if they are preceded by an entry that is tagged to be excluded from the exported document. For example, exporting the following document will produce a document with a title and TOC, but no content:

#+EXCLUDE_TAGS: no_export

* No export                          :no_export:
  This is not meant for you to see.
* No title                           :no_title:
  You should see this.

The reason for this is that org-mode removes the headline tagged with :no_title: before it removes the entry whose headline is tagged with :no_export:. As a result, when it gets around to removing the no_export entry, it treats contents of the no_title entry as belonging to the preceding no_export entry. This makes perfect sense because there is no structural element (heading) that separates the contents of the original entries anymore.

To solve this problem, we can transfer responsibility for removing no_export entries to org-remove-headlines. This way, we make sure all no_export entries are gone before headlines are removed from no_title entries:

(defun org-remove-headlines (backend)
  "Remove headlines with :no_title: tag."
  (org-map-entries (lambda () (let ((beg (point)))
                                (outline-next-visible-heading 1)
                                (backward-char)
                                (delete-region beg (point))))
                   "no_export" tree)
  (org-map-entries (lambda () (delete-region (point-at-bol) (point-at-eol)))
                   "no_title"))

(add-hook 'org-export-before-processing-hook #'org-remove-headlines)
17
  • I've been using this a lot, so thanks! But I noticed a problem... If a heading tagged with no_export heading comes right before one tagged with no_title, then the contents of the second heading are also not exported.
    – Brian Z
    Apr 7, 2015 at 8:33
  • @BrianZ You're welcome! Are the no_export and no_title headings siblings (i.e., do they have the same number of leading stars)? If not, which of the two headings has more stars?
    – itsjeyd
    Apr 7, 2015 at 19:47
  • They are siblings. An export of this toy example has no content at all for me (just title, table of contents etc.) pastebin.com/KaiK9qJm
    – Brian Z
    Apr 8, 2015 at 2:35
  • 1
    @BrianZ By itself, org-remove-headlines is just a function; it won't help you much if it's never called from anywhere. So yes, you still need to evaluate the line (add-hook 'org-export-before-processing-hook #'org-remove-headlines) to enable the correct behavior. I probably should have been explicit about that in the edits to my original answer. As for the warnings you're seeing: I don't get those with vanilla Emacs 24.4 and version 8.2.10 of Org Mode. To get rid of the warning you mention you could try to (require 'org-agenda) before evaluating the add-hook line.
    – itsjeyd
    Apr 11, 2015 at 18:42
  • 2
    This feature is also implemented as org contrib. Ref: orgmode.org/cgit.cgi/org-mode.git/plain/contrib/lisp/… Oct 8, 2015 at 8:47

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.