Tuesday, January 19, 2010

Aquamacs for your LaTeX needs in OS X.

I recently decided that I wanted an improvement over TeXShop. One of the best solutions I came across was Aquamacs, an emacs version specifically configured for LaTeX and OS X. It has very good syntax highlighting and even puts your section headings in bigger bold typeface (after a while you notice how this is REALLY cool). You can also run a preview, which shows the output of figs and equations in your document, a wysiwyg type preview.  Getting it setup took longer than I would have liked, so here is how to do it correctly, without wasting time. Download the Aquamacs .dmg  here http://aquamacs.org/download.shtml

Skim:
Skim is a very good pdf viewer, and works great with aquamacs, enabling forwards and backwards searches. That is, you can jump from a point in your LaTeX document to the corresponding point in the pdf document, and also do the reverse operation. In Skim go to Skim->preferences, and go to the sync tab, tick the "Check for file changes" box and select Aquamacs Emacs as the preset option. Get Skim here http://skim-app.sourceforge.net/ 

Setting up Aquamacs:
Emacs has a rather ugly and convoluted way of changing options. The main hassle has been that Aquamacs doesn't have a preset way of easily doing (latex dvips ps2pdf) and also dvi viewing support in OS X is virtually non-existant, but we'll do some workarounds here.
If you want to fiddle around with any of the LaTeX options go to (once you have a .tex file open) LaTeX->"Customize AUCTeX".

Some settings I recommend is best pasted into the settings file. It is located at /Users/yourfolder/Library/Preferences/Aquamacs_Emacs/customizations.el open it in a text editor (not Aquamacs) and paste the following in after the line: " ;; If there is more than one, they won't work right."

'(LaTeX-command "latex -synctex=1")
 '(TeX-command-list (quote (("TeX" "%(PDF)%(tex) %`%S%(PDFout)%(mode)%' %t" TeX-run-TeX nil (plain-tex-mode texinfo-mode ams-tex-mode) :help "Run plain TeX") ("LaTeX" "%`%l%(mode)%' %t" TeX-run-TeX nil (latex-mode doctex-mode) :help "Run LaTeX") ("dviPS" "%(o?)dvips %d -o %f" TeX-run-command nil (latex-mode doctex-mode) :help "DVI->PS") ("ps2pdf" "ps2pdf14 %f" TeX-run-command nil (latex-mode doctex-mode) :help "Run AMSTeX") ("ConTeXt" "texexec --once --texutil %(execopts)%t" TeX-run-TeX nil (context-mode) :help "Run ConTeXt once") ("ConTeXt Full" "texexec %(execopts)%t" TeX-run-TeX nil (context-mode) :help "Run ConTeXt until completion") ("BibTeX" "bibtex %s" TeX-run-BibTeX nil t :help "Run BibTeX") ("View" "%V" TeX-run-discard t t :help "Run Viewer") ("Print" "%p" TeX-run-command t t :help "Print the file") ("Queue" "%q" TeX-run-background nil t :help "View the printer queue" :visible TeX-queue-command) ("Index" "makeindex %s" TeX-run-command nil t :help "Create index file") ("Check" "lacheck %s" TeX-run-compile nil (latex-mode) :help "Check LaTeX file for correctness") ("Spell" "(TeX-ispell-document \"\")" TeX-run-function nil t :help "Spell-check the document") ("Clean" "TeX-clean" TeX-run-function nil t :help "Delete generated intermediate files") ("Clean All" "(TeX-clean t)" TeX-run-function nil t :help "Delete generated intermediate and output files") ("XeLaTeX" "%`%l%(mode)%' %t" TeX-run-TeX nil (latex-mode context-mode) :help (format "Run X%sLaTeX" (string (decode-char (quote ucs) 600)))) ("Other" "" TeX-run-command t t :help "Run an arbitrary command") ("Jump To PDF" "(aquamacs-call-viewer \"%o\" %(FileLine) \"%b\")" TeX-run-function nil t :help "Jump here in Skim"))))
 '(TeX-output-view-style (quote (("^dvi$" "." "%(o?)dvips -Ppdf -G0 -ta4 %d -o && ps2pdf14 %f && open -a Skim.app %s.pdf") ("^pdf$" "." "open -a Skim.app %o") ("^html?$" "." "open %o")))))
(add-hook 'LaTeX-mode-hook
      '(lambda()
         (defun do-LaTeX ()
           "Compile LaTeX"
(interactive)
           (TeX-command "LaTeX" 'TeX-master-file))
(defun do-dvips ()
           "dvi --> ps (portrait)"
           (TeX-command "dviPS" 'TeX-master-file))
(defun do-ps2pdf ()
           "Compile ps2pdf..."
           (TeX-command "ps2pdf" 'TeX-master-file))
(defun do-comp-pdf ()
           "Compile LaTeX + dvips + ps2pdf..."
(interactive)
           (TeX-command "LaTeX" 'TeX-master-file) (TeX-command "LaTeX" 'TeX-master-file) (do-dvips) (do-ps2pdf))
         (local-set-key "\C-c\C-t\C-t" 'do-LaTeX)
         (local-set-key "\C-c\C-t\C-u" 'do-comp-pdf)
         ))

This part here "'(TeX-output-view-style (quote (("^dvi$" "." "%(o?)dvips -Ppdf -G0 -ta4 %d -o && ps2pdf14 %f && open -a Skim.app %s.pdf") ("^pdf$" "." "open -a Skim.app %o") ("^html?$" "." "open %o")))))" Makes sure that when you are not running pdflatex, ie. you are making dvi files when you try to view your output it is converted to ps and then to pdf.
'(LaTeX-command "latex -synctex=1") makes sure that the TeX file and pdf are syncronised so you can jump between them. This only works if you are using pdflatex.

The shortcut key for running latex on your tex file is "Control-c Control-c". In Emacs this is abreviated as C-c C-c. M is the meta key which on your keyboard should be "option" or "alt". 

To jump between your tex file and the pdf use "Shift"-"Command"-mouseclick. Use the same in Skim to jump from the pdf. This only works if you used pdflatex to make the pdf. But for a thesis you probably will since you can then use the hyperref package. The hyperref package is pretty cool, makes links within your pdf, so you can jump from your index to the section by clicking on the heading of the section in the index, and more.

1 comment: