Variables in LaTeX documents exported from Emacs Org Mode

Org Mode vs Emacs
Table of Contents

The usual introduction to Emacs, Org Mode and LaTeX

This is the usual introduction to Emacs, Org Mode and LaTeX: if you already know everything you can skip it.

Org Mode under GNU Emacs is a very powerful tool for organizing ideas.

An excellent features of this tool is its ability to directly export documents from Org Mode to LaTeX, the absolute excellence for pagination, as well as to other languages such as Markdown and HTML.

When exporting to LaTeX, you can add commands in the preliminary part of the documento in Org Mode, to customize the output in the final PDF document.

Variables are useful commands that allow LaTeX to automatically replace placeholders fields with names, numerical strings or any other combination of text and numbers.

You can find information on this topic in my previous article.

In the actual article I suggest some solutions for generating variables when exporting documents from Org Mode to LaTeX.

Preamble section in Org Mode for essential export to LaTeX with variables generation

Let’s see the following simple preamble into an Org Mode document:

#+AUTHOR: Name of the Author
#+TITLE: Title of the document
#+LATEX_HEADER: \newcommand{\myname}{Franco}
#+LATEX_HEADER: \newcommand{\mysurname}{Pasut}

The first line configures the english language (default), the line the author’s name and the third line the title of the document.

In the exported LaTeX document you will, then, automatically find the author’s name and the title in the front page.

Of course, other preliminary settings can be added, but this is not the subject of this article.

The lines dedicated to the export of the variables

It is, however, the last three lines that I would like to highlight.

The fourth line #+LATEX_HEADER: \newcommand{\myname}{Franco} creates a new variable called “myname” that will be replaced, in the exported LaTeX document, with the name “Franco”.

The fifth line #+LATEX_HEADER: \newcommand{\mysurname}{Pasut} creates a new variable called “mysurname” that will be replaced, in the exported LaTeX document, with the name “Pasut”.

Now you can scatter the variables \myname{} and \mysurname{}, or any other variable you want to create, in the Org Mode document and, then, get the automatic replacement in the document exported to LaTeX with the names you set, in this case Franco and Pasut.

Be careful not to use variable names that are reserved for the system Emacs: you will soon realise this because the variables will not be transformed correctly.

As you can notice above, variables are followed by a the blank space {}: it’s used to distance the names generated by the variables from the words immediately following them.

If, on the other hand, there is a punctuation mark after the variable, there is no need for such an addition.

Now, to export the Org Mode document to LaTeX you have to press C-c C-e ll or, if you want the automatic opening of the exported document in a PDF reader, C-c C-e lo.

Why variables?

But what’s the purpose of creating variables instead of names? Wouldn’t it be enough to write the content directly in the document?

Very simple: if in the document exported to LaTeX it is necessary to replace the names generated by variables with others, it will be enough to modify the variable assignment in the preamble and you won’t run the risk of leaving some “old” name all over the orginal Org Mode document.

It’s a solution particularly useful if you need to reuse the exported LaTeX document.

Thank you for your attention.


Let’s talk about technology?