Friday, August 18, 2006

LaTeX Tip: Emacs, AUCTeX and Preview TeX - A Fantastic Editing Environment

I think I've discovered the best new environment for LaTeX under Windows, it's Emacs 22 with AUCTeX 11.38. It's the sort of setup that mostly just works. With a bit of tweaking, it really does just works. How rare it is to find this situation in the world of software!

This post covers how to get and install this combo under Windows, how to do a few useful customisations and a few useful commands.

The best place to start is to download the pre-built Windows version of Emacs with the latest AUCTeX built in (download here). The installation of this version is dead simple: download it, un-zip it and run the program runemacs.exe in the bin directory.

First off, the latest Emacs 22 has fantastic Windows integration, way ahead of Emacs 21 or XEmacs. Things like cut-and-paste now actually work. It's worth upgrading to Emacs 22 if you're running any other version of Emacs or Emacs variant.

AUCTeX is a package that includes a whole bunch of useful commands, syntax highlighting and formatting for LaTeX and related programs. The most useful AUCTeX command is C-c C-c, which will compile and view your LaTeX document.

Preview-TeX is now built into AUCTeX and works with both regular LaTeX and PDF LaTeX documents (i.e. it works with EPS, PDF and PNG images and outputs). Preview-TeX will go through your LaTeX document and interpret and display in place various LaTeX elements, including section headings, equations and figures.

A few useful Preview-TeX commands are
  • run the preview on the whole document: C-c C-p C-d
  • run the preview at a point: C-c C-p C-p
  • run the preview on an environment: C-c C-p C-e

Customising the Emacs Startup
The only annoyances with the Emacs startup is that it always displays the splash screen and it does not necessarily start at a nice size. This can be easily fixed by adding a few lines to the .emacs file (usually at C:\.emacs).

To disable the splash screen, add the line
(setq inhibit-startup-message t)
and to set the window size, in the number of displayed characters, add the lines
(set-frame-height (selected-frame) 40)
(set-frame-width (selected-frame) 80)

Make AUCTeX Play Nice with MikTeX
By default, AUCTeX expects that the LaTeX installation is a typical UNIX one, which is rarely the case with a Windows environment. As it happens, AUCTeX can reconfigure itself nicely to use the MikTeX (one of the most popular implementation of LaTeX for Windows) setup quite easily. It is simply a matter of adding
(require 'tex-mik)
to the .emacs file.

Make AUCTeX Jump to the Current Position in the DVI File
By Default, when AUCTeX invokes the DVI viewer, it does so without any fancy options. This means no jumping to position or inverse editing. This is easily enabled by default by adding
(setq TeX-source-specials-mode t)
to the .emacs file.


  1. Thank you for your article. But I couldn't find .emacs in C:\. I am using Windows Vista. Can you help me? Thanks.

  2. As I've never touched a Vista box, I can't give any specific Vista-related tips. However, it could just be that you don't have a .emacs file yet. That file doesn't get created until you save some customisations.

    Under the Options menu, change one of the customisation options and then, again in the Options menu, select Save Options. That should create a .emacs file for you.

    If you still can't find your .emacs after that, just do a file search. That file should be somewhere on you system, but it's not necessarily the same place between two installations.

  3. Thank you very much. I follow your advice and found .emacs in C:\emacs\

  4. Günter Rote27 May, 2007 13:40

    In my Installation, on Windows Vista,
    the .emacs file is in
    C:\Users\Günter\AppData\Roaming (where Günter is my user name)
    The Appdata directory is not shown in the windows explorer or with the DIR command; it has to be typed in manually.

    (Still, I have a problem accessing MikTeX from emacs: Miktex (and the previewer) complain that "The file system path could not be retrieved." and exit abnormally. I have to call latex from thecommand line. (When I log in with an administrator account, everything works fine.)

  5. Windows Vista has some new way of doing things. Solution:

    1. go to command line
    2. make sure that you can run pdflatex fine
    3. type "set"
    4. copy everything that was printed to the screen, eg:


    5. create a new batch file somewhere, name it pdflatex.bat
    6. paste the stuff from above into your file, and prefix each line with "set ":

    set HOMEPATH=\Users\Foo
    set LOCALAPPDATA=C:\Users\Foo\AppData\Local

    7. Put "@echo off" at the first line, and "pdflatex.exe %*" at the last:

    @echo off
    set HOMEPATH=\Users\Foo
    set LOCALAPPDATA=C:\Users\Foo\AppData\Local
    pdflatex.exe %*

    8. save, and point texlipse or your editor to this batch file instead of pdflatex.exe

  6. Thanks Tor,

    That will help when I eventually migrate to Vista.

  7. This comment has been removed by a blog administrator.