user3321294
user3321294

Reputation: 329

Emacs org: Promote all headings of a subtree during export?

I use emacs org a lot to export parts of org documents to latex/pdf. I was wondering whether there is a way to promote all headings of the selected parts during the export process. For instance, suppose the file looks like this:

* Project 1
** Task 1                   :export:
*** Introduction
    Text text text. 
*** Results
    Text text text. 
* Project 2

The emacs org export to latex would produce a tex file of the following structure:

\section{Project 1}
\subsection{Task 1}
\subsubsection{Introduction}
    Text text text. 
\subsubsection{Results}
    Text text text.

But because there is not highest level in the part to be exported, it would make more sense to have the following structure:

\section{Task 1}
\subsection{Introduction}
    Text text text. 
\subsection{Results}
    Text text text.

Or, even better:

\title{Task 1}
\maketitle
\section{Introduction}
    Text text text. 
\section{Results}
    Text text text.

I was wondering whether anyone has an idea how to go about this? My lisp skills are unfortunately very rudimentary, seems like it should not be too hard.

Thanks!

Stephan

Upvotes: 5

Views: 1082

Answers (1)

itsjeyd
itsjeyd

Reputation: 5280

The first behavior you describe can be achieved by adding the following to your .emacs:

;; Define a function for turning a single subtree into a top-level tree
;; (:export: headings might be located at an arbitrary nesting level,
;; so a single call to "org-promote-subtree" is not enough):
(defun org-promote-to-top-level ()
  "Promote a single subtree to top-level."
  (let ((cur-level (org-current-level)))
    (loop repeat (/ (- cur-level 1) (org-level-increment))
          do (org-promote-subtree))))

;; Define a function that applies "org-promote-to-top-level" 
;; to each :export: subtree:
(defun org-export-trees-to-top-level (backend)
  "Promote all subtrees tagged :export: to top-level.
BACKEND is the export back-end being used, as a symbol."
  (org-map-entries 'org-promote-to-top-level "+export"))

;; Make org-mode run "org-export-subtrees-to-top-level" as part of the export
;; process:
(add-hook 'org-export-before-parsing-hook 'org-export-trees-to-top-level)

Implementing the second behavior is a bit trickier but you can use theorg-export-trees-to-top-level function as a starting point if that's what you ultimately need. I'd like to point out, however, that this will not work for files with more than one :export: subtree (unless you also come up with a way to decide which headline would become the \title in these cases).


Sources:

Upvotes: 5

Related Questions