LaTeX on the iPad

March 29, 2012

It’s now possible to set up a functional system for editing and viewing LaTeX documents on the iPad for under $15 and a few minutes of shell scripting. All the necessary tools are readily available: TeX Touch as the LaTeX editor, Good Reader as the PDF viewer,1 and Dropbox as the synchronization backend. All that remains is a remote compile server. For that, A simple bash script called from a cron job or executed in a loop on your main computer will work nicely.

The Editor

TeX Touch seems to be the best LaTeX editor currently available for the iPad. It has syntax highlighting, quick access to common LaTeX commands and symbols, support for Dropbox, and provides for two remote compilation methods. A “TeX Cloud” service is provided by the author, or you can provide your own compile service which must be able to detect changed files in your Dropbox account and recompile them as needed. The second approach is of course more flexible, if you have the ability to set this up yourself. A diagram overview of this approach is available on the TeX Touch homepage. A couple of example shell scripts are offered below.

The Compile Server

If you have a primary computer with a Unix-like operating system that’s usually connected to the internet and has access to your Dropbox account, then you can write a simple shell script to handle compiling your documents. This can be as simple or as complex as you need.

Consider this simple script that runs in an infinite loop and, every five seconds, recompiles any documents for which the .tex file has changed:

#!/bin/bash

TEX_PATH="$HOME/Dropbox/latexbot"
SLEEP_TIME=5
TIMESTAMP_FILE="$HOME/.latexbot-timestamp"
LATEX="pdflatex -interaction=nonstopmode"

cd "$TEX_PATH"
while true; do
    for file in *.tex; do
        if [ "$file" -nt "$TIMESTAMP_FILE" ]; then
             $LATEX "$file"
        fi
     done
     touch "$TIMESTAMP_FILE"
     sleep $SLEEP_TIME
done

Of course, you’ll have to kill it manually if you ever want to stop it. This should work fine for cases where all of your documents are single .tex files, for example, academic articles, lecture notes, resumes, and so on.

If you have more complex multi-file projects, or if you want to compile the files in place (instead of collecting them in a single latexbot directory), then the following script might work better. It reads a file, $HOME/.latexbot, which consists of a list of paths to directories and/or single .tex files. When it encounters a directory, it processes any .tex files in that directory that have changed since the last run. When it encounters a filename, it assumes this is the master file for a LaTeX project contained in the same directory. In this case, it will check to see if any of the .tex files in the directory have changed. If so, then it runs LaTeX on the master file only. This second case is useful for a large project like a book, thesis, or dissertation, where you might want to \include or \input the individual chapter files.

#!/bin/bash

PATH_FILE="$HOME/.latexbot"
TIMESTAMP_FILE="$HOME/.latexbot-timestamp"
LATEX="pdflatex -interaction=nonstopmode"

while read path; do
    if [ -d "$path" ]; then
        cd $path
        for file in *.tex; do
            if [ "$file" -nt $TIMESTAMP_FILE ]; then
                $LATEX "$file"
            fi
        done
    elif [ -f "$path" ]; then
        dir=$(dirname $path)
        file=$(basename $path)
        cd $dir
        for other in *.tex; do
            if [ "$other" -nt $TIMESTAMP_FILE ]; then
                $LATEX "$file"
                break
            fi
        done
    fi
done < $PATH_FILE
touch $TIMESTAMP_FILE

Notice that both scripts have some configurable variables at the top. You can change the location of the path file or timestamp file by setting PATH_FILE or TIMESTAMP_FILE accordingly. If you need to pass different command-line options to LaTeX, change the command stored in the LATEX variable. The path file should contain absolute paths to your projects, like so:

/home/username/Dropbox/latexbot/
/home/username/Dropbox/book/main.tex

The first line instructs the script to watch all .tex files in the latexbot directory. The second line contains the path to a master .tex file for a project in the book directory.

Note that this script does not run in a loop like the previous one. It’s straightforward to make it do so, but as it stands it should be called periodically from cron (or some other scheduler) like so:

# min hr dom mon dow  command
  */1  *   *   *   *  /path/to/latexbot

If you don’t have a Linux or OS X machine with a consistent internet connection, a small Linode server with the Linux Dropbox client and a TeX Live installation would also work well.

The Viewer

Good Reader is an excellent PDF viewer for the iPad which also has builtin support for almost every type of file server imaginable. You can transfer files to and from services such as SFTP, Dropbox, WebDav, FTP, IMAP, SugarSync, and many more. Of particular relevance to our iPad LaTeX system, Good Reader allows you to syncronize individual files (bi-directional or read-only), keeping them updated if they change. So, you can ask it to sync the PDF file that gets produced for each of your LaTeX projects. After this sync is configured, each time you finish editing a file (and once the compile server has finished processing it), you only have to click the sync button to fetch the latest version. Only files that have been updated will be downloaded, saving time and bandwidth.

Putting it Together

In practice, first you’ll run TeX Touch and open a LaTeX file from your Dropbox account. After editing the file you’ll either use the built-in compilation interface or save the file and upload it back to your Dropbox account. Either way, once Dropbox updates the file back on your compile server, your script will re-compile the document and produce an updated PDF file. Then, you can open Good Reader, press sync, and read your new document.


  1. TeX Touch includes a basic PDF viewer, but I prefer Good Reader for serious reading.