17

I’m writing in LaTeX on a macOS machine, while using the preinstalled Preview.app for viewing the compiled document. After the LaTeX file is recompiled, Preview.app:

  1. does not refresh the PDF automatically, and
  2. refreshes when I click its window, but jumps to the top of the document.

I’m wondering whether there is a way to configure it so that Preview.app:

  1. refreshes automatically when the underlying PDF file changes, and
  2. after refreshing, stays on the same page.
ib.
  • 27,830
  • 11
  • 80
  • 100
Chitu Mazhong
  • 187
  • 1
  • 4
  • 1
    This question should better be directed to **["Ask Different" (Apple/OSX StackExchange)](http://apple.stackexchange.com/)**. – Kurt Pfeifle Feb 04 '15 at 09:39

4 Answers4

14

I have been annoyed with this myself and, unfortunately, I only have a “hacked” solution for you.

You can view the PDF in the Single Page mode (activated by the +2 shortcut) to stay on the same page when the file is updated.

I hope someone posts a permanent solution that works in the Continuous Scroll mode.

ib.
  • 27,830
  • 11
  • 80
  • 100
Sigve Karolius
  • 1,356
  • 10
  • 26
10

I believe, there are a couple of options.

  1. Install Skim PDF viewer, as has been suggested in the answers to a similar question on TeX StackExchange.

  2. Put together something yourself using the fswatch command, which you can install via Homebrew to provide something akin to inotify on Linux, and make your script monitor the PDF file, so that when it is modified, some AppleScript triggers a refresh in Preview.

It would look like so:

$ fswatch -iI ./*.pdf | xargs -I{} ./RefreshPreview {}

where the RefreshPreview script would be

#!/usr/bin/env bash
osascript -e 'tell application "Preview" to quit'
sleep 1
open -a Preview "$1"

If you don’t want Preview to come forwards and get the focus, use the following, instead:

#!/usr/bin/env bash
open -g -a Preview "$1"

Do not forget to make the script executable by running the command

$ chmod +x RefreshPreview

I just found that a somewhat simpler and more elegant script will work even better:

#!/usr/bin/env bash
osascript<<EOF
tell application "Preview" to open POSIX file "$1"
EOF
ib.
  • 27,830
  • 11
  • 80
  • 100
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

I have found a solution that works in the Continuous Scroll mode in Preview. I have used BetterTouchTool to chain a bunch of commands and key presses that are activated one after another when I press a certain shortcut in my LaTeX editor, LyX.

Here is a screenshot of the BetterTouchTool window. (I have configured F5 to be the trigger shortcut for this sequence of actions.) Screenshot

The chain consists of the following actions:

  1. Trigger the ++R keypress, which invokes the Update action in LyX.

    If you use a different LaTeX editor, replace it with the corresponding shortcut that performs a re-compilation of the LaTeX sources.

  2. Wait for half a second.

    This allows the Update action in LyX to complete. You might increase or decrease the delay time depending on the circumstances.

  3. Run the AppleScript command tell application "Preview" to activate.

    This switches to Preview and makes it the active application. At this moment preview re-reads the file and reloads the view.

  4. Trigger the +2 keypress.

  5. Trigger the +1 keypress.

  6. Run the AppleScript command tell application "LyX" to activate.

    This returns the control to LyX.

ib.
  • 27,830
  • 11
  • 80
  • 100
Vicky
  • 11
  • 2
1

This is not an answer for Preview, but I found that Skim can do this by changing a couple settings (from the Skim help):

Skim can automatically reload files you view when their contents changes on disk, by another program.

To reload files when their contents changes on disk,

  • choose Skim > Preferences,
  • click Sync, and
  • select "Check for file changes".

Skim will always first ask you whether it should reload the file when the contents changes on disk, unless you have chosen Auto.

Then check the box for "Reload automatically". Restart Skim, and the file should be updated automatically as soon as you build it.

Skim > Preferences > Sync

Community
  • 1
  • 1
John
  • 1,335
  • 12
  • 17