Replacing greek glyphs in math mode

I want to switch fonts, away from computer modern. I'm pretty sure that I will end up with a mixture of fonts. I can't provide a full list of required features, but Linux Libertine's feature set pretty much covers it. It is, at the moment, my base font for text work, together with Linux Biolinum.

I need to adjust the math glyphs, though. They are still taken from the cm fonts and obviously don't fit. As a start, I want to use those provided by GFS, like GFS Bodoni (too playful), or GFS Didot (all available as OTF). To achieve this glyph replacement, I started with one of the mathastext examples:

\documentclass{article}

\usepackage[T1]{fontenc}
\renewcommand{\rmdefault}{bodoni}
\usepackage[LGRgreek,defaultmathsizes,italic]{mathastext}
\usepackage{libertine}
\let\varphi\phi

\begin{document}
ff fi fj fi ffj fl ffl äÄ öÖ üÜ ß\\
01234567890\\
$$abcdefghijklmnopqrstuvwxyz$$
$$\alpha \beta \gamma \delta$$
\end{document}


This compiles without any errors, and the math glyphs are replaced by those from GFS Bodoni, I get ligatures, correct Umlauts (the next problem arises here: LL provides an alternative O-umlaut, which I prefer). However, I don't know how to access text figures from either LL or GFS Bodoni, while both provide them. My plan was to translate the above mwe to valid code for LuaLaTeX, gaining more control over the font selection. I failed:

\documentclass{article}

\usepackage[no-math]{fontspec}
\renewcommand{\rmdefault}{bodoni}
\usepackage[LGRgreek,defaultmathsizes,italic]{mathastext}
\setmainfont{Linux Libertine O}
\setsansfont{Linux Biolinum O}


This sets mathnormal, mathrm, mathbf and mathit to computer modern (bad), but uses greek glyphs (which is good). The symbols used are still taken from the CM fonts, but I have not yet tried to replace them. I will need to, because the default int-symbol's terminals are - as an example - ball-shaped.

I then found the unicode-math package, but from what I read it is meant to be used for fonts which provide a full unicode math alphabet, which the GFS fonts do not. I tried to use unicode-math, but with less success than with the examples given above.

Ultimately, I need a font selection procedure which is not hacked to fit one specific situation. I want to see how the different glyphs work together on the same page, and I might even buy a font that suits the text and its purpose, and not the technical limitations caused by my lack of skill. How can I develop a procedure of selecting fonts and glyphs that is robust and gives me the required flexibility?

Is unicode-math the way to go?

Regarding \setmainfont instead of \renewcommand: I now used

\documentclass{article}
\usepackage{fontspec}
\setmainfont[%
]{GFSBodoni}
\usepackage[LGRgreek,defaultmathsizes,italic]{mathastext}


as you suggested, with and without the no-math option for fontspec, and lualatex complains:

! Font \LGR/GFSBodoni(0)/m/it/7=grmn0700 at 7pt not loadable: metric data not found or bad.


These errors occur in

$$abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ$$


This could either be caused by my usage of the font or by an error in my miktex 2.9 installation. However, a pdf is produced, with the following features:

• The main fonts are GFS Bodoni,
• The greek glyphs in math mode are upright, not CM anymore, but not to be found in the GFS Bodoni Specimen sheet either. I don't know where they come from. The preamble above is what I used, it is directly followed by \begin{document}.

I then downloaded the otf files from the GFS website, and told fontspec to use them directly. I even renamed them to make sure that lualatex does not find any other Bodoni files elsewhere:

\setmainfont[%
ItalicFont=GFS-Bodoni-Italic.otf,
BoldFont=GFS-Bodoni-Bold.otf,
BoldItalicFont=GFS-Bodoni-Bold-Italic.otf
]{GFS-Bodoni.otf}


I'm puzzled. I'll now start trying the same thing with unicode-math, as you suggested.

unicode-math: After switching back and forth between different combinations of options, this is what I first came up with:

\documentclass{article}
\usepackage{unicode-math}
\setmathfont{XITS Math}
\setmainfont{Linux Libertine O}
\setsansfont{Linux Biolinum O}
\setmathfont[
math-style=ISO,
range=\mathit/{greek,Greek},
]{GFSBodoni-Italic}
\setmathfont[
math-style=ISO,
range=\mathrm/{greek,Greek},
]{GFSBodoni}


Greek glyphs are now replaced, as intended. I can change the latin characters to Linux Libertine with the same trick, using two new blocks and {latin,Latin} in the range option. My original question has been answered, in the sense that I was encouraged to use unicode-math. Thanks Ulrike!

However, the quest goes on. Using a unicode math font left me with undesired figures, which I could also replace by figures from GFS Bodoni or Linux Libertine. Unfortunately, none of the following ideas work (the first is taken from the unicode-math documentation):

\setmathfont[
math-style=ISO,
range=\mathit/{num},
]{Linux Libertine O}

\setmathfont[
math-style=ISO,
range=\mathrm/{num},
]{Linux Libertine O}

\setmathfont[
math-style=ISO,
range=\mathit/{"0030-"0039},
]{Linux Libertine O}

\setmathfont[
math-style=ISO,
range=\mathrm/{"0030-"0039},
]{Linux Libertine O}


A major drawback is that I need to use a unicode math font, as above. My choice is at the moment limited to XITS, Asana Math, and (partially) Neo Euler. Neo Euler seems to have problems with scaling parentheses. Apart from that, I don't really like any of these choices, judging just by the more prominent math symbols (like int, parentheses, n-ary sum and n-ary product sign), but that's not the point of this question.

• Hi Christoph! Welcome to tex.sx! This is a very good question, that unfortunately I don't feel able to answer, but someone probably will! It's not necessary to sign your questions or answers (as there is already a box with your username below it) or to begin them with a greeting, as this is more of a q&a site than a conventional forum. – Edd Apr 5 '11 at 12:39

In your lualatex example the \renewcommand{\rmdefault}{bodoni} won't work. It will look for an eu2bodoni.fd which doesn't exist. Switch to the font you want mathastext to pick up by using (as described in documentation) a suitable \setmainfont before loading mathattext.

Apart from this: The font selection depends a lot of the actual font. It is rather easy to replace the font for letters and numbers, but when it comes to symbols you need one or more fonts which contains them and you will perhaps have to adjust various fontdimens. If you really want to use another open type font for the symbols your best bet is at my opinion unicode-math, but it can't do wonders if a symbol doesn't exist:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{Arial}
\setmathfont{Arial}
\begin{document}
$a = \sin{0} \int \boxtimes \mp$
\end{document}

• My lualatex example does work, it compiles with 0 errors, 0 warnings and 1 badbox. The resulting pdf definitely shows the GFS Bodoni glyphs in math mode. Maybe it didn't work as should be expected, but the glyphs have been replaced. – Christoph Apr 5 '11 at 14:23
• You are contradicting yourself. You wrote "This sets mathnormal, mathrm, mathbf and mathit to computer modern (bad)". Also I tested it. \renewcommand\rmdefault doesn't work. You need \setmainfont instead. – Ulrike Fischer Apr 5 '11 at 14:45
• You suggested unicode-math, which did the trick. I'm still struggling with furhter "small wishes" which are related to my question, but technically, my initial problem has been solved. – Christoph Apr 5 '11 at 21:45

Ulrike is right. Note that when LuaLaTeX complains and writes \LGR/GFSBodoni(0)/m/it/7=grmn0700 the grmn is not at all GFS Bodoni. It is a font from the Claudio Beccari cbfonts, and it comes in this story I guess from the \DeclareFontShape{LGR}{lmr}{m}{n} declarations from lgrlmr.fd. At some point LuaLaTeX did not find GFS Bodoni, so tried Latin Modern, and in LGR encoding it leads up to the grmn font. I don't know enough about LuaLaTeX and fontspec to understand and/or explain more.

On the other hand, if your goal is to use the Greek glyphs from Bodoni, while at the same time you are happy with Linux Libertine and Biolinum, you can try this:

\documentclass{article}

\usepackage[no-math]{fontspec}

\setmainfont{Linux Libertine O}
\setsansfont{Linux Biolinum O}

\usepackage[LGRgreek,defaultmathsizes,italic]{mathastext}
\SetSymbolFont{mtlgrfontlower}{normal}{LGR}{bodoni}{m}{it}
\SetSymbolFont{mtlgrfontupper}{normal}{LGR}{bodoni}{m}{n}
\SetSymbolFont{mtlgrfontlower}{bold}{LGR}{bodoni}{\bfdefault}{it}
\SetSymbolFont{mtlgrfontupper}{bold}{LGR}{bodoni}{\bfdefault}{n}

\begin{document}
abcd  $\mathrm{abcd}$ $\mathbf{abcd}$

\textit{abcd} $abcd$  $\mathnormal{abcd}$

\textsf{abcd} $\mathsf{abcd}$

$\alpha\beta\Gamma\Delta$
\end{document}


Here is a different method, which has a defect though:

\documentclass{article}

\usepackage[no-math]{fontspec}

\let\savedrmdefault\rmdefault
\renewcommand{\rmdefault}{bodoni}
\usepackage[LGRgreek,defaultmathsizes,italic]{mathastext}

%% at this stage the normal math version is not functional
%% because eu2bodoni.fd does not exist, but the Greek
%% letters are correctly set up.
\renewcommand{\rmdefault}{\savedrmdefault}

\setmainfont{Linux Libertine O}
\setsansfont{Linux Biolinum O}

\Mathastext[libertine]
%% We have to use it with the optional parameter, else it
%% will reset the Greek letters. Also make sure to have
%% version 1.14c of mathastext, and do not use LGRgreeks as
%% an option, but as done here LGRgreek. The LGRgreeks
%% option is for people who want the Greek font to be reset
%% in each math version.

\begin{document}
%% the first thing we do is to change the "normal"
%% (non-functional) for the "libertine" math version.
\Mathastextversion{libertine}

abcd  $\mathrm{abcd}$ $\mathbf{abcd}$

\textit{abcd} $abcd$  $\mathnormal{abcd}$

\textsf{abcd} $\mathsf{abcd}$

$\alpha\beta\Gamma\Delta$

\end{document}


The defect is that the math versions normal and bold will not be functional.

Let me point out in passing that for mathastext the alphabets mathrm, mathbf, mathnormal and mathit necessarily refer to the font used for the Latin letters in math mode. If you want otherwise, you can use the defaultalphabets option. Or, you may set up in the preamble yourself the alphabets. Also it is a limitation mathastext that when it redefines \mathsf and \mathtt this is necessarily to refer to fonts with the same encoding as the font used for Latin letters and digits in math mode.

But, while I am at it, why not shamelessly do also some pub for my libgreek package?

\documentclass{article}
\usepackage[no-math]{fontspec}
\setmainfont{Linux Libertine O}
\setsansfont{Linux Biolinum O}

\usepackage{libgreek}
\usepackage[defaultmathsizes,italic]{mathastext}

\begin{document}
abcd  $\mathrm{abcd}$ $\mathbf{abcd}$

\textit{abcd} $abcd$  $\mathnormal{abcd}$

\textsf{abcd} $\mathsf{abcd}$

$\alpha\beta\Gamma\Delta$

\end{document}


and then everything is with Libertine/Biolinum!

• I could not compile your first example (which uses \SetSymbolFont). lualatex and xelatex both complain about undefined symbol fonts "mtlgrfontlower" (and more similar ones). I stumbled across a similar piece of code somehere on the internet, but did not succeed in adapting it to my needs. – Christoph Apr 5 '11 at 21:42
• Hi Christoph, I am slow in answering because I am new on this community site. If "mtlgrfontlower" is said not to exist, then the simplest explanation is that your version of mathastext is a bit old. (more than 24 hours old...). Make sure you have the latest one (currently 1.14c) – user4686 Apr 5 '11 at 22:16
• I'll get a recent mathastext when I'm home from work! Thanks for pointing that out. – Christoph Apr 6 '11 at 5:49

I reply briefly again on the possible use and limitations of mathastext to replace fonts for glyphs in math mode: as described in the doc mathastext is mainly concerned with the Latin letters, the digits, and a few other signs almost all from the Basic Latin-1. It also provides a few options for Greek glyphs (without diacritics). Regarding your initial attempt, the way to make it work would have been:

\usepackage[no-math]{fontspec}

\renewcommand\rmdefault{bodoni}

\let\savedencoding\encodingdefault % EU2 with Luatex
\renewcommand\encodingdefault{T1}

\usepackage[LGRgreek,defaultmathsizes,italic]{mathastext}
\renewcommand\encodingdefault{\savedencoding}

\setmainfont{Linux Libertine O}
\setsansfont{Linux Biolinum O}


Note that the mathrm, mathit, mathbf, mathnormal will all use the T1-encoded Bodoni. mathsf will use the T1-encoded Latin modern sans. If we put the \setsansfont prior to mathastext, \mathsf will try to use the OpenType Linux Biolinum but under a non-existent T1 encoding (rather than EU2).

If on the other hand you type:

\setmainfont{GFS Bodoni}
\setsansfont{Linux Biolinum O}
\usepackage[defaultmathsizes,italic]{mathastext}
\setmainfont{Linux Libertine O}


then OpenType Bodoni will be used for Latin letters and digits in math mode, and the \mathsf will correctly use the OpenType Linux Biolinum. But it is not possible to use the LGRgreek option, which would try to load a non-existent LGR/GFSBodoni(0) font rather than the TeX font LGR/bodoni.

In brief, unicode-math is definitely the way to go, mathastext is not designed for Unicode fonts, although it can be used with them within its modest scope.