How I do LaTeX on Drupal

2 minute read

Some people wanted to know what I am using to enter my LaTeX code into Drupal. There are several Drupal modules that can deal with some LaTeX, but none of them really suited my needs.

The most important advantage of LaTeX blogging is that I’m able to use my reference database seamlessly in my blog with BibTeX. Another is that I can generate PDFs of my posts from the same source as the blog (believe it or not, I get a lot of PDF requests). Slightly less important, I can repurpose text more easily. DruTeX and other Drupal-centric solutions are especially good if you want to use LaTeX code as a Markdown-like shorthand instead of HTML, but don’t fill my requirements.

To generate HTML from LaTeX, I depend on Tex4ht. I also use this to transfer LaTeX to RTF for publishing articles and my textbook, and it does an excellent job for most purposes.

No matter your destination format, TeX4ht always seems to take some tweaks to get everything converted. The strict XHTML produced by Tex4ht works fine in a browser, but it contains a number of elements that stymie Drupal’s “Full HTML” filter, including lots of HTML comments and superfluous newline characters (Exactly why “Full HTML” doesn’t mean full HTML, I don’t understand.) I wrote a short Python script that strips these things out. At the moment it’s just a hack, but it works.

Styling: For whatever reason, TeX4ht doesn’t like the simple bold and italic tags of standard HTML and instead applies styles with SPAN tags and custom classes like “cmbx-10,” for the LaTeX-native Computer Modern fonts. It’s simple enough to include these classes in your CSS file, and apply whatever styling you like. If you really don’t like it, you could always rig up a custom config file for TeX4ht.

Math: Math goes to PNG pictures by default under TeX4ht; I keep all graphics in a common directory on my server. Copying the PNG files, and updating references in the HTML source (again, that Python script), is all it takes for equations to come out right.

Hyperlinks: The hyperref package for LaTeX does a nice job with links and internal references (including bibliography references). Links come out the same whether through Drupal or PDF.

So, that’s easy enough, but probably not recommended unless you’re ready to do a little scripting. UPDATE (2008/08/24): A reader writes:

Not sure which Drupal version you have (it might not be in 4.x), but if you go to Administer -> Input Formats; and then click on 'configure' near 'Full HTML' you can disable the HTML filter and line break filter altogether and it might remove the necessity for your intermediate step.
That will probably do the trick for a lot of people.