[Tex/LaTex] Org-mode export to LaTeX: temptation or nuisance

draftemacsorg-modesectioningtables

We are talking about Emacs and Org-mode: There are several advantages of drafting papers with Org-mode and to have them transferred later to LaTeX, e.g.:

  1. I'd like to avoid »programming while writing«. I prefer a clean text without the usual LaTeX-Commands in the editor.
  2. Org-mode has a incredible feature: radio tables. We can have a spreadsheet, calculations and export the whole thing to a LaTeX tabular easily.

Of course the export from Org-mode to LaTeX is more or less basic, see the manual here and here. The more features we implement in Org-mode, the more »crowded« and less clean the view of the text in the window will become. That said, I've got a

Question:

What do you do to draft using Org-mode and to publish in LaTeX? How do you »enrich« your Org-mode file to be able to export to LaTeX without the need to add more than some finishing touches to the result?

Is there anybody out there who has to offer experience and all the dirty tricks?

Edit:

  1. Is there anybody drafting letters and exporting them to LaTeX?
  2. TODO-Lists are coloured in Emacs, but the output in LaTeX is simply black, while to html all colours are conserved. I'd like to have coloured TODO-Lists in the LaTeX output as well!?

Best Answer

When drafting you benefit from effectively being able to write an outline and edit it. Org-mode is a good tool for this. Since Org-mode is an Emacs mode you should know the basics of Emacs, such as commands for navigating in buffers and switching between buffers as described under Learning in A simpleton's guide to (...)TeX workflow with emacs.

The basics of Org-mode are easy to learn but make sure you handle them and their related commands well. All this is found under Document structure in the manual. As you mention LaTeX export from an Org buffer is straightforward. What I do when drafting is basically to edit the Org buffer as usual and then press C-c C-e d once in a while to see the resulting PDF. You should read the sections on export options and LaTeX export in the manual to know what is possible.

Structure and preamble

To see the simple structure of a document I draft in Org-mode see the following:

# -*- mode:org; mode:reftex; indent-tabs-mode:nil; tab-width:2 -*-
#+TITLE: My title
#+AUTHOR: N.N.
#+OPTIONS: toc:t
#+LATEX_CLASS: myarticle

* Introduction...
* Section...
* Section...
* References...

The headings are folded here (hence the ... following them). If they are unfolded the text under them is shown. The first line is a mode line that is not necessary but helpful in telling Emacs that this buffer should have Org-mode switched on, that it should switch on RefTeX (which I use for references) and some things having to do with tabs. The second and third line specify the title and the author of the document. The fourth line sets the export options so that the document includes a table of contents. The fifth line, #+LATEX_CLASS: myarticle, makes Org-mode export with a LaTeX preamble defined in my .emacs. In my .emacs the preamble is defined as follows:

;; My custom LaTeX class for Org-mode export. require is needed for it to work.
(require 'org-latex)

(add-to-list 'org-export-latex-classes
             '("myarticle"
"\\documentclass[a4paper,12pt]{article}

\\usepackage[utf8]{inputenc}
\\usepackage{lmodern}
\\usepackage[T1]{fontenc}

\\usepackage{fixltx2e}

\\newcommand\\foo{bar}
               [NO-DEFAULT-PACKAGES]
               [NO-PACKAGES]
               [EXTRA]"
               ("\\section{%s}" . "\\section*{%s}")
           ("\\subsection{%s}" . "\\subsection*{%s}")
           ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
           ("\\paragraph{%s}" . "\\paragraph*{%s}")
           ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

You can use this as a template to create your own preamble for Org-mode by putting whatever you like your preamble to contain between '("myarticle" " and [NO-DEFAULT-PACKAGES].

For exporting to beamer you can instead use #+LaTeX_CLASS: beamer. This exports to beamer with a given preamble. If you want to define the beamer preamble yourself you can do as follows. Put the following in your .emacs:

;; My custom  class for Org-mode export. require is needed for it to work.
(add-to-list 'org-export-latex-classes
             '("mybeamer"
               "\\documentclass[presentation]{beamer}
               \\usepackage{...}
               [NO-DEFAULT-PACKAGES]
               [NO-PACKAGES]
               [EXTRA]
               [BEAMER-HEADER-EXTRA]"
               org-beamer-sectioning))

This also requires (require 'org-latex) in your preamble. You can include whatever you like your preamble to include between '("mybeamer" and [NO-DEFAULT-PACKAGES]. To export an Org buffer to this class it has to include #+LaTeX_CLASS: mybeamer.

Tricks

Here are some further tips on using Org-mode:

  • If you want Org-mode to open the PDF that results from the export in a particular viewer you have to configure it to do so. To open it in Evince put the following in your .emacs:

    ;; PDFs visited in Org-mode are opened in Evince (and not in the default choice)
    (eval-after-load "org"
      '(progn
         ;; Change .pdf association directly within the alist
         (setcdr (assoc "\\.pdf\\'" org-file-apps) "evince %s")))
    
  • You can use biblatex for citations. Simply include biblatex macros directly in the Org buffer. To insert them you can use RefTeX. See Setting up RefTeX with biblatex citation commands and put the following in your .emacs

     (setq reftex-default-bibliography '("path/to/file.bib")); So that RefTeX in Org-mode knows bibliography
    

    Obviously you need to replace path/to/file.bib with the path to the bib file you want recognized. To include the list of references create a header which you want to hold your references and under it include \printbibliography[heading=none] as in

    * References
    
      \printbibliography[heading=none]
    
  • To export with latexmk put the following in your .emacs:

    ;; Use latexmk for PDF export
    (setq org-latex-to-pdf-process (list "latexmk %f"))
    
  • To export " to csquotes macros put the following in your .emacs:

    ;; Export " to csquotes macros
    (setq org-export-latex-quotes
          '(("en" ("\\(\\s-\\|[[(]\\)\"" . "\\enquote{") ("\\(\\S-\\)\"" . "}") ("\\(\\s-\\|(\\)'" . "`"))))
    
  • To automatically indent lists and similar structures of Org buffers put the following in your .emacs:

    (setq org-startup-indented t); Use virtual indentation for all files
    
  • Org-mode has good support for making footnotes. Use the commands and you will easily create them. To make a new paragraph inside a footnote use \par.

  • If you need to include lots of LaTeX related code to complete a part of a document, e.g. for a TikZ figure, you can do that in a separate document and then include the generated PDF via pdfpages. To do this include #+LaTeX_HEADER: \usepackage{pdfpages} in the beginning of your Org buffer and then at the place you want to include a PDF use:

    #begin_latex
    \includepdf[pages={-}]{path/to/file.pdf}
    #end_latex
    

    Obviously you need to replace path/to/file.pdf with the path to the PDF you want to include.

  • Bonus: you can integrate Stack Exchange with Org-mode. The ideas for this answer was organized in an Org-mode buffer and then the answer was written in markdown-mode.

  • Note: Writing in Org-mode and exporting to LaTeX may work for text, but for a more sophisticated layout (e.g. figures, two figures side by side, etc...), you end up thinking half of the time about how to export it properly from Org-mode to LaTeX. The solution is then to write drafts focusing on the content and structure with Org-mode, and to export it to LaTeX and stick to LaTeX when doing the editing work (adding illustrations, optimizing the flow and the aspect of the work).