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
'latex
:translate-alist '((headline . org+-latex-headline))
:menu-entry
'(?L "Export to LaTeX files"
org+-latex-export-to-latex)
)
(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)
contents
(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."
(interactive)
(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)
ext-plist)))
(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.