I'm writing a document using xelatex and have set up my document fonts using fontspec as follows:

\setmainfont[Mapping=tex-text,Numbers=OldStyle]{Linux Libertine O}
\setsansfont[Mapping=tex-text,Numbers=OldStyle,Scale=MatchLowercase,SmallCapsFont={Linux Biolinum Capitals O}]{Linux Biolinum O}
\setmonofont[Mapping=tex-text,Scale=MatchLowercase]{DejaVu Sans Mono}

In my document, I'd like the math font to use either Linux Libertine or Linux Biolinum based on whether the surrounding text font is serif or sans serif. For example:

\normalfont This text is Linux Libertine as is the following math: $a^2 + b^2 = c^2$.
\sffamily This text is Linux Biolinum as is the following math: $a^2 + b^2 = c^2$.

Here's what I've tried so far and the components of the problem I'd like to solve:

  1. The normal math text should be set in Linux Libertine, not Computer Modern. CM can serve as a fallback provide any glyphs that LL doesn't provide, however (e.g., large symbols and operators, but not Greek letters). The mathspec package handles this requirement fairly well.

  2. If the surrounding body text is set in sans serif (this is used for tables, figures, and captions), then the math should be set in sans serif. I'd prefer this be done automatically, but I don't mind adding a \sfmath command (or the like) if necessary. Unfortunately, I couldn't figure out how to coerce mathspec into handling this part.

A few things to note:

  1. Neither Linux Libertine nor Linux Biolinum have much if anything in the Unicode range for math glyphs. Therefore, the Greek letters should be pulled from the Greek alphabet block and not the math Greek block, for instance.

  2. I'm not using \mathsf or the math sans alphabet to indicate anything special in this document so there won't be any confusion when the math is set in sans serif solely to match the surrounding text.

The similar questions I've found on TeX.sx appear to be for pdflatex or to make use of the \mathsf command.

  • 2
    As you've noted, neither Linux Libertine nor Linux Biolinum have unicode-based built-in math fonts. The unicode math font which (AFAICT) is closest to Libertine/Biolinum is Asana Math; it has both serif/italics and sans-serif/upright letters. Give it a try. The match with Libertine/Biolinum is by no means perfect, but it's a whole lot better than what's achievable with the Latin Modern, the XITS, or Cambria math fonts. If you choose to use the Asana math fonts, you can load them with the commands \usepackage{unicode-math} and \setmathfont[version=asana]{Asana Math}.
    – Mico
    Jan 24, 2012 at 8:41
  • @Mico: Asana Math is a nice font, but I'm more interested in having the math match the surrounding font (Linux Libertine or Linux Biolinum) instead. (I really only need the numbers and letters to match. The symbols could be from a different math font as long as they're in a relatively similar style to LL and LB, as appropriate.) I'm not using Unicode to typeset the math; I'm using the standard LaTeX macros.
    – godbyk
    Jan 24, 2012 at 10:53
  • mathastext is one of the best option
    – MadyYuvi
    Feb 26 at 9:35
  • Instead of Libertine. use Libertinus, it has a math font and is more or less the same as Libertine
    – user187802
    Feb 26 at 9:54

2 Answers 2


The following isn't sophisticated, but it is clean:

  • Create two virtual fonts that are amalgams of the maths sans and serif fonts you want, so that all the characters are in them. See topskip's explnation of how to create virtual fonts, How to create a virtual font?

With these fonts, defining \mathsf is easy. It is possible to define the maths font from outside the maths environment, for instance as described by Andrew Stacey, Change math font only in some parts of a document?.

  • Is it possible to automatically type all the math in the mathsf font when the surrounding text is sffamily? I'd like to find a solution that automatically switches between sans serif and serif math based on the surrounding text.
    – godbyk
    Mar 23, 2013 at 8:52
  • @godbyk - Yes, using the technique Andrew Stacey described in the 2nd link I gave: the unicode-math package provides \setmathfont (which I assume uses \everymath), which allows you to redefine \sffamily so that it changes both the body and math fonts. Mar 23, 2013 at 17:20

How about using mathastext? The following uses newtx which loads libertine and all the math symbols.


\setmonofont{DejaVu Sans Mono}[
\swf{sans} Sans test 123 $\sum f(x)\cdot\frac{x}{yz}+\alpha\beta$

\swf{mono} Mono test 123 $\sum f(x)\cdot\frac{x}{yz}+\alpha\beta$

\swf{roman} Roman test 123 $\sum f(x)\cdot\frac{x}{yz}+\alpha\beta$

Switch only math, but not text:

\swm{sans} Sans math test 123 $\sum f(x)\cdot\frac{x}{yz}+\alpha\beta$

\swm{mono} Mono math test 123 $\sum f(x)\cdot\frac{x}{yz}+\alpha\beta$

\swm{roman} Roman math test 123 $\sum f(x)\cdot\frac{x}{yz}+\alpha\beta$


switch roman to sans to tt

Compiling with Lualatex the output looks better (better spacing)

same in lualatex

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .