How can I make Org-mode export to LaTeX with a specific preamble?

This doesn't answer your question, but it does allow you to do what you want.

(defun headless-latex ()
  "exports to a .tex file without any preamble"
  (interactive)
  (org-export-as-latex 3 nil nil nil t nil)
)

This function exports the content of your ORG-mode file without any preamble. You can then \input it into a file with your desired preamble. Further reading.


I use a different method to get things done :

Define a class (I call it per-file-class for some strange reason. You can call it something else). Put this code in your .emacs :

;; per-file-class with minimal packages
(unless (find "per-file-class" org-export-latex-classes :key 'car
              :test 'equal)
  (add-to-list 'org-export-latex-classes
               '("per-file-class"
                 "\\documentclass{article}
                [NO-DEFAULT-PACKAGES]
                [EXTRA]"
                 ("\\section{%s}" . "\\section*{%s}")
                 ("\\subsection{%s}" . "\\subsection*{%s}")
                 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                 ("\\paragraph{%s}" . "\\paragraph*{%s}")
                 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))))

Use this class in your org file :

#+LaTeX_CLASS: per-file-class

#+LaTeX_CLASS_OPTIONS: [10pt, a4paper]


Deactivated packages (such as geometry above)

Org-mode recognizes LaTeX syntax inside LaTeX code-blocks, as well as when including LaTeX files in the content. (See Quoting LaTeX code.)

Packages loaded by RequirePackage

As above.

Input macros

As above.

\immediate\write18 macros

I believe this should also be as above, however there is an alternate method of dealing with this. If you create a source code block of type sh with the command within it, Org will evaluate it on export and produce the desired behaviour. You have to enable sh as a babel language type for it to work however.

(require 'ob-shell)

You can also include sh as one of the languages loaded by babel by adding it to org-babel-load-languages

(acons 'sh 't org-babel-load-languages)

Then use a code block similar to the following to run your ./vc

#+name: Test
#+begin_src sh :results output silent :exports results
  ./vc
#+end_src

As long as this comes before your \input{vc} line it should run the code and then include it. Simply follow the code-block with

#+LATEX: \input{vc}

And your content should be included.

Comments after usepackage macros

If the code is within a LaTeX block it should recognize it as LaTeX.

A hypersetup macro that recognizes #+TITLE and #+AUTHOR from Org-mode files.

This will have to be included within each document rather than separate. The following will provide what you desire for your macros. It will not be within the preamble, however it will end up at the top of the document and the export does behave as expected (however it will not behave as expected if added through #+INCLUDE: from org.

#+begin_latex
  \hypersetup{% Setup for hyperref
  pdftitle    = {{{{TITLE}}}}, %Org macro to take from #+TITLE
  pdfauthor   = {{{{AUTHOR}}}} %Org macro to take from #+AUTHOR
  }
#+end_latex

Creating your own Latex export class

If you follow the instructions in the worg tutorials (See Org Latex Export) you can create your own export-class. If you want to have full control over the packages in the preamble you would simply need to:

(add-to-list 'org-export-latex-classes
             '("<CLASS NAME>"
               "\\documentclass{article}
               [NO-DEFAULT-PACKAGES]
               [NO-PACKAGES]"
               <insert desired sectioning configuration>))

You can also add in your desired packages between the \\documentclass and [NO-DEFAULT-PACKAGES] lines. The alternative would be to add them to the file itself using:

#+LATEX_CLASS: <CLASS NAME>
#+LATEX_HEADER: \usepackage{package}
...

As a third option, you can simply create a custom .sty file with the desired packages etc and include it as a single #+LATEX_HEADER:.