Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Using Preview.app as a TeX/LaTeX previewer UNIX
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.
    •    
  • Currently 2.50 / 5
  You rated: 2 / 5 (4 votes cast)
 
[11,625 views]  

Using Preview.app as a TeX/LaTeX previewer | 8 comments | Create New Account
Click here to return to the 'Using Preview.app as a TeX/LaTeX previewer' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Using Preview.app as a TeX/LaTeX previewer
Authored by: _merlin on Mar 19, '04 11:30:05AM

Or you could use TeXShop or iTeXMac, which include nice previewers and syntax-aware TeX/LaTeX editors (you can find both of them on VersionTracker).



[ Reply to This | # ]
Using Preview.app as a TeX/LaTeX previewer
Authored by: frankxiv on Mar 19, '04 06:15:54PM

I agree. I was also going to suggest TeXShop. It really is a great program and it is free.

Later, Frank



[ Reply to This | # ]
Using Preview.app as a TeX/LaTeX previewer
Authored by: jmenard42 on Mar 19, '04 11:51:19AM

I have a small script that I use to turn a LaTeX file into PDF and open it in Preview.

[code]
#! /bin/sh
#
# Called from Emacs in tex-dvi-view-command (see .emacsrc.el).

pdflatex ${1%dvi}tex && open ${1%dvi}pdf
[/code]

In my .emacs file:

[code]
(setq tex-dvi-view-command "/Users/jimm/bin/latex-view '*'")
[/code]

When editing a .tex file, I first hit ^C-^F which runs tex-file and then hit ^C-^V which runs the script. The script runs pdflatex and then Preview.



[ Reply to This | # ]
Using Preview.app as a TeX/LaTeX previewer
Authored by: johnblommers on Mar 19, '04 01:09:18PM
"Too bad Preview.app cannot be scripted."

Indeed.

Acrobat Reader and Acrobat Professional cannot be scripted either.

Think about it. If you have an accursed DRM'd document that lets you view-only (no copy, no print, no save as), how are you going to exercise your fair use rights under the US Copyright Act? The DMCA took that away and lets content creators limit what you can do with your paid-for content.

And that is supported by both Adobe and by Apple and THAT is why Preview cannot be scripted.

BTW as an author of several books, I understand the viewpoint of the content creators. I understand it, I did not say I necessarily agree with it.

- John

[ Reply to This | # ]
Using Preview.app as a TeX/LaTeX previewer
Authored by: ByronEllis on Mar 19, '04 01:26:52PM

Um, what does DRM have to do with whether or not Preview.app should be scriptable?



[ Reply to This | # ]
Using Preview.app as a TeX/LaTeX previewer
Authored by: bynkii on Mar 21, '04 10:51:16AM

actually, Acrobat Professional is highly scriptable. It has both Javascript and AppleScript implementations. The AppleScript dictionary is so broken as to be useless, but it's there.

The Javascript implementation in Acrobat is top-flight though, and you can do more with Javascript than you can with the plugin APIs.

Acrobat is a production tool. For it to be completely unautomatable would be stupid.

Almost as stupid as Preview not being scriptable.

john



[ Reply to This | # ]
Using Preview.app as a TeX/LaTeX previewer
Authored by: Garin on Mar 19, '04 10:47:19PM

This is a nice little script, and I don't mean to detract from it..

However, some folks may not be aware of Gerben Wierda's i-installer package available at http://www.rna.nl/ii.html. This package installs an excellent version of LaTeX that comes out of the box fully configured for pdflatex which, you guessed it, generated PDF directly from regular latex source. This package, when combined with, say, TeXShop (http://www.uoregon.edu/~koch/texshop/) makes for some deadly typesetting action.

I'm an emacs guy, so that's what I use. The sufficiently motivated emacs hacker can easily customize a local tex-site.el file that adds a pdflatex item into the LaTeX menu, and something that hooks right into the "open" command to display the produced pdf from emacs.



[ Reply to This | # ]
Using Preview.app as a TeX/LaTeX previewer
Authored by: max_zorn on Mar 22, '04 09:03:14PM
Dear all, some other options, relying on fink:
  • install xpdf. Changing pages or hitting the "r" button will do a refresh!
  • work with gv, so latex your document, followed by dvips. I prefer this since gv is a beautiful viewer and for printing you go with postscript anyway...
Cheers, Max

[ Reply to This | # ]