Today marks the end of a longterm project for us as the 1.1 update of Texpad makes it to the iPad App Store with the ability to typeset LaTeX onboard the iPad. Having written a blogpost some time ago about our first failed attempt I will write an update with how we did it.
The secret this time around was KerTeX, a little known TeX distribution being developed by Thierry Laronde. It is unencumbered by GPLed code and it does not use TeXLive’s atrocious Kpathsea library. Consequently it is faster, smaller, embeddable and the distribution’s directory structure is much simpler. The build process is quirky, but clear as it carefully separates building the WEB-to-C translator from building TeX itself from the translated WEB source, which makes it very easy to extract the relevant C files and build them into an iOS binary.
For those out of the loop, TeX was written in the WEB literate programming language, in which code consists of a mix of documentation and Pascal on equal footing. The Tangle executable extracts the Pascal and translates it to C ready for compilation, whilst the Weave executable creates TeX files from the embedded documentation. In order to build TeX into iOS you need to build Tangle for the desktop, translate TeX’s WEB source to C, then build those C files for iOS. Unfortunately, a few tweaks were necessary to the C source to make TeX play nicely with iOS. You can imagine the state of C code automatically generated from Pascal which was written in an era when it seems that all arguments were passed to functions as global variables, hence these very minor alterations took quite some time. Nevertheless, once this was all hidden behind an Objective-C interface, we at last had LaTeX running natively on iOS.
The fly in the ointment for any iPad TeX is that the full TexLive 2012 distribution takes up approximately 4GB. No one will thank us for an iPad app that size, so although we are expanding Texpad bundled packages in coming updates, it will always lack at least some of TexLive’s packages and abilities. For this reason we will keep our free cloud typesetting servers running as a core part of the product because without the ability to use all of LaTeX’s features, Texpad would be incomplete.
An exciting opportunity created by this project is that we now have TeX in C, ready to be refactored into a readable and maintainable form. Although WEB was a great piece of work in its time, and it was the predecessor of all source code documentation, both WEB, and to a lesser extent Pascal, have long since fallen into disuse. This, combined with Knuth’s resistance to any alterations to the TeX source, has stunted TeX’s development and forced all work out into external binaries. The problem with this from an iOS point of view is that no iOS app can launch an external binary, but on all platforms it complicates development of TeX and leads to the bloated and slow distributions we have become used to. For example, in the average typeset TeX itself will have to fork to grab search paths from the Kpathsea system, then with the output in the archaic DVI format the dvipdfmx
executable is necessary to create a PDF. Although XeTeX and pdfTeX skip the DVI step, they are still weighed down by kpathsea, and their development is hampered by them being written in WEB. By refactoring the translated C code into a modern, readable and maintainable form, we will end up with not just a faster TeX, but one that is entirely compatible with all of TeX’s ecosystem and a great starting point for future development of TeX itself.
As such we have begun work on refactoring TeX’s translated C sources into C++, dropping the string pool and global variables along the way. Right now we are just getting going, but once the project has reached a certain level of maturity we will of course open up the repository on Github. Meanwhile if you are looking for a LaTeX editor for either iPad or your macbook, check out Texpad.