# Automatic switching of math font based on current text font

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

``````\usepackage{fontspec}
\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.

-
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 '12 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 '12 at 10:53

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 '13 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. – Charles Stewart Mar 23 '13 at 17:20