[Tex/LaTex] the difference between unicode-math and mathspec

best practicesmathspecunicode-mathxetex

I'm currently typesetting my lecture notes for classical electrodynamics using the tufte-book class and XeLaTeX.

This is my first time using XeLaTeX and so I stumbled upon the two packages unicode-math and mathspec, when it came to include an OpenType math font (here TeX Gyre Pagelle Math).

I browsed the documentation of the two packages a little bit, but wasn't able to find out, which is the "better practice" to use.

Question: Should I use unicode-math or mathspec for my XeLaTeX documents?

Best Answer

(Please note that this answer has been revised to reflect issues raised in the comments.)

unicode-math

The unicode-math and mathspec packages have very different goals. The unicode-math package is designed to map math markup into unicode characters as supplied by real OpenType math fonts such as the Latin Modern Math, STIX, Asana Math. It also allows (as much as makes sense/is possible) Unicode input of math, as well as output so that you can enter unicode math characters in the source and have them be interpreted correctly as math by TeX.

For example, you could create the following document:

% !TEX TS-program = XeLaTeX

\documentclass[12pt]{article}
\usepackage{unicode-math}

\begin{document}

This is some math text entered with math in the source:
\[
∀X [ ∅ ∉ X ⇒ ∃f:X ⟶  ⋃ X\ ∀A ∈ X (f(A) ∈ A ) ]\]

This is some math text entered with regular markup

\[
\forall X [\emptyset \not\in X \Rightarrow \exists f:X \rightarrow  \bigcup X\ 
    \forall A \in X (f(A) \in A ) ]\]
\end{document}

which would produce

output of unicode-math

The unicode-math package is compatible with both XeTeX and LuaTeX.

mathspec

The mathspec package is not designed to allow you to use unicode characters in your source math input. Instead it is designed to allow you to use open-type fonts within math so that you can, for example, match the text font of your math with the text font in your document.

So for example you can use:

% !TEX TS-program = XeLaTeX

\documentclass[12pt]{article}
\usepackage{mathspec}
\setmainfont{Linux Libertine O}
\setmathfont(Digits,Latin)[Scale=MatchLowercase]{Linux Libertine O}


\begin{document}
This is some text in Libertine \emph{X(f(A))}
\[
\forall X [\emptyset \not\in X \Rightarrow \exists f:X \rightarrow  \bigcup X\ 
    \forall A \in X (f(A) \in A ) ]\]
\end{document}

which will produce:

mathspec output

The mathspec package can only be used with XeTeX; it cannot be used with LuaTeX.

Which should you use

If you want to use one of the available OpenType math fonts, then using unicode-math makes sense.

If you want to match a non-math font with the math font then mathspec can help, but because TeX does not apply kerning between characters from different fonts, using mathspec has its drawbacks, since you may find places in which the switch from the text font to the math font produces bad spacing, as in the example given.