Using Preview.app as a TeX/LaTeX previewer

Mar 19, '04 09:46:00AM

Contributed by: MattiHanninen

I like to use Preview.app for previewing my TeX/LaTeX documents (and MetaPost drawings for that matter). However, Preview.app is somewhat lacking as a TeX/LaTeX previewer. It doesn't support .dvi files, nor does it refresh the viewed document automatically when the underlying file changes. In fact, Preview.app doesn't refresh the viewed document even if you reopened it explicitly with the shell command open unless you closed the document in Preview.app first.

The following script converts a given file to PDF format and then opens it in Preview.app. If the document is already being viewed, the script uses GUI AppleScript (it is a shame Preview.app isn't scriptable) to close the viewed document first before opening the updated version.

#!/bin/sh
PDFFILE=`echo "$1" | sed -e 's/.[^.]*$/.pdf/'`
PDFBASENAME=`basename ${PDFFILE}`
case $1 in
  *.dvi)
    dvipdfm -o ${PDFFILE} $1
    ;;
  *.ps)
    pstopdf $1 -o ${PDFFILE}
    ;;
  *.pdf)
    ;;
  *)
    echo "Don't know how to handle $1" &>2
    exit 1
    ;;
esac
echo "
tell application \"System Events\"
 if exists process \"Preview\" then
  tell application \"Preview\" to activate
  tell process \"Preview\"
   tell menu bar 1
    # next three lines are ONE line; insert spaces in
    # place of returns!
    repeat with currentItem in (every menu item of menu
    \"Window\" of menu bar item \"Window\" whose name
    contains \"${PDFBASENAME}\")
     click currentItem
     click menu item \"Close\" of menu \"File\" of menu bar item \"File\"
    end repeat
   end tell
  end tell
 end if
end tell
" | osascript
open ${PDFFILE}
This script can be used directly from the command line, but I like to use it from Emacs. Assuming the script was saved as ~/Bin/mypreview, the following line needs to be added to tex-site.el to make the script available on AUCTeX:
(setq TeX-view-style '(("." "~/Bin/mypreview %d")))
Now, I just hit C-c C-c RET RET to generate the .dvi file and then C-c C-c RET RET again to view it without needing to close the previous version of the document in Preview.app first.

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040314102550515