5

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

6
  • I would have added a comment, but unfortunately I dont have enough reputation. Here's a very nice tutorial, that should fit your needs. emacs org lisp tutorial
    – user3508321
    Jun 10, 2014 at 8:19
  • Moreover, try to post your question there: tex.stackexchange.com
    – user3508321
    Jun 10, 2014 at 10:57
  • @user3321294 Be aware that the tutorial mentioned in this answer is for org-mode versions older than 8.0, and org-mode "has transitioned to a new export framework".
    – itsjeyd
    Jun 10, 2014 at 12:06
  • I can't reproduce the behavior you describe. When I export your example input file to .tex the resulting file contains Project 1 as a top-level \section: \section{Project 1}. What version of org-mode are you using? I am on version 8.2.6. What did you do to prevent org from including Project 1 in the output file?
    – itsjeyd
    Jun 12, 2014 at 8:56
  • You are correct -- I updated the example to correct that error. Does it now look like what you get? I am using org-mode 8.2.5. Jun 12, 2014 at 16:26

1 Answer 1

5

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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.