Skip to content

Instantly share code, notes, and snippets.

@rldotai
Last active November 23, 2020 20:59
Show Gist options
  • Save rldotai/6583064acfe20932d077 to your computer and use it in GitHub Desktop.
Save rldotai/6583064acfe20932d077 to your computer and use it in GitHub Desktop.
A makefile for LaTeX and Skim on OS X

We have, somehow (and against all odds) installed TeX and the relevant packages for compiling PDFs, but the process of compiling a file while we make changes has become tedious, especially because of Preview's fondness for resetting to the beginning of the document every time a file changes.

Fortunately, Skim is better suited to this sort of revision-intensive work, so we can use that instead.

Getting it to actually reload every time you recompile is also kinda tedious, but it is possible if you're using OSX because you have AppleScript. If you're using Linux, it's pretty easy to just do what you want using Okular. If you're using Windows, well, my condolences. But at least you indirectly helping to end malaria!

Assumptions

  • Using OSX
  • pdflatex is installed on your system, and your documents compile correctly as is
  • Have make available
  • AppleScript is available
  • Skim is installed and functional

Directory Structure

Your directory is laid out like

.
| master.tex
| bibliography.bib
| Makefile
| (whatever else is in your directory)

Usage

It's fairly straightforward:

make

Depending on your editor, you can make this a hotkey to handle this without having to switch over to a Terminal.

PDFLATEX = pdflatex -interaction=batchmode -synctex=1
SH = /bin/bash
ASCRIPT = /usr/bin/osascript
SOURCE = master.tex
BASE = "$(basename $(SOURCE))"
default : pdf view
.PHONY: pdf graphics
pdf: $(SOURCE)
# run pdflatex and bibtex multiple times to get references right
$(PDFLATEX) $(BASE) && bibtex $(BASE) && $(PDFLATEX) $(BASE) && $(PDFLATEX) $(BASE)
.PHONY: view
view:
# reload the document in Skim
$(SH) skim-view.sh $(BASE)
.PHONY: clean
clean :
# remove all TeX-generated files in your local directory
$(RM) -f -- *.aux *.bak *.bbl *.blg *.log *.out *.toc *.tdo _region.*
#!/bin/bash
# modified from the version available at http://sourceforge.net/p/skim-app/wiki/TeX_and_PDF_Synchronization/
# so it just does the viewing part, and also returns focus to the previous window after reloading Skim.
# the first argument should be the tex file, either with or without extension
file="$1"
[ "${file:0:1}" == "/" ] || file="${PWD}/${file}"
pdffile="${file%.tex}.pdf"
/usr/bin/osascript << EOF
# get the current frontmost window
tell application "System Events"
set frontmostApplicationName to name of 1st process whose frontmost is true
end tell
# reload the PDF file in Skim
set theFile to POSIX file "${pdffile}" as alias
tell application "Skim"
activate
set theDocs to get documents whose path is (get POSIX path of theFile)
if (count of theDocs) > 0 then revert theDocs
open theFile
end tell
# Reactivate frontmost window
tell application frontmostApplicationName
activate
end tell
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment