Making Emacs+LaTeX+AASTeX Sing

Emacs works well with LaTeX, and can be used to control all aspects of the tedious document compile/view/rinse/lather/repeat process, but to really make it sing, including queing the viewer up to a chosen point in the text document, and visa versa, you need to invoke a bit more magic.

Emacs with Skim/synctex

The code below, using XDVI with source specials, is deprecated. Source specials can affect the layout of the file, and, at least on OSX, there is no reasonable DVI viewer (XDVI runs, but is painfully slow). Moreover, direct PDF output is preferred by modern LaTeX packages.

Luckily, a new scheme for forward/reverse searching has been released in recent years: SyncTeX. This produces a file alongside the PDF output named file.synctex.gz which contains information mapping between output position within the PDF and source file position. Together with the fantastic PDF viewer Skim on OSX, this offers a much better experience.

It's easy to get this running. Install Skim in your normal /Applications directory, and get emacs 23, and AucTex from MacPorts. Then modify .emacs to include:

(add-hook 'LaTeX-mode-hook 
	  (lambda ()
	    ;; Enable source-specials for Control-click forward/reverse search.
	    (TeX-PDF-mode 1)
	    (TeX-source-correlate-mode 1)
	    (setq TeX-source-correlate-method 'synctex)

	    (setq TeX-view-program-list
		     "/Applications/ -g %n %o %b"))
		  '((output-pdf "Skim")))))

Optionally, you can include some of the convenience bindings from the deprecated section below. Now just Command-shift-click in Skim, or invoke View (C-c C-v) in AUCTeX, to queue up the other file to the matching location. In Skim, if you use MacPorts emacs, you may need to configure the direct path to /opt/local/bin/emacsclient.

BibTeX and RefTeX

  1. (optional) Grab and install an updated version of RefTeX, to make managing your bibliography oh so fun. It comes pre-installed with most Emacs versions, so check beforehand.
  2. Go learn about BibTeX here.
  3. Learn about using BibTeX and ADS together here, and grab the bib style file apj.bst from that page.
  4. When entering references, instead of a traditional bibliography, use, e.g.:

    referencing .bib files either explicitly, or anywhere on the $BIBINPUTS path.

  5. To enter references, just C-c [ type any part of any of the fields, and hit return on the correct reference.
  6. Send me a postcard to thank me.

XDVI and LaTeX with reverse search (deprecated)

  1. Go get and install AucTeX, a fabulous LaTeX mode for Emacs, which doesn't come with it by default.
  2. Modify your .emacs file to include the following:
    ;;Bind shift mouse-3 to the imenu, and meta shift mouse-3 to alphabetical imenu
    (when window-system 
      (define-key global-map [S-down-mouse-3] 'imenu)
      (define-key global-map [M-S-down-mouse-3]
        (lambda ()
          (let ((imenu-sort-function 
    	(call-interactively 'imenu))))) ;;===> (La)TeX
    ;;===> (La)TeX
    (require 'tex-site) ;; AUCTeX
    (defun latex-signal-view-reload (process name)
      ;; Force all XDVIs to reload: modify the binary name as necessary
      ;; ===== EDIT ======
      (shell-command "killall -USR1 xdvi-motif.bin")
      (TeX-LaTeX-sentinel process name))
    (defun latex-explicit-latex ()
      (TeX-save-document "")
      (TeX-command TeX-command-default 'TeX-master-file)
      ;;overload sentinel function to send USR1 signal to view
      (setq TeX-sentinel-function 'latex-signal-view-reload))
    (add-hook 'LaTeX-mode-hook 
    	  (lambda ()
    	    ;; Enable source-specials for Control-click forward/reverse search.
    	    (TeX-source-specials-mode 1)
    	    (setq TeX-source-specials-view-start-server t)
    	    (setq reftex-plug-into-AUCTeX t)
    	    ;; Special arguments to put XDVI in the correct place.
                ;; ===== EDIT ======
    	    (setq xdvi-extras 
    		  (concat "-expert -xoffset .35in -yoffset .4in "
    			  "-s 4 -geometry 1100x1500+0+0"))
    	    ;; Add the above arguments to all instances of xdvi
    	    (unless (get 'TeX-output-view-style 'xdvi-extra)
    	      (mapcar (lambda (list)
    			(let ((func (nth 2 list)))
    			  (if (string-match "xdvi" func)
    			      (setq func 
    				     (concat "\\& " xdvi-extras) t nil func)))
    			  (setcar (last list) func)))
    	      (put 'TeX-output-view-style 'xdvi-extra t))
    	    ;; Bind F5 to latex and reload xdvi, F6 to dislay.
    	    (local-set-key [f5] 'latex-explicit-latex)
    	    (local-set-key [f6] 'TeX-view)
    	    ;; Make M-up/down skip among sections
    	    (local-set-key [M-down] 
    			   '(lambda ()
    	    (local-set-key [M-up] 
    			   '(lambda ()
    	    ;; Some conveniences
        	    (setq TeX-auto-save t)
    	    (setq TeX-save-query nil)
    	    (setq TeX-parse-self t)
    	    (setq-default TeX-master nil)
    	    ;; For reftex to recognize AASTeX tables etc. correctly
    	    (setq reftex-label-alist 
    		  '(  ; Some AASTeX modes for quick references
    		    ("deluxetable" ?t nil nil nil nil)
    		    ("deluxetable*" ?t nil nil nil nil)
    		    (nil ?s nil "\\,\\ref{%s}" nil "\\S")
                        (nil ?e nil "~\\ref{%s}" nil)
    		    ("\\figcaption[]{}" ?f nil nil 1 nil)
  3. Adjust the XDVI options (-s the initial zoom, in particular), and binary name where it says ==== EDIT ==== above to suit your system (common names are xdvi.bin, xdvi, xdvi-motif.bin: just start xdvi and ps auxw | grep xdvi to see).
  4. Now use F5 to run LaTeX and reload any XDVI processes, F6 to view your document with XDVI and queue it to the exact enclosing paragraph, table, figure, etc., highlighting it with a colored box around it.
  5. In XDVI, control-click on any paragraph, table, figure, etc. to have your LaTeX buffer automatically queued up at that exact position. Wow.
  6. In a LaTeX buffer, using Shift-mouse-3 to show all your sections/subsections/etc.

JD Email Last modified: Wed May 18 18:23:17 EDT 2011