How to create LaTeX documents with Emacs

This tutorial takes you through the process of creating a document in LaTeX, a powerful open source typesetting system, using GNU Emacs.
277 readers like this.
document sending

Opensource.com

In his excellent article, An introduction to creating documents in LaTeX, author Aaron Cocker introduces the LaTeX typesetting system and explains how to create a LaTeX document using TeXstudio. He also lists a few LaTeX editors that many users find helpful in creating LaTeX documents.

This comment on the article by Greg Pittman caught my attention: "LaTeX seems like an awful lot of typing when you first start...". This is true. LaTeX involves a lot of typing and debugging, if you missed a special character like an exclamation mark, which can discourage many users, especially beginners. In this article, I will introduce you to GNU Emacs and describe how to use it to create LaTeX documents.

Creating your first document

Launch Emacs by typing:

emacs -q --no-splash helloworld.org

The -q flag ensures that no Emacs initializations will load. The --no-splash-screen flag prevents splash screens to ensure that only one window is open, with the file helloworld.org.

Emacs startup screen

GNU Emacs with helloworld.org file opened in a buffer window.

Let's add some LaTeX headers the Emacs way: Go to Org in the menu bar and select Export/Publish.

template_flow.png

How to insert a default template.

In the next window, Emacs offers options to either export or insert a template. Insert the template by entering # ([#] Insert template). This will move a cursor to a mini-buffer, where the prompt reads Options category:. At this time you may not know the category names; press Tab to see possible completions. Type "default" and press Enter. The following content will be inserted:

#+TITLE: helloworld
#+DATE: <2018-03-12 Mon>
#+AUTHOR:
#+EMAIL: makerpm@nubia
#+OPTIONS: ':nil *:t -:t ::t <:t H:3 \n:nil ^:t arch:headline
#+OPTIONS: author:t c:nil creator:comment d:(not "LOGBOOK") date:t
#+OPTIONS: e:t email:nil f:t inline:t num:t p:nil pri:nil stat:t
#+OPTIONS: tags:t tasks:t tex:t timestamp:t toc:t todo:t |:t
#+CREATOR: Emacs 25.3.1 (Org mode 8.2.10)
#+DESCRIPTION:
#+EXCLUDE_TAGS: noexport
#+KEYWORDS:
#+LANGUAGE: en
#+SELECT_TAGS: export

Change the title, date, author, and email as you wish. Mine looks like this:

#+TITLE: Hello World! My first LaTeX document
#+DATE: \today
#+AUTHOR: Sachin Patil
#+EMAIL: psachin@redhat.com

We don't want to create a Table of Contents yet, so change the value of toc from t to nil inline, as shown below:

#+OPTIONS: tags:t tasks:t tex:t timestamp:t toc:nil todo:t |:t

Let's add a section and paragraphs. A section starts with an asterisk (*). We'll copy the content of some paragraphs from Aaron's post (from the Lipsum Lorem Ipsum generator):

* Introduction

  \paragraph{}
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras lorem
  nisi, tincidunt tempus sem nec, elementum feugiat ipsum. Nulla in
  diam libero. Nunc tristique ex a nibh egestas sollicitudin.

  \paragraph{}
  Mauris efficitur vitae ex id egestas. Vestibulum ligula felis,
  pulvinar a posuere id, luctus vitae leo. Sed ac imperdiet orci, non
  elementum leo. Nullam molestie congue placerat. Phasellus tempor et
  libero maximus commodo.

helloworld_file.png

Helloworld.org file

With the content in place, we'll export the content as a PDF. Select Export/Publish from the Org menu again, but this time, type l (export to LaTeX), followed by o (as PDF file and open). This not only opens PDF file for you to view, but also saves the file as helloworld.pdf in the same path as helloworld.org.

org_to_pdf.png

Exporting helloworld.org to helloworld.pdf

org_and_pdf_file.png

The helloworld.pdf file opened at the bottom

You can also export org to PDF by pressing Alt + x, then typing "org-latex-export-to-pdf". Use Tab to auto-complete.

Emacs also creates the helloworld.tex file to give you control over the content.

org_tex_pdf.png

Emacs with LaTeX, org, and PDF files open in three different windows

You can compile the .tex file to .pdf using the command:

pdflatex helloworld.tex

You can also export the .org file to HTML or as a simple text file. What I like about .org files is they can be pushed to GitHub, where they are rendered just like any other markdown formats.

Creating a LaTeX Beamer presentation

Let's go a step further and create a LaTeX Beamer presentation using the same file with some modifications as shown below:

#+TITLE: LaTeX Beamer presentation
#+DATE: \today
#+AUTHOR: Sachin Patil
#+EMAIL: psachin@redhat.com
#+OPTIONS: ':nil *:t -:t ::t <:t H:3 \n:nil ^:t arch:headline
#+OPTIONS: author:t c:nil creator:comment d:(not "LOGBOOK") date:t
#+OPTIONS: e:t email:nil f:t inline:t num:t p:nil pri:nil stat:t
#+OPTIONS: tags:t tasks:t tex:t timestamp:t toc:nil todo:t |:t
#+CREATOR: Emacs 25.3.1 (Org mode 8.2.10)
#+DESCRIPTION:
#+EXCLUDE_TAGS: noexport
#+KEYWORDS:
#+LANGUAGE: en
#+SELECT_TAGS: export
#+LATEX_CLASS: beamer
#+BEAMER_THEME: Frankfurt
#+BEAMER_INNER_THEME: rounded


* Introduction
*** Programming
    - Python
    - Ruby

*** Paragraph one

    Lorem ipsum dolor sit amet, consectetur adipiscing
    elit. Cras lorem nisi, tincidunt tempus sem nec, elementum feugiat
    ipsum. Nulla in diam libero. Nunc tristique ex a nibh egestas
    sollicitudin.

*** Paragraph two

    Mauris efficitur vitae ex id egestas. Vestibulum
    ligula felis, pulvinar a posuere id, luctus vitae leo. Sed ac
    imperdiet orci, non elementum leo. Nullam molestie congue
    placerat. Phasellus tempor et libero maximus commodo.

* Thanks
*** Links
    - Link one
    - Link two

We have added three more lines to the header:

#+LATEX_CLASS: beamer
#+BEAMER_THEME: Frankfurt
#+BEAMER_INNER_THEME: rounded

To export to PDF, press Alt + x and type "org-beamer-export-to-pdf".

latex_beamer_presentation.png

Latex Beamer presentation created using Emacs and Org mode

I hope you enjoyed creating this LaTeX and Beamer document using Emacs (note that it's faster to use keyboard shortcuts than a mouse). Emacs Org-mode offers much more than I can cover in this post; you can learn more at orgmode.org.

Tags
psachin
Sachin is passionate about Free and Open source software. He is avid GNU Emacs user and likes to talk and write about open source, GNU/Linux, Git, and Python. He has previously worked on OpenStack, ManageIQ/CloudForms & Red Hat Insights. He also likes to explore Swift Object Storage in his spare time. He can be reached on IRC as psachin@{Libera.Chat, Freenode, OFTC, gnome}.

14 Comments

This is interesting. At Scribus, we're in the process of conceptually at least working on transforming the current documentation. The idea was to switch to markdown, though as I look up markdown, it seems the main emphasis is going from markdown to HTML. Org-mode would seem to be an interesting alternative.

Thanks for the mention and I’m glad you liked the article :), it’s very reciprocal because ironically I’ve just started using Emacs in the past few months and have been looking for a way to properly use LaTeX in Emacs.

This takes a while to get the hang of.
I ran into a problem on export, when it told me that it couldn't find wrapfig.sty.
How do I make sure I have all the needed files?

Most of the times the dependencies are taken care by installing texlive

```
sudo dnf install texlive
```
but sometimes they have to be installed individually. I'm glad that you fixed the issue by looking at the errors.

In reply to by Greg P

Me again. Now that I got the PDF issue fixed, I tried exporting to HTML, which worked, although the \paragraph{} showed up on the webpage. Also, there is this Validate link created (that fails), but I don't know where that came from.

Hi Greg, you might want to use the html template instead of default template to export has HTML. Instead of using 'default' category use 'html' and that should do the work.

Regarding \paragraph and \today, those are not valid for html export(they are LaTeX tags).

Below is the content I used for html export

```
#+TITLE: Hello World! My first LaTeX document
#+OPTIONS: html-link-use-abs-url:nil html-postamble:auto
#+OPTIONS: html-preamble:t html-scripts:t html-style:t
#+OPTIONS: html5-fancy:nil tex:t
#+CREATOR: Emacs 25.3.1 (Org mode 8.2.10)
#+HTML_CONTAINER: div
#+HTML_DOCTYPE: xhtml-strict
#+HTML_HEAD:
#+HTML_HEAD_EXTRA:
#+HTML_LINK_HOME:
#+HTML_LINK_UP:
#+HTML_MATHJAX:
#+INFOJS_OPT:
#+LATEX_HEADER:
#+OPTIONS: tags:t tasks:t tex:t timestamp:t toc:nil todo:t |:t

* Introduction

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras lorem
nisi, tincidunt tempus sem nec, elementum feugiat ipsum. Nulla in
diam libero. Nunc tristique ex a nibh egestas sollicitudin.

Mauris efficitur vitae ex id egestas. Vestibulum ligula felis,
pulvinar a posuere id, luctus vitae leo. Sed ac imperdiet orci, non
elementum leo. Nullam molestie congue placerat. Phasellus tempor et
libero maximus commodo.
```

In reply to by Greg P

It almost seems like you need to create a generic text document, then paste in the settings after(?). Or maybe write a Perl or Python script to transform default settings to html(?). If I'm going to use this, I may have to write my own documentation, since what I find out there is thick and/or terse -- unless you want to keep writing about this....

In reply to by Greg P

I should mention, making seperate files to export to pdf and html seems redundant and not in the nature in org-mode. I usually have a one file for every export!

The way OP is adding paragraphs are not needed, leaving an empty line will should automattically do it AFIK!. Even then if you want to add inline latex commands that only export in latex that is also doable use an option I can't remember, Just google "Latex export org mode".

In reply to by Greg P

As I said, the latex tag "\today" is not read as expected when converting to HTML. Simply specifying a date would have solved the problem.

"\paragraph" was added due to the fact that the content was copied from Aaron's post(Simply wanted to demonstrate that "it works")

"... a one file for all" is the ultimate case and user gets that with time. This was an introduction, I wanted to keep it simple.

In reply to by Justin Kaipada (not verified)

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.