# The macros setmainfont and setsansfont do not accept the new command of a newfontfamily. Is there compatibility between them?

I use the class memoir, the packages polyglossia, fontspec, compile in XeLaTeX in Linux and on macOS.

Based on XeTeX package fontspec: Use previously defined \newfontfamily with \setmainfont (there were not available answers to this), I would like to set an extra font coming from a new command of newfontfamily to setmainfont and setsansfont because I find unnecessary to multiply the same path/localization of the fonts in each macro. I want to economise and optimise the codes. Here is the MWE:

\documentclass[12pt, a4paper, oneside, oldfontcommands, dvipsname]{memoir}

\usepackage{fontspec}

\newfontfamily\alegreya{Alegreya}[Path = ./fontes/,
FontFace = {xb}{n}  {* Black},
FontFace = {xb}{it} {* Black Italic},
UprightFont = * Regular,
ItalicFont = * Regular Italic,
BoldFont = * Bold,
BoldItalicFont = * Bold Italic,
]

\setmainfont{\alegreya}
\setsansfont{\alegreya}


Only my newfontfamily part works, but the set*font does accept the defined newcommand.

• That won't work. Create a fontspec-file if you want to reuse font settings, see section 2.3 By custom file name in the fontspec documentation. – Ulrike Fischer Nov 30 '18 at 11:09

You can do it by assigning the family name. The string is essentially arbitrary, so long as it doesn't refer to an existing font family.

\documentclass{article}

\usepackage{fontspec}

\newfontfamily\alegreya{Alegreya}[
%Path = ./fontes/,
FontFace = {xb}{n}  {*-Black},
FontFace = {xb}{it} {*-BlackItalic},
UprightFont = *-Regular,
ItalicFont = *-Italic,
BoldFont = *-Bold,
BoldItalicFont = *-BoldItalic,
NFSSFamily=alegreyaot
]

\renewcommand{\rmdefault}{alegreyaot}
\renewcommand{\sfdefault}{alegreyaot}

\begin{document}

This should be Alegreya

\textsf{This too}

{\normalfont\alegreya Check}

\end{document}


• Thank you a lot, I also have tested fontspec-file as Ulrike has suggested and it worked too. But what is the renewcommand for default maths font, similar to setmathsfont: mathnormal, mathrm or mathsf? – Gustavo Reis Nov 30 '18 at 12:06
• @GustavoReis Since there is no math font compatible with Alegreya, I'd not bother. But \setmathrm{Alegreya-Regular} should work. – egreg Nov 30 '18 at 12:10
• Then I will use Charter BT Math or GFS Neohellenic Math or Linux Libertinus Math. – Gustavo Reis Nov 30 '18 at 12:13