I often write notes in org mode and export them to latex. However, because some documents are complex I need to include many latex packages at the top like so:

#+LATEX_HEADER: \usepackage[margin=1.15in]{geometry}
#+LATEX_HEADER: \usepackage{parskip}
#+LATEX_HEADER_EXTRA: \usepackage{graphicx}
#+LATEX_HEADER_EXTRA: \usepackage{mdframed}
#+LATEX_HEADER_EXTRA: \usepackage{needspace}
#+LATEX_HEADER_EXTRA: \usepackage{hyperref}         
#+LATEX_HEADER_EXTRA: \usepackage{titling}
#+LATEX_HEADER_EXTRA: \usepackage{enumitem}
#+LATEX_HEADER_EXTRA: \usepackage{etoolbox}                             
#+LATEX_HEADER_EXTRA: \usepackage{color}                             
#+LATEX_HEADER_EXTRA: \usepackage{underscore}...

Is it possible to store all the latex header commands (\usepackage, macros definitions etc.) in a separate tex file so that I can just have a single line include at the top of the org file? This will reduce a lot of the clutter and make it easier for me to add packages.


You can make use of a #+SETUPFILE:

Put all of these settings in a file, for example ./setup/setupfile.org, and then include them into your document with

#+SETUPFILE: ./setup/setupfile.org

In this file, you can also include #+OPTIONS:, #+AUTHOR, and similar in-buffer settings. What's even more useful, is that if you load the #+SETUPFILE: first, you can then override some of these configurations with local settings.

See also the org manual on exporting.

Another solution is to make use of #+INCLUDE: "./setup/more-settings.org" src org (note the ") which simply includes the file contents as org source. This is a little more versatile, as there can more than one #+INCLUDE where there (afaik) can be only one #+SETUPFILE.

Finally, to hide the clutter visually, you can also put all of these local settings in a drawer (i.e., between a line with :DRAWER: and a line with :END:), and simply hide them with tab.

  • 3
    In my practical experience the drawer solution is the most convenient and fastest way of working with headers. May 29 '18 at 7:04

An alternative solution is to check/customize the variable org-latex-packages-alist - add there all your needed \usepackage{...}.

This variable is declared in and used by the ox-latex, so your org configuration must have an declaration (require 'os-latex). This way no lines to add to org documents, except the title.

As for macros, an usual place to store is the yasnippet package, if you use it.


An alternative solution is to use a non exported headline to wrap configuration:

* Configuration :ignoreheading:
#+STARTUP: showall

The advantage of this solution vs drawers is that you can easily include sophisticated configuration, such as LaTeX blocks


which are not read (by default) when in drawers.

The advantage of this solution vs configuration files (setupfile or include) is that you can easily change the configuration since it is included in the current file.

Finally, if you want this headline to remain closed by default when opening you org file, you can use the following syntax:

* Configuration :ignoreheading:
  :VISIBILITY: folded
  • I have but one upvote to give - but this just became my new default! All of these answers should go into the org-manual as suggested best-practice for latex export, as a document-focused setup vs putting most of the setup in emacs config.
    – glallen
    Oct 17 '19 at 13:45

Rather than use #+SETUPFILE: as per the accepted answer, if you're just interested in LaTeX output, then you could add the following instead:

#+LATEX_HEADER: \input{header.tex}

In header.tex (or whatever you want to call it), you can then put all your LaTeX preamble without the need for a whole bunch of #+LATEX_HEADER: arguments. To be super meta, you could also have this #+LATEX_HEADER option in your #+SETUPFILE.

  • Edited my answer in response to this question.
    – jdtonkin
    Jul 3 '18 at 4:11
  • This is what I'm doing currently. \input{header.tex} is much more convenient separating out the formatting and content. Jan 16 at 11:16

You can also create your own org-latex-class. Have a look at C-h v org-latex-classes and evaluate (info "(org) LaTeX header and sectioning") for more info.

Then just add #+LATEX_CLASS: <added_class> at the beginning of your org file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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