TeX Live for iPhone

TeX Live is a common distribution of LaTeX, which is a system built around the TeX typesetting system by Donald Knuth (and others). It is well known for typesetting mathematics, such as {E} \psi(r) = - {\hbar^2 \over 2m} \nabla^2 \psi(r) + V(r) \psi(r), but it can also do almost any kind of typsetting you throw at it.

Installing via Cydia

The easiest way to install TeX Live for the iPhone is my Cydia repository. Be warned that TeX is huge. You will end up downloading 500 megabytes of data from my site, and after installation, it will take up a whole gigabyte or so. Make sure your device doesn’t go to sleep during installation.

To install TeX Live, you will need to add both my repo and the coredev.nl repo. Adding my repo is as simple as adding my URL in Cydia, but the coredev repo is a bit trickier. Install apt-get and wget in Cydia, then fire up a terminal (as root, and make sure Cydia is closed!) and run the following:

iphone:~ root# wget http://coredev.nl/cydia/coredev.pub
iphone:~ root# apt-key add coredev.pub
iphone:~ root# echo 'deb http://coredev.nl/cydia iphone main' > /etc/apt/sources.list.d/coredev.nl.list
iphone:~ root# apt-get update

Once you’ve added them, a section “TeX Live” should show up in Cydia, with a package “TeX Live” that you can install.

Compiling it Yourself

If you want to compile it yourself, you will need an on-iPhone development environment. Also you will need a copy of XZ Utils. You can either build it yourself or get it from my Cydia repository.

Luckily, the TeX Live source includes all the libraries it needs, so you don’t have many dependencies to go through. Using these libraries is easy, but they link statically so the final TeX binaries are much bigger. This works right now; my next goal is using system libraries instead.

The TeX Live build is long: make sure your device won’t go into sleep mode, or otherwise interrupt the build. TeX Live is mighty picky about build order, sometimes, so an interruption could be bad. It might not be, but it’s best not to risk it.

As always, if you have any questions, feel free to contact me.

The Easy Way

Here, I’m using the term “easy” lightly. There’s still a lot of steps, even with a patch file. Again, be sure your device won’t fall asleep on you!

Installing Build Tools

We need to add the coredev.nl APT repository so we can download perl. The following commands will add the source and its public key. Note that we use su to become root for a bit; the password asked for is root‘s.

iphone:~ mobile$ wget http://coredev.nl/cydia/coredev.pub
iphone:~ mobile$ su
iphone:/var/mobile root# apt-key add coredev.pub
iphone:/var/mobile root# echo 'deb http://coredev.nl/cydia iphone main' > /etc/apt/sources.list.d/coredev.nl.list
iphone:/var/mobile root# apt-get update
iphone:/var/mobile root# exit

Now, we can install the dependencies we need. perl is broken into a ton of small packages, so don’t be alarmed when APT tells you it’s installing about 100 things.

iphone:~ mobile$ sudo apt-get install perl bison flex m4

(We need to install m4 manually, even though it should be included as a dependency of either bison or flex.)

The iPhone OS limits the number of processes that a single user can run to some small number. Normally, this isn’t a problem, even when building things like Emacs. However, the TeX Live build system is massive, and you’ll quickly run up against this limit. I’ve written a little script that launches a subshell where this limit is much higher (64, in fact), so you can complete the TeX build.

#!/bin/bash
(whoami | grep --quiet root) || sudo $0
(whoami | grep --quiet root) || exit
sysctl -w kern.maxprocperuid=256 > /dev/null || exit
ulimit -u 64 > /dev/null || exit
echo Entering supermode...
sudo -i -u mobile
echo Leaving supermode...

To use, just save this as supermode in your home directory, and chmod +x supermode. Then, whenever you need the extra processes, use the command ~/supermode, type in your password if it asks, and a new shell will launch. To leave, just use exit. For the rest of this guide, I’ll assume you’re in “supermode”.

Fetching, Installing, and Building

Now we can fetch the TeX Live sources and the patch.

iphone:~ mobile$ wget ftp://tug.org/historic/systems/texlive/2009/texlive-20091107-source.tar.xz
iphone:~ mobile$ wget http://gammalevel.com/forever/texlive-20091107-iPad.patch

Make sure the XZ Utils are installed, then extract the sources and apply the patch. Try to free up as much memory as possible before this step; the extraction can take a while, and the file is large.

iphone:~ mobile$ xzcat texlive-20091107-source.tar.xz | tar xv
iphone:~ mobile$ cd texlive-20091107-source
iphone:~/texlive-20091107-source mobile$ patch -p1 < ../texlive-20091107-iPad.patch

Now, we’ll create a seperate build directory, and configure TeX Live. Make sure to include every part of this massive list of configure options, and note that pwd is inside back-ticks (the un-shifted version of ~ on most keyboards). In fact, it’d probably be best if you just copy and paste that last command…

iphone:~/texlive-20091107-source mobile$ mkdir ../texlive-build
iphone:~/texlive-20091107-source mobile$ cd ../texlive-build
iphone:~/texlive-build mobile$ ../texlive-20091107-source/configure --datadir=`pwd` --prefix=/usr --with-tex-banner="TeX Live 2009/iPhone OS [gammalevel.com]" --disable-xetex --disable-shared --disable-missing --disable-largefile --disable-threads --without-x --disable-native-texlive-build --disable-multiplatform --disable-xdvipdfmx

Once TeX Live is done configuring itself, we’re ready to build and install. This part can take a while; go get some coffee.

iphone:~/texlive-build mobile$ make texmf=${TEXMF_PATH:-/usr/share/texmf}
iphone:~/texlive-build mobile$ sudo make texmf=${TEXMF_PATH:-/usr/share/texmf} install-strip

Congratulations! You’ve just installed TeX Live for iPhone! However, it’s not usable yet…

Installing the texmf Tree

First of all, TeX Live just installed a useless baby texmf tree already, and into the wrong place no less, so let’s remove that:

iphone:~/texlive-build mobile$ cd ../
iphone:~ mobile$ sudo rm -rf /usr/texmf /usr/texmf-dist

Now, you need to download, extract, and install the texmf tree. This file is gigantic: around 900MB! This part will probably take even longer than the compile, so if you want, you could download it on a computer, then move it to your device.

iphone:~ mobile$ wget ftp://tug.org/historic/systems/texlive/2009/texlive-20091107-texmf.tar.xz

(If the download is cancelled somehow, just run the wget command again with -c to ensure it will continue downloading where it left off.)

At this point, you really should make sure the file is correct:

iphone:~ mobile$ sha256sum texlive-20091107-texmf.tar.xz
63e38a218c04e28081e12b4f48969a7e25cf5f81a8c4807aae815a49b575ae5d  texlive-20091107-texmf.tar.xz

Now we can continue with extraction and installation:

iphone:~ mobile$ xzcat texlive-20091107-texmf.tar.xz | tar xv
iphone:~ mobile$ sudo mv texlive-20091107-texmf/texmf* /usr/share/

Finishing Up

The texmf.cnf we installed has the wrong paths, so we’ll replace it:

iphone:~ mobile$ wget http://gammalevel.com/forever/texlive-20091107-texmf.cnf
iphone:~ mobile$ sudo mv texlive-20091107-texmf.cnf /usr/share/texmf/web2c/texmf.cnf

Now, we need to tell TeX Live to reconfigure itself after the install:

iphone:~ mobile$ sudo mktexlsr
iphone:~ mobile$ sudo texlinks
iphone:~ mobile$ sudo texconfig-sys init

(The last command will exit with an error, complaining about failed format builds for xetex: these are normal, because we disabled it! If there’s any other errors, though, those are real problems.)

Congratulations! You have just finished installing TeX Live! To configure your new system (for example, to set the default paper size), use this command (though honestly, it’s never worked for me):

iphone:~ mobile$ texconfig

The Hard Way

I’m still backtracking through what I did to find out all these patches and configure options, but when I figure it all out again, I’ll put why I made those changes here.