Posts Tagged ‘latex’

This is apparently becoming a series on making various parts of my reference-managing toolchain play nicely together.  One of the great features of Mendeley is that it can automatically sync up your library with a BibTeX file.  One of the annoying things about Mendeley is that you can’t control what fields it puts in that file.  Why is this annoying?  Because apacite overloads the @article entry type to cover both journal articles and newspaper/magazine articles.  These have different APA citation formats, though: one should include the month of publication, while the other shouldn’t.  The only way to control which format is produced is to include or omit the month in the BibTeX entry.

At this point you might be wondering why I haven’t just switched to something a little more sensible for doing APA citations in LaTeX.  I probably will.  But in the mean time, here’s the solution I’ve cobbled together: monitor the library.bib file that Mendeley exports for changes, and then remove all the month lines.  The monitoring can be accomplished very easily on Mac OS X using launchd, and the removing is done with a shell script: 

Latexdiff is a script that produces a word-by-word diff of two versions of a latex file that can be compiled to mark up additions and deletions.  It generally works well for me except for one important caveat: it doesn’t recognize the citation command syntax from apacite, the package I use to get APA-style citations.  Luckily, being open-source, I could just fork the repository and fix it.

Latexdiff uses a latex-aware difference algorithm that treats latex commands as single “words”. The problem is that apacite allows citations of the form

\cite<text before citation>{AuthorYear}

Normally, a \cite command is only followed by arguments in […] or {…}. Latexdiff sees the < and thinks that the command is over and it can safely split things.  But this results in the \cite command being separated from its arguments in the diff which then refuses to compile.

The fix is easy and should be incorporated into the next version that’s released. But in the meantime, if you’re suffering as I was you can clone my fix:

git clone git@github.com:kleinschmidt/latexdiff.git
cd latexdiff
git checkout anglebracket-cite-command
ln -s latexdiff /usr/local/bin/latexdiff   # or wherever you like

If you know me at all you know that I love LaTeX.  It let’s you specify the content and logical structure of your document and takes care of making it look nice, including tricky mathematical expressions, yadda yadda yadda.

One thing that LaTeX is really bad at, though, is font support.  This isn’t a problem for the most part, and I’ve actually come to prefer the look of Computer Modern (the only real font ever created using Knuth’s Metafont language, and instantly recognizable to LaTeX geeks the world over).  But if you need to, say, typeset something in Arial (shudder), there isn’t exactly an easy way to do it, and god forbid that you might want to use a font that’s not freely available (like, just an arbitrary example, Times New Roman).

Enter our hero, XeTeX (and it’s big sibling, XeLaTeX).  XeLaTeX extends the low-level typesetting engine of TeX to use modern font/typography technology.  This includes support for Unicode, and super-slick typography conventions like OpenType and Apple Advanced Typography (AAT), which allow the typesetting of scripts with complicated rules for combining symbols (like Tibetan) and different writing directions (e.g. right-to-left scripts such as Arabic and Hebrew).

The really great thing about XeTeX, for my current purposes, is that it allows you to typeset almost anything using any font installed natively on your computer.  That is, XeTeX essentially adds that drop-down font-selection menu that every other text editor has.

As an example, I will show you how stupid-easy it is to typeset a document in 12 pt. Times New Roman (with 1-inch edges, not that it matters)

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{Times New Roman}

Yes, folks, it really is that easy: just two lines of code (above and beyond the usual documentclass/geometry combo), and nothing whatsoever that needs to be converted using FontForge, etc.  The only trick is that, instead of running latex, you run xelatex (which is easy to automate using emacs and AUCTeX).

As you can probably tell from the snippet above, the package you want to use is fontspec, which is the LaTeX interface for XeTeX’s font-specification system, and its documentation has lots of good examples.