Org LaTeX Export

Posted on March 20, 2021

Prerequisites

Things you’ll need before following through with this video.

  • Decent Org Mode knowledge
  • Decent LaTeX Knowledge
  • Locally installed LaTeX compiler

LaTeX Compiler

This has links for all operating systems. https://www.latex-project.org/get/

Most basic LaTeX Export

The most basic one possible. We only choose the document class and paper size. And, disable the table of contents.

Options

Org Mode header for a basic LaTeX export
#+LaTeX_CLASS: article
#+LaTeX_CLASS_OPTIONS: [letterpaper]
#+OPTIONS: toc:nil

Using your own LaTeX setup

Set up a plain LaTeX class for yourself

This will give us a class that won’t include all the default packages in the generated LaTeX file. Put this in your init file. You can then use it with #+LATEX_CLASS: org-plain-latex.

(with-eval-after-load 'ox-latex
(add-to-list 'org-latex-classes
             '("org-plain-latex"
               "\\documentclass{article}
           [NO-DEFAULT-PACKAGES]
           [PACKAGES]
           [EXTRA]"
               ("\\section{%s}" . "\\section*{%s}")
               ("\\subsection{%s}" . "\\subsection*{%s}")
               ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
               ("\\paragraph{%s}" . "\\paragraph*{%s}")
               ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))))

Create & include a SETUPFILE

A SETUPFILE is simply a file that contains some sort of org mode configuration. For example, the #+LATEX_CLASS: we saw earlier. Or, maybe #+LATEX_HEADER:. It could even be as simple as #+TITLE:. In this SETUPFILE we can place all of our lines of LaTeX code we want included in the final generated document. This is a very powerful and easy method of perfectly customizing our export.

Linking to a SETUPFILE
#+SETUPFILE: ~/Dropbox/Mackup/emacs-stuff/org/jake-standard-latex-export.org

Structure the SETUPFILE

Now, in that SETUPFILE, we place the LaTeX lines we normally would put in a .tex file. For example, \usepackage{amsmath}. Hopefully you have your own favorite .tex file setup, but if not I suggest learning LaTeX and/or finding a template online.

I suggest using a macro to add #+LATEX_CLASS: to each line, rather than typing it manually of course! Notice the LaTeX_CLASS: - the custom one we just created.

#+LaTeX_CLASS: org-plain-latex
#+LaTeX_CLASS_OPTIONS: [letter]
#+LATEX_HEADER: \usepackage{lmodern} % Ensures we have the right font

#+LATEX_HEADER: \usepackage[AUTO]{inputenc}
#+LATEX_HEADER: \usepackage{graphicx}
#+LATEX_HEADER: \usepackage{amsmath, amsthm, amssymb}
#+LATEX_HEADER: \usepackage[table, xcdraw]{xcolor}

% and so on...

Extra Notes

Some notes not covered in the video.

Code

Enable using listings for code highlighting

(setq org-latex-listings 't)

In your SETUPFILE:

#+LATEX_HEADER: \usepackage{listings}

If you enable org-latex-listings but don’t include the listings package in your TeX file, it will likely not compile (if you are exporting an org file with code blocks).

Useful commands

  • org-latex-preview - toggles preview LaTeX math at point

Resources

My setupfile

#+LaTeX_CLASS: org-plain-latex
#+LaTeX_CLASS_OPTIONS: [letterpaper, 11pt]
#+LATEX_HEADER: \usepackage{lmodern} % Ensures we have the right font

#+LATEX_HEADER: \usepackage[T1]{fontenc}
#+LATEX_HEADER: \usepackage[AUTO]{inputenc}
#+LATEX_HEADER: \usepackage{graphicx}
#+LATEX_HEADER: \usepackage{amsmath, amsthm, amssymb}
#+LATEX_HEADER: \usepackage[table, xcdraw]{xcolor}

% Colorizing links in a nicer way.
#+LATEX_HEADER: \definecolor{bblue}{HTML}{0645AD}
#+LATEX_HEADER: \usepackage[colorlinks]{hyperref}
#+LATEX_HEADER: \hypersetup{colorlinks, linkcolor=blue, urlcolor=bblue}

% Moving up the title.
#+LATEX_HEADER: \usepackage{titling}
#+LATEX_HEADER: \setlength{\droptitle}{-6em}

#+LATEX_HEADER: \setlength{\parindent}{0pt}
#+LATEX_HEADER: \setlength{\parskip}{1em}
#+LATEX_HEADER: \usepackage[stretch=10]{microtype}
#+LATEX_HEADER: \usepackage{hyphenat}
#+LATEX_HEADER: \usepackage{ragged2e}
#+LATEX_HEADER: \usepackage{subfig} % Subfigures (not needed in Org I think)
#+LATEX_HEADER: \usepackage{hyperref} % Links
#+LATEX_HEADER: \usepackage{listings} % Code highlighting
% Disables flush alighment on the right side. Personal preference.
# #+LATEX_HEADER: \RaggedRight

% Page geometry
#+LATEX_HEADER: \usepackage[top=1in, bottom=1.25in, left=1.55in, right=1.55in]{geometry}

% Line spacing
#+LATEX_HEADER: \renewcommand{\baselinestretch}{1.15}

% Page numbering - this disables it
# #+LATEX_HEADER: \pagenumbering{gobble}

% Spacing, titling, text setting.
#+LATEX_HEADER: \usepackage[explicit]{titlesec}

% Title customization
#+LATEX_HEADER: \pretitle{\begin{center}\fontsize{20pt}{20pt}\selectfont}
#+LATEX_HEADER: \posttitle{\par\end{center}}
#+LATEX_HEADER: \preauthor{\begin{center}\vspace{-6bp}\fontsize{14pt}{14pt}\selectfont}
#+LATEX_HEADER: \postauthor{\par\end{center}\vspace{-25bp}}

#+LATEX_HEADER: \predate{\begin{center}\fontsize{12pt}{12pt}\selectfont}
#+LATEX_HEADER: \postdate{\par\end{center}\vspace{0em}}


% Section/subsection headings:

%Section
#+LATEX_HEADER: \titlespacing\section{0pt}{5pt}{5pt} % left margin, space before section header, space after section header

%Subsection
#+LATEX_HEADER: \titlespacing\subsection{0pt}{5pt}{-2pt} % left margin, space before subsection header, space after subsection header

%Subsubsection
#+LATEX_HEADER: \titlespacing\subsubsection{0pt}{5pt}{-2pt} % left margin, space before subsection header, space after subsection header 

% List spacing
#+LATEX_HEADER: \usepackage{enumitem}
#+LATEX_HEADER: \setlist{itemsep=-2pt} % or \setlist{noitemsep} to leave space around whole list


# %Section
# #+LATEX_HEADER: \titleformat{\section} {\Large}{\thesection}{1em}{\textbf{#1}} % Section header formatting
# #+LATEX_HEADER: \titlespacing\section{0pt}{5pt}{-5pt} % left margin, space before section header, space after section header

# %Subsection
# #+LATEX_HEADER: \titleformat{\subsection} {\large}{\thesubsection}{1em}{\textbf{#1}}
\titlespacing\subsection{0pt}{5pt}{-5pt} % left margin, space before subsection header, space after subsection header

# %Subsubsection
# #+LATEX_HEADER: \titleformat{\subsubsection} {\large}{\thesubsubsection}{1em}{#1}
\titlespacing\subsubsection{0pt}{5pt}{-5pt} % left margin, space before subsection header, space after subsection header