Let's say I have a very long org-mode file main.org with 100 top-level headlines:

* H1
* H2
* H100

I'd like to export main.org into

  • 100 .tex files H<n>.tex (1<=n<=100) --or with other filenames-- one for each headline with its corresponding text content,
  • plus one master file main.tex (with a preamble) and 100 inputs \input{H<n>} (1<=n<=100)

Icing on the cake: any export would update H<k>.tex only if a modification of the corresponding headline H was detected in main.org.

Would this be doable?

1 Answer 1


You can write your own LaTeX export backend derived from latex.

That is quite easy because you can inherit almost everything from latex.

The only thing that you need to overwrite is org-latex-headline.

In your own org+-latex-export-to-latex you define the section
counter and the file name base for the exported files.

Here is how this looks like:

(require 'ox-latex)

(org-export-define-derived-backend 'latexfiles
  :translate-alist '((headline . org+-latex-headline))
  '(?L "Export to LaTeX files"

(defun org+-latex-headline (headline contents info)
  "Write CONTENTS into section file and return input statement.
Extract headline level from SECTION.
Get file name base and section number from INFO."
  (let* ((level (org-element-property :level headline)))
    (cl-assert (numberp level) "Expected headline level")
    (setq contents (org-latex-headline headline contents info))
    (if (> level 1)
      (let ((num (plist-get info 'top-header-counter)))
    (cl-assert (numberp num) "Cannot get section number")
    (plist-put info 'top-header-counter (incf num))
    (let ((fn (format "%s-%0.3d.tex" (plist-get info 'file-name-base) num)))
      (with-temp-file fn
        (insert contents))
      (format "\\input{%s}\n" fn))))))

(defun org+-latex-export-to-latex
    (&optional async subtreep visible-only body-only ext-plist)
  "Export to \"main.tex\" and section files."
  (let* ((fn-base (org-export-output-file-name "" subtreep))
     (outfile (concat fn-base ".tex"))
     (ext-plist (append
             `(top-header-counter 0
               file-name-base ,fn-base)
    (org-export-to-file 'latexfiles outfile
      async subtreep visible-only body-only ext-plist)))

(provide 'org+-latex)
;;; org+-latex.el ends here

Put this stuff into a file org+-latex.el within your loadpath and require it in your init file.

There is a new menu item [L] Export to LaTeX files in the Org export dialog window which you can use to export the top level sections to separate files.

  • This is wonderful and clear, thank you. I'm going to implement this, replacing \input by \includeso that I'll be able to add \includeonlyin my preamble to select only specific headlines to be compiled, while keeping all references and page number unchanged, as explained here latexref.xyz/_005cinclude-_0026-_005cincludeonly.html Commented Dec 17, 2021 at 11:29

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.