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?

up vote 59 down vote accepted

(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.

  • 3
    @HenriMenke Yes, that's true. mathspec is not compatible with LuaTeX. (The extra \setmathfont was a typo). – Alan Munn Jun 8 '13 at 14:33
  • 5
    (+1) You could use ⋃ (U+22C3) instead of \bigcup to have an a no-markup example. – David Carlisle Jun 8 '13 at 14:47
  • 2
    @DavidCarlisle Thanks. I've fixed the example, but it reinforces the point about how painful it can be to enter unicode source: I was didn't immediately find that character with my character viewer; also in the source the n-ary union is almost indistinguishable from the regular union character, which is kind of defeats the purpose of having more readable input. – Alan Munn Jun 8 '13 at 15:29
  • 2
    @AlanMunn There is no need to use Unicode input with unicode-math; it works perfectly with traditional input. – egreg Jun 8 '13 at 17:09
  • 2
    I don’t think the description of unicode-math is right, the Unicode input is merely a side effect of the main goal; supporting Unicode math fonts, and you can still use csnames for math inout, which is actually what most people do. – Khaled Hosny Jun 8 '13 at 17:27

mathspec is a clever attempt to make it possible to use math in XeLaTeX documents, with system fonts for the letters and symbols from standard math fonts.

With unicode-math one can basically use only specially tailored OpenType math fonts such as Latin Modern Math, TeX Gyre Termes Math, TeX Gyre Pagella Math, XITS Math, Asana Math (among the free ones) or Cambria Math and Lucida Bright Math (not free).

mathspec

With this package you can do limited math with the main system font. Just a silly example:

\documentclass{article}
\usepackage{mathspec}
\setmainfont[Ligatures=TeX]{Old Standard}
\setmathsfont(Digits,Latin,Greek){Old Standard}
\begin{document}
Some text and a formula $a+b=\int_{\xi}^{\theta} f(x)\,dx$.
\end{document}

enter image description here

The emulated math is not bad, and the package has features for manually correcting the possible bad spacings, which happen because the fonts have really no support for math. For instance, inputting the formula as

$a+b=\int_{\xi}^{\theta} "f(x)\,dx$

would give the better result

enter image description here

The " means ”add some space on both sides of the following letter”.

Digits, Latin and Greek letters can be taken from different fonts.

unicode-math

The unicode-math is compatible both with XeLaTeX and LuaLaTeX. Here's the same example with Lucida Bright:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont[Ligatures=TeX,Scale=0.85]{Lucida Bright OT}
\setmathfont[Scale=0.85]{Lucida Bright Math OT}
\begin{document}
Some text and a formula $a+b=\int_{\xi}^{\theta} f(x)\,dx$.
\end{document}

enter image description here

Spacing is correct without manual intervention, because the font used for math is a real math font. Here's the same with TeX Gyre Pagella:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont[Ligatures=TeX]{TeX Gyre Pagella}
\setmathfont{TeX Gyre Pagella Math}
\begin{document}
Some text and a formula $a+b=\int_{\xi}^{\theta} f(x)\,dx$.
\end{document}

enter image description here

Unicode input

One can input math with Unicode symbols, but this is not mandatory. It's possible both with mathspec and unicode-math. For instance, the integral with mathspec could be input as

$∫_ξ^θ "f(x)\,dx$

provided that is made known to the environment, for instance with

\usepackage{newunicodechar}
\newunicodechar{∫}{\int}

Some symbols (in particular letters) are already known.

Nothing particular is needed for inputting the integral as

$∫_ξ^θ f(x)\,dx$

with unicode-math. However the traditional syntax is understood by the package.

  • Great answer, thanks! It is very helpful to know, how to define additional unicode symbols as math operators. – Henri Menke Jun 8 '13 at 18:42
  • I would not call it 'clever' tbh. that's a standard feature of some programming language. to not have it by default in a language dedicated to typesetting is laughable – nicolas Apr 25 '16 at 7:20
  • @egreg Is it possible to somehow use sequences of unicode symbols without having to load unicode-math? I am thinking in particular about mapping ⁻¹ to ^{-1}. – Gaussler May 24 '16 at 8:35
  • @Gaussler It may be possible; whether it's convenient is another matter – egreg May 24 '16 at 8:49
  • Is it better to use TeX-Gyre Pagella Math, NewPX, Neo-Euler or Asana?. I'm looking for some opentype font with nice math, and with text looking like Palatino (because of university restrictions). – skan Nov 25 '16 at 13:45

unicode-math is mainly concerned about facilitating the use of using Unicode math fonts, namely OpenType math fonts in LaTeX. While mathspec is about allowing to take math alphabets from text fonts in the absence of matching math font.

Unicode defines a large set of math symbols and many fonts include them, but proper math typesetting requires many font parameters to aid the typesetting engine and those have to be provided by the font. Traditionally TeX fonts would provide the needed parameters in the font dimension of TFM files, which meant that non-TFM fonts can not be used to typeset math in XeTeX (and later LuaTeX). When Microsoft started adding TeX-like math typesetting in Word, they extended OpenType with a special MATH table to hold the needed parameters for math typesetting and XeTeX implemented it (and later LuaTeX) and unicode-math was written to provide macro support for using the new fonts. So with unicode-math you get proper math typesetting with proper OpenType math fonts, like TeX Gyre Pagelle Math, but you are limited to those specially prepared fonts.

  • 1
    Thank you for answer. I like espacially, that you provided additional information on the history. – Henri Menke Jun 8 '13 at 18:39
  • So the reason that using mathspec leads to the differences shown in the other answers is that it tries to be compatible to any font that defines the Unicode math characters, not only those that provide the additional parameters you mentioned? Put differently, it will ignore those parameters even if they are available? – balu Jan 8 '15 at 19:48
  • @balu The fonts don't necessarily need to define the unicode maths characters. mathspec lets you use just e.g. the letters for a calligraphic alphabet or the letters from your text font for operator and variable names etc. so that text and maths use the same ABC ... abc ... or whatever. You can substitute just the bits you want - pick 'n mix. – cfr Aug 2 '16 at 22:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.