20

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.

6 Answers 6

17

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.

1
  • 5
    In my practical experience the drawer solution is the most convenient and fastest way of working with headers. May 29, 2018 at 7:04
8

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.

3
  • Edited my answer in response to this question.
    – jdtonkin
    Jul 3, 2018 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, 2021 at 11:16
  • I tried this, but it only seems to work for export. If I want to do C-c C-x C-l to see the images of the formulas rendered, it doesn't seem to apply header.tex. Is there a way to have the tex file import and have every line prefixed with #+LATEX_HEADER? Nov 25, 2021 at 15:00
5

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.

5

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

* Configuration :ignoreheading:
#+AUTHOR:   
#+STARTUP: showall
...

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

#+BEGIN_EXPORT LATEX
...
#+END_EXPORT

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:
  :PROPERTIES:
  :VISIBILITY: folded
  :END:
2
  • 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, 2019 at 13:45
  • but as I see it you need to set up your emacs config so the ignoreheading is actually honored
    – xeruf
    Aug 3, 2022 at 20:16
1

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.

0

While the answer given by @EFLS is correct, I would like to add that you can in fact have multiple setup files e.g.

#+SETUPFILE: setup
#+SETUPFILE: latex_setup
#+SETUPFILE: other_latex_macros

## Rest of the org buffer

However, care must be taken (possibly) to not have recursive includes, though in practice org seems to avoid that, perhaps with a stack.

The difference between #+SETUPFILE and #+INCLUDE is that the setup file is only searched for org keywords (recursively if required) when required for setting options. #+INCLUDE directives are only searched for export and not for general options settings.

See functions org-collect-keywords, org-set-regexps-and-options and org-export-copy-buffer.

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.