Tuesday, February 16, 2010

Matching fonts in Keynote and LaTeX

LaTeX is great if you want to typeset documents with lots of mathematics in them, but what if you want to make presentations with lots of math? Sure, there are LaTeX packages like Beamer with which you can straightforwardly make decent looking presentations using nothing but LaTeX. But none of those packages lets you easily control where items end up on a page, make subtle changes to overall lay-out, or add some motion to your slides.

Keynote does all of those things, and a bit more. Here's for example a slide of presentation I gave some time ago:


The things in the image are all created within Keynote (not in a separate drawing program like Adobe Illustrator), and some of them are even animated. This is simply impossible to do solely with LaTeX, and that's why I've been using Keynote for quite some time now for all my presentations.

However, there was always something annoying me: the fonts in formulae I got from LaTeXiT (a small program that lets you insert LaTeX formula in Keynote) didn't match the font over the overall presentation. And if you're a typesetting nerd like me, that's pretty annoying. Luckily, I found a solution. Here's an example with both type of fonts:


The difference is subtle, but certainly noticable. The greek letters haven't changed, but the others are in the same font in the second equation. Here is how you change fonts:

  1. First, go to the LaTeXiT preferences, and change the default configuration to use xelatex instead of pdflatex:

  2. Next, add the following to the LaTeXiT preamble:

    \usepackage{mathspec}
    \usepackage{xunicode}
    \usepackage{xltxtra}
    \setmainfont{Gill Sans}
    \setmathsfont(Digits,Latin,Greek){Gill Sans}


    Be sure to change the "Gill Sans" to match whatever font you're using in Keynote.

And that's it! After restarting LaTeXiT, all new equations you'll typeset will be in the correct font. Enjoy!

13 comments:

  1. Unfortunately, this does not work for me. Attempting to compile a simple example mixing text and math with the given preamble yields this error:

    /var/folders/2P/2PpdJWqZG201SQ2gCx9hSk+++TI/-Tmp-/LaTeXiT-2.0.0/latexit-1.tex:1
    3: LaTeX Error: Missing \begin{document}.

    See the LaTeX manual or LaTeX Companion for explanation.
    Type H for immediate help.
    ...

    l.13 \setmathsfont(D
    igits,Latin,Greek){Gill Sans}
    No file latexit-1.aux.
    [1] (./latexit-1.aux) )
    (see the transcript file for additional information)
    Output written on latexit-1.pdf (1 page).
    Transcript written on latexit-1.log.

    It appears that it gacks on the setmathsfont command, which is confirmed by a test in which I commented out the setmathsfont command, leaving the rest the same. This compiled without error, yielding output in which the text was in Gill Sans but math was in my default math font.

    ReplyDelete
  2. Had to add \documentclass[10pt]{article} at the beginning of the proposed preamble, now it works. Thx!

    ReplyDelete
  3. Brilliant dude! I too am writing a Physics PhD thesis and experiencing font hell between CorelDRAW on Windoze and TexShop my beloved Mac.

    With your explanation, I can now have math in Arial from LaTeXiT, and can export as PDF to CorelDRAW.

    Blessings!

    ReplyDelete
  4. Mmmm, I'm still not 100% happy with hbar, but things are looking a lot better now that previously!

    This page may help someone re hbar:

    http://www.fileformat.info/info/unicode/char/210f/fontsupport.htm

    ReplyDelete
  5. Maybe an additional suggestion: If you have individual symbols (e.g. a greek alpha) in your Keynote slide, you can use UnicodeIt for the symbol in combination with LatexIt for the equation. The unicode text will pick up the styling of the rest of the document and will nicely flow with the text.

    Here is the link: http://www.svenkreiss.com/UnicodeIt
    There is also an online version: http://www.unicodeit.net

    ReplyDelete
  6. brilliant. used it to generate equations for a nature paper that requires arial.

    ReplyDelete
  7. Awesome! I used this tip for putting formulae into a Pages document, but the principle is the same. The main problem I've got now is that LaTeX doesn't seem to know how to align things naturally using Baskerville – superscripts sometimes overlap other parts of the formula, for example. I've fixed this by adding in a bit of space where needed, but the important thing is that my formulae now look the same as the body of text... Yay!

    ReplyDelete
  8. Thanks a lot for taking the time to write this down.

    ReplyDelete
  9. I used Gotham Book. There was no problem when I compiled text. When I compiled an equation, there are numbers (xxxxx.0000bp,xxxx.0000bp,...) that show up before the actual equation. I can probably crop it but is there a way to avoid it?

    ReplyDelete
  10. Spacing. Spacing seems to be a problem for any accents, superscripts, etc. Regardless of the font choice, the spacing seems to be shifted to the left for all 'extra' mathematical markings whenever I use your above approach. Do have any work arounds for this? (other than using \, before every accent) Thanks!

    ReplyDelete
  11. I actually find the original appearance more pleasing. It's totally fine to have math in a different font than text. Probably inline math is the problem.

    ReplyDelete

Note: Only a member of this blog may post a comment.