Monday, April 23, 2012

How to Easily Use OpenType Fonts in LaTeX

I became interested in LaTeX out of a desire to be able to produce high-quality PDFs for self-published books. Someday I hope to be able to produce books of comparable quality to these humanities books typeset in TeX. This idea became even more feasible when I discovered the text content could be written in Markdown and converted to LaTeX with pandoc (More information in this article).

Typographically, the example books I linked to above are more the exception than the rule: the vast majority of LaTeX documents use the same boring default font, Computer Modern, that was originally packaged with the software in the 1980s. Using Computer Modern in a self-published book would be almost as bad as using Times New Roman or Arial.

If you try to figure out whether and how you might be able to use your computer’s normal fonts with LaTeX, you will soon come across a lot of extremely complicated and incomplete documentation about how to convert TrueType or OpenType fonts into a format LaTeX can use.

The happy truth is that these instructions are now obsolete: you now have easy access to OpenType fonts on Windows and Mac platforms, thanks to a new version of LaTeX called XeTeX. XeTeX includes a package called fontspec that gives full access to all system fonts, as well as advanced features for OpenType fonts, such as ligatures and small caps. XeTeX is available for Mac, but what most people don’t say is that this font-accessing goodness can also be used on Windows since XeTeX is included with Windows distributions such as TeX Live and MikTeX.

That being understood, here’s how to use your system fonts in your TeX documents (source):

  1. Use the xelatex command in place of pdflatex
  2. Add \usepackage{xltxtra} at the beginning of your preamble (some XeTeX goodies, in particular it also loads fontspec, which is needed for font selection).
  3. Add \setmainfont{Name of OTF font} in the preamble.
  4. No step 4.

Note: If you are using the aforementioned pandoc to generate your TeX documents, you do not need to do step 2 — pandoc already includes the fontspec package in its default template. Also, you can set the main font by adding the option --variable=mainfont:"font name" when calling the pandoc command.


soramimi said...

Wow, awesome! Just tried this out and it worked quite very well. Thanks!

James Plant said...

Worked! Thanks!