How to create LaTeX documents with Emacs

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.

document sending
Image by : 

opensource.com

x

Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

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.png

Emacs startup screen

GNU Emacs with the 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.

insert_template_flow.png

template_flow.png

Inserting 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_file.png

The 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

org_to_pdf.png

Exporting helloworld.org to helloworld.pdf

org_and_pdf_file.png

org_and_pdf_file.png

Opening the helloworld.pdf file

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

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.png

The 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.

Topics

About the author

psachin
Sachin Patil - Sachin works at Red Hat and 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 works on OpenStack & ManageIQ. He also like to explore Swift Object Storage in spare time. He can be reached on IRC as psachin@{Freenode, OFTC, gnome}. Sachin blogs at http://psachin.gitlab.io