Emacs and TeXShop

Since I spent some time figuring out how to make Emacs and TeXShop interact the way I want, I figured I might as well post and record what I've done.

What I'm using

Goals

  1. Use emacs to edit, teTeX to process and TeXShop to view.
  2. Use a short keyboard command to typeset.
  3. Have the output automatically update.
  4. Be able to process with pdflatex or altpdflatex (the latter means latex source -> dvi -> PostScript -> pdf)

Crucial featurs

When TeXShop (version 2) is properly configured, it will watch a pdf file, and re-open the file if it changes. There does not appear to be any other way to automatically have TeXShop process/reprocess/update a file if it's been changed by an external program.

Thus, all the commands need to be issued by Emacs and need to result in a pdf file. Again, I want as much of this to be automated as possible.

TeXShop settings

In the preferences for TeXShop, under the "Preview" pane, check the button for "Automatic Preview Update". (I don't think it matters if you also check the "Configure for External Editor" button on the Document pane.)

Backend tools

I use teTeX as installed by Gerben Wierda's i-installer. For the altpdflatex process you need to have Ghostscript and dvips installed. I use "altpdflatex" which is a shell script written by Wierda.

Thus, if I type "altpdflatex foo.tex" at a command line, latex is run first, then dvips is run on the dvi file, then ps2pdf is run on the PostScript file; the dvi and the PostScript files are removed. The "altpdflatex" script should run on any unix system (it is available at CTAN).

Emacs

After all of the above, it is clear that the goals are just to get keyboard commands for Emacs to run "pdflatex" or "altpdflatex" on the main file. In plain emacs this is done by binding an emacs command (I forget what it's called, something like 'tex-master-document) to the command line command. In Emacs with auctex I found that the easiest way is to add a new command to the command menu, and then call this with the keyboard.

The .emacs file already contains the following line:

         (list "pdfLaTeX" "pdflatex '\\nonstopmode\\input{%t}'" 'TeX-run-LaTeX nil t)  

This creates a line in the "Command" menu with an item called "pdfLaTeX". This menu item runs the command "pdflatex" on the main tex file, represented by %t.

Thus, I want to add two lines like the above. One which runs "altpdflatex" and one which opens the pdf file in TeXShop.

Here are the first lines I added in my .emacs file (within the command menu list):

      (list "altpdf" "altpdflatex '\\nonstopmode\\input{%t}'" 'TeX-run-LaTeX nil t)
      (list "pdfview" "open -a TeXShop %s.pdf" 'TeX-run-LaTeX nil t)
  

Now, I want keyboard shortcuts which call the menu items "altpdf", "pdfview" and "pdfLaTeX". For some reason in auctex the way I figure out how to do this is to use an intermediate step of creating a function, which is then bound to a keyboard command. For the keyboard commands I used C-t (i.e. control-t) and M-t (i.e. meta-t, or Apple-t). This clobbers pre-existing emacs keyboard commands for "twiddle", i.e. for reversing two adjacent letters (for C-t) and for reversing two adjacent words (for M-t). I don't mind losing these commands; if you do then pick a different keyboard shortcut.

Here's what I added to create the keyboard shortcut M-t for "pdflatex" (these lines are not added to the command menu list, just add them to the file after the list somewhere):

      (global-set-key "\M-t" 'do-pdflatex)
      (defun do-pdflatex ()
            "pdflatex the current file."
            (interactive)
            (TeX-command "pdfLaTeX" 'TeX-master-file))

For "altpdflatex" I created a very similar entry:

    (global-set-key "\C-t" 'do-altpdf)
    (defun do-altpdf ()
        "altpdflatex the current file."
        (interactive)
        (TeX-command "altpdf" 'TeX-master-file))

Finally, I created a (better for me) keyboard shortcut for saving a file:

          (global-set-key "\M-s" 'save-buffer)

Note that this keyboard shortcut only works (in my version of Emacs anyway) in major modes. Thus, for plain text files it does not work. The reasons why Emacs does this are beyond me; but things like this (so-called global settings not working in certain modes) are a feature of Emacs which I continue to find very frustrating.

You might wonder why I bothered to do this since Emacs has C-x C-s. Well, (1) I don't think something I do as often as save my tex file should require two keyboard commands and (2) I use the dvorak keyboard (actually a dvorak keyboard with further modifications to make tex-ing easier) and "x" and "s" are on different hands, so "C-x C-s" is not at all convenient for me to enter.

How I use it


educkworth
Last modified: Thu Sep 15 09:10:58 EDT 2005