Emacs as the Ultimate LaTeX Editor

Everyone knows, that GNU Emacs is THE Best Programmer’s Editor. Not everyone knows, though, that when you combine it with AUCTeX macros, it also becomes THE Best Editor for LaTeX.

The biggest problem with Emacs is that it’s not a particularly intuitive piece of software, to say the least, hence many users flee after their first encounter with it. Emacs has its complicated keyboard shortcuts, enormous documentation and config files written in a Lisp dialect (called Emacs lisp), so at first it might seem very unpleasant using it. However, once tamed, it becomes your best friend.

I’d like to present some tips that customize Emacs making it a perfect and very sophisticated editor for LaTeX. Most of these ideas are taken from various config files, howtos and other resources found on the web. Some of them are mine, but I can no longer tell which.

First things first: you need to get Emacs and AUCTeX, and get it running.

Every major linux distro comes with both Emacs and AUCTeX available via package systems. In Ubuntu, you just type sudo apt-get install emacs23 auctex and you’re laughing. Emacs is also available for Windows, and AUCTeX website has instructions on how to set it up with Windows systems. Mac users have a choice of setting up either Carbon Emacs (a version closer to original GNU Emacs) or Aquamacs (an Emacs variant supporting tabs and other nice tweaks; preferred Emacs package by all Mac users I know, Karolina included). Full comparison of Mac Emacs variants is available here, so you can make your own choice. Both Carbon Emacs and Aquamacs come with AUCTeX bundled, so there’s no need to download additional packages.

After running Emacs and loading a TeX file (C-x C-f file_name.tex), AUCTeX should load itself automatically. If it doesn’t happen, you can invoke it with M-x tex-mode, or you can put the following into your $HOME/.emacs file:

(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq TeX-save-query nil)
;(setq TeX-PDF-mode t)

(all the code snippets from this post are available as a Github Gist)

The options above will make sure, that AUCTeX macros are loaded every time a TeX file is opened. The last option, ;(setq TeX-PDF-mode t), is commented (all lines beginning with ; are a comment in Emacs Lisp), but you can uncomment it if you want to have PDFLaTeX mode enabled by default for all documents.

AUCTeX has a number of nice features, the two I use most often are:

  • automatic formatting of a section: C-c C-q C-s;
  • section preview: C-c C-p C-s; (see the screenshot on the right)

Preview function is very nice, because you can see the commands that are behind preview images, edit the code, apply preview again and see the results — no need to parse the whole file too often, and most importantly no need to switch to a PDF/PS viewer to see if your math formula/xypic tree is formatted correctly. Trust me, this saves a lot of time.

(update 27.12.2012: there’s another way of previewing LaTeX symbols inside an Emacs buffer, take a look here)

AUCTeX has many many more features, and you can always consult its documentation if you want to learn more. It’s a little bit overwhelming, but learning it is a very good investment, especially if you work with TeX a lot. But there are more packages that provide features which make your life easier.

Flymake is one of those packages. It enables Emacs to check the syntax of your TeX file on-the-fly. To turn it on, put the following code in your $HOME/.emacs:

(require 'flymake)

(defun flymake-get-tex-args (file-name)
(list "pdflatex"
(list "-file-line-error" "-draftmode" "-interaction=nonstopmode" file-name)))

(add-hook 'LaTeX-mode-hook 'flymake-mode)

Beware, though — flymake consumes quite a lot of CPU power, especially when used with large files (and paradoxically large files make it most useful).

On the other hand, spell-checking while you type isn’t so cpu consuming, and you can turn it on with:

(setq ispell-program-name "aspell") ; could be ispell as well, depending on your preferences
(setq ispell-dictionary "english") ; this can obviously be set to any language your spell-checking program supports

(add-hook 'LaTeX-mode-hook 'flyspell-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-buffer)

Another nice package is the Outline Mode. It allows the user to hide some parts of the text file, which makes working with large files much easier. To enable it, put the following in $HOME/.emacs:

(defun turn-on-outline-minor-mode ()
(outline-minor-mode 1))

(add-hook 'LaTeX-mode-hook 'turn-on-outline-minor-mode)
(add-hook 'latex-mode-hook 'turn-on-outline-minor-mode)
(setq outline-minor-mode-prefix "C-c C-o") ; Or something else

Now you can fold sections, subsections, chapters, or the whole document. To hide all the contents of your current section, use C-c C-o C-l. You can apply it to a chapter, subsection, etc. You can also move to a next unit of your document with C-c C-o C-n, or to a previous one with C-c C-o C-p. If you’re lost and want to see the whole document again, use C-c C-o C-a.

Folding and unfolding parts of the text might be confusing, though, but there’s another way to navigate through a big TeX file, and you can use Reftex mode for it. Reftex is a mode that helps with managing references (full documentation), but it can also be used to create a table of contents for a TeX file and to navigate using it. Here is my configuration for Reftex from my .emacs file:

(require 'tex-site)
(autoload 'reftex-mode "reftex" "RefTeX Minor Mode" t)
(autoload 'turn-on-reftex "reftex" "RefTeX Minor Mode" nil)
(autoload 'reftex-citation "reftex-cite" "Make citation" nil)
(autoload 'reftex-index-phrase-mode "reftex-index" "Phrase Mode" t)
(add-hook 'latex-mode-hook 'turn-on-reftex) ; with Emacs latex mode
;; (add-hook 'reftex-load-hook 'imenu-add-menubar-index)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)

(setq LaTeX-eqnarray-label "eq"
LaTeX-equation-label "eq"
LaTeX-figure-label "fig"
LaTeX-table-label "tab"
LaTeX-myChapter-label "chap"
TeX-auto-save t
TeX-newline-function 'reindent-then-newline-and-indent
TeX-parse-self t
TeX-style-path
'("style/" "auto/"
"/usr/share/emacs21/site-lisp/auctex/style/"
"/var/lib/auctex/emacs21/"
"/usr/local/share/emacs/site-lisp/auctex/style/")
LaTeX-section-hook
'(LaTeX-section-heading
LaTeX-section-title
LaTeX-section-toc
LaTeX-section-section
LaTeX-section-label))

Once Reftex is loaded, you can invoke the table of contents buffer with C-c =

All right, enough. If I mention any more packages, I guess it will scare off those who aren’t already scared. I know that Emacs is a bit peculiar with its complicated keyboard shortcuts, enormous documentation and thousands of modes. It’s not easy to learn, but definitely worth it. I remember that switching from Vim to Emacs for LaTeX editing wasn’t easy, but I never regretted that, and I hope whoever’s going to switch under the influence of this post will not regret it either.

update 06.10.2011: I am happy to see that this post is very popular. Anyone convinced that Emacs is the best LaTeX editor out there might want to check out some other posts in Emacs and/or LaTeX categories.

12 thoughts on “Emacs as the Ultimate LaTeX Editor

  1. Pingback: Tweets that mention Emacs as the Ultimate LaTeX Editor | Sound and Complete -- Topsy.com

  2. Pingback: What type of fishing rod/reel combo would be best? | Pro Bass Fishing Blogger

  3. Thanks for the reminder — I turned off preview-latex years ago, on an ancient computer that couldn’t handle it, and haven’t turned it back on since, despite a couple of upgrades in the meantime.

    Perhaps it’s worth mentioning, for folks new to this whole business, that AUCTeX is well set up for customisation (M-x customize-group(ENTER)AUCTeX). You can set heaps of options there, and browsing the list will give you an idea of what AUCTeX might be capable of that you don’t know about yet.

  4. What about
    1) whizzytex-mode
    or
    2) a buffer in doc-view-mode focused on the output file and auto-revert-mode on?
    I think that these setups are quite amazing (couldn’t find anything better so far)

  5. 1) I didn’t know/try whizzytex.

    2) doc-view-mode is cool, but I don’t actually need to see the preview of the whole document. math formulas and xypic drawings are what I’m interested in.

    Thanks for the tip about whizzytex though, I’ll give it a try.

  6. Pingback: Upgrading to Emacs 23 from Carbon Emacs | Marko Budišić

  7. Good piece of writing. Cheers for sharing this. I was not knowledgeable of your weblog, but will come back more frequently now. Adding you to my RSS reader.

Comments are closed.