Emacs is often thought of as a programmer’s text editor. It has an extensible programming support as well as the ability to automatically evaluate expressions within files. But one thing that Emacs also does well is allowing users to compose beautiful documents with LaTeX through Org Mode.
Tip: Download our Emacs keyboard shortcuts cheatsheet to increase your productivity.
Also read: How to Manage Git in Emacs with Magit
What is LaTeX and Org Mode?
LaTeX is a staple in academic writing. Its beautiful typography and near magic way of tracking references makes it attractive for both students and professors that want to have print-ready publications right from the comfort of their keyboard.
However, LaTeX often disregards the fact that the TeX code behind it is complex and hard to use. For example, creating a table through LaTeX requires you to know a code which involves creating a sub-environment with a syntax that only applies to it.
Because of this, LaTeX is often seen as a difficult program to use especially for users that do not have any background in writing code. Despite that, it is possible to write a LaTeX document without knowing any LaTeX code by using Emacs with Org Mode.
Org Mode is an add-on that you can install in your vanilla Emacs installation. At its core, it allows you to manage bullet-organized documents. You can then use this to track tasks and create outlines for those tasks.
Also read: 5 Hidden Features You Can Use to Improve Emacs
Aside from that, Org Mode also allows you to use its outlining format to create custom documents that you can export to any format you want. This can be useful for those that want to maintain a single file format for their documents. For example, you can create a document using Org Mode’s bullet syntax and easily convert it to HTML.
Also read: 5 Useful Emacs Packages for Better Productivity
How to Install LaTeX and Org Mode
Starting with LaTeX in Emacs is incredibly easy. That’s because it is a highly popular software that almost all Linux distributions carry in their repositories.
- In Ubuntu, you can install LaTeX by running the following command:
sudo apt install texlive-full
After that, you can check whether you have installed the program properly by running the following:
tex -v
- With that done, the next thing that you need to do is to add Org Mode in your vanilla Emacs installation. Just press Alt + X, then type “package-install”.
This will bring up a prompt where you can give the name of the package that you want to install. Type “org-mode”. From there, Emacs will download, compile then install the source code for Org to your machine.
Also read: How to Use Emacs for RSS with Elfeed
Create Your First LaTeX Document in Emacs
- First, you need to create an Org document in Emacs. To do that, you can press Ctrl + X, then Ctrl + F to open up Emacs’ buffer prompt at the current directory.
- Next, go ahead and provide a name for the file that you want to create.
- Once done, create a LaTeX template for your Org document. This will allow you to easily include attributes such as a title, author’s name and publication date. To do so, write these lines at the beginning of your Org document:
#+TITLE: My Title #+AUTHOR: Ramces Red #+EMAIL: ramcesred@domain.com #+DATE: \today
- After that, you can export this to a PDF file by pressing Ctrl + C, then Ctrl + E.
- Doing that will then bring up Org Mode’s Export Menu. From there, you can then to press L to export file it as LaTeX then O to open it afterwards.
Also read: How to Use Email within Emacs
Add Sections and Subsections
With that done, you can now start creating the structure of your document. For example, this is a simple Org file with two first-level and one second-level bullet headings. Under each heading, I wrote a brief paragraph to provide some length to the document:
#+TITLE: My Title #+AUTHOR: Ramces Red #+EMAIL: ramcesred@domain.com #+DATE: \today * This is the first heading This is the paragraph of the first heading of my new LaTeX document. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ** This is the first subheading This is a second-level heading for this document. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. * This is the second heading This is another first-level heading for this document. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Now, you can immediately run this exact document through the Export Menu (as shown above). In doing so, it will use this exact syntax, convert it to LaTeX and then process it to a printable PDF.
The way this works is that Org makes a number of assumptions about how you want your document to look. In this case, it converts each bullet point as a LaTeX section with a corresponding section number. From there, Org then assumes that any text directly under a bullet point are paragraphs that belong to that section.
This intelligent approach, in turn, allows you to continue writing in Org syntax and use that to seamlessly convert to LaTeX code.
Also read: How to Use Emacs As a USENET Reader With Gnus
Use Special Symbols
Another brilliant feature of LaTeX in Org Mode is its ability to easily write special characters from inside Emacs. In traditional LaTeX, writing special symbols for math formulae will require you to know the control command for rendering symbols. For example, in order to write the Greek letter “τ” you need write the following line of code:
{\displaystyle \tau}
This approach can be cumbersome especially for articles and documents that make extensive use of these special symbols. Knowing this, Org can help alleviate this problem through two things: in-editor rendering and tab completion.
- In-editor rendering is a feature in Org that automatically converts any LaTeX special character to its exact printed glyph. In order to enable this, all you need to do is to press Ctrl + C, Ctrl + E then \.
- From there, you can now type any special character in your buffer and Emacs will immediately replace it as soon as you finish typing.
- Aside from that, Org also provides a tab completion feature which can suggest any matching symbol that you are typing. To use this, you can press Esc + Tab while typing the name for the symbol that you want to use.
- This feature can also be helpful if you cannot remember the name of a symbol. For example, you can type “\a” and then Esc + Tab to look for a symbol that starts with “\a”. Org will then look at all the possible symbols and print a buffer where you can choose the one that you want.
Also read: 7 Useful Cron Alternatives For Linux
Add Images
Further, it is also possible to directly include figures and graphics to your LaTeX documents through Org Mode. Unlike traditional LaTeX, inserting images in Org only requires you provide a link for the file that you want to add. Knowing that, consider the following example:
#+TITLE: My Title #+AUTHOR: Ramces Red #+EMAIL: ramcesred@domain.com #+DATE: \today * This is the first heading This is the paragraph of the first heading of my new LaTeX document. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. This is the second paragraph and I want to include a couple of pictures: [[/home/ramces/documents/projects/latex/01/img1.png]] [[./img2.png]]
In this, I included two images in my LaTeX document file. The first is an absolute link that points to a specific image file in my machine. This can be helpful if you want to include images that are located in different parts of your system.
On the other hand, the second one uses a relative link which takes the current directory and looks for a file named “img2.jpg”. Unlike an absolute link, this allows your LaTeX document to be both portable and flexible to changes in file structure.
Create Tables
Another great feature of Org Mode is that you can create tables in it which you can convert to LaTeX. This can be helpful for users that want to create print-ready tables but are not familiar with LaTeX’s table syntax and environment.
- To get started, all you need to do is to press |, then Tab. This will tell Org that you want to create a single column table at the current cursor position.
- Once done, you can then press Tab again to move to the next row.
- The following is an excerpt of an Org document that I made with a single column.
#+TITLE: My Title #+AUTHOR: Ramces Red #+EMAIL: ramcesred@domain.com #+DATE: \today * This is the first heading This is the paragraph of the first heading of my new LaTeX document. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | This is the only column | |--------------------------| | This is a single column | | table in Org Mode. |
- However, it is also possible to create tables with multiple columns in Org. To do this, continue typing past the second | symbol and press Tab. This will tell Org that you want to add a new column right on the current table.
- From there, you can then use Org’s Export Menu to convert this table to the LaTeX format.
Also read: 5 Tricks to Speed Up Compile Times in Gentoo Linux
How to Customize LaTeX’s Preamble in Org Mode
The “Header Field” is a special Org Mode value that you can insert at the start of your document. With this, you can change almost any value on any variable for your LaTeX document. In that regard, the “Header Field” is similar to setting the preamble of your LaTeX document.
- In order to use the Header Field all you need to do is to add
#+LATEX_CLASS_OPTIONS:
at the start of your Org document. - From there, you can then add a bracket list of settings for your LaTeX document. For example, this is a document that is on an A4 size paper with 12 point font.
#+TITLE: My Title #+AUTHOR: Ramces Red #+EMAIL: ramcesred@domain.com #+DATE: \today #+LATEX_CLASS_OPTIONS: [a4paper, 12pt] * This is the first heading This is the paragraph of the first heading of my new LaTeX document. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Also read: 8 Useful and Interesting Bash Prompts
How to Use LaTeX Plugins in Org Mode
Lastly, it is also possible to include external LaTeX plugins through Org Mode. These include bibliography management utilities such as BibTeX as well as advanced plugins such as Geometry. As such, this can be especially useful if you rely on these plugins to create your custom documents.
- Including third-party plugins in Org is relatively straightforward. Similar to adding document-specific options, you can include plugins by adding the “+#LATEX_HEADER” option at the start of your document.
- From there, you can then include the “\usepackage{}” command to enable the plugin that you want to use.
For example, this is an Org document that uses the BibTeX plugin for bibliography management.
#+TITLE: My Title #+AUTHOR: Ramces Red #+EMAIL: ramcesred@domain.com #+DATE: \today #+LATEX_HEADER: \usepackage{bibtex} * This is the first heading This is the paragraph of the first heading of my new LaTeX document. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
That’s a wrap! You now know how to use Org Mode to create and publish LaTeX PDF documents straight from Emacs. Not only that, you also know how Org can allow you to easily create and maintain LaTeX documents without knowing a lot of LaTeX code.
If all this talk made you more curious about LaTeX. You can check this article where we talk about some of the best LaTeX editors that you can install in Linux.
Also read: How to Fix Broken Packages in Linux
Frequently Asked Questions
Is it possible to remove the Table of Contents in my LaTeX documents?
Yes! It is incredibly simple to remove the Table of Contents from your LaTeX documents. In that, all you need to do is to add “#+OPTIONS: toc:f” at the start of your Org document.
Is it possible to create horizontal separators in Org Tables?
Yes! Creating horizontal separators in Org Mode is relatively straightforward. Similar to adding new table columns, all you need to do to add these separators is to press - on a new table row then press Tab. This will, in turn, automatically fill that row with horizontal dashes and move the cursor to the next row.
Is it possible to change the LaTeX document class of an Org document?
Yes! By default, Org Mode assumes that you intend to create an Article-type LaTeX document. This is because an Article-type document should be able to handle most composition jobs. However, this approach can still be an issue for users that want to use the other templates that LaTeX provides. Knowing that, changing the LaTeX document class is simple. All you need to do is to add the “#+LATEX_CLASS:” option followed by the document type that you want to use. For example, you can use LaTeX’s “report” document class instead.
Image credit: Unsplash. All screenshots by Ramces Red
Our latest tutorials delivered straight to your inbox