11

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.

12

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. – Dieter.Wilhelm May 29 '18 at 7:04
4

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.

1

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
1

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:

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.