[Tex/LaTex] \text (polyglossia) changes font

fontspecpolyglossia

I'm using polyglossia to switch between languages in my documents and fontspec to define various fonts. The combination gives me the following problem.

An MWE:

\documentclass{article}
\usepackage{polyglossia}
\setmainlanguage{english}
\setotherlanguage{dutch}
\usepackage{fontspec}
\newfontfamily\mono{Latin Modern Mono}[Scale=2] % scaled for visibility
\begin{document}
foo\mono bar foo\textdutch{bar}
\end{document}

Result:

enter image description here

Why isn't the second "bar" typeset in the current font family (\mono)? Is this due to a feature in polyglossia that supports different fonts for different languages? If so, how do I redefine \text<language> so that it uses the current font, or: How do I detect the current font and then activate it within \text<language>{<here>}? Or am I plainly doing something wrong?

Update:
Answered by Ulrike Fischer, see below, but I have a persistent problem and a solution:

Another not-so-MWE to illustrate what I'm doing wrong.

\documentclass[]{memoir}

\usepackage{polyglossia}
\setmainlanguage{english}
\setotherlanguage{dutch}
\newcommand*\dutchfont{} % the solution to the first problem, thanks to Ulrike Fischer

\usepackage{fontspec}
\newfontfamily\titlefont{Latin Modern Mono}[Scale=2] % scaled for visibility

\renewcommand*{\maketitlehooka}{\titlefont} % this hook executes just before \thetitle is printed

\title{fo\textdutch{ob}ar}

\begin{document}

\sffamily % <=== disable this and the thanks have the desired font
% \fontfamily\sfdefault\selectfont % this works correctly
\maketitle

\end{document}

It causes the same incorrect font selection when the language is changed. In this MWE, the text foobar is in english and in a font selected with \titlefont, but the -ob- in the middle is in dutch. This language change results in a font change as well if \sffamily (and probably any other \..family command) has been called before the text. I used \sffamily before \maketitle to typeset the various title page elements in sans serif. Any text containing another font than default and language changes reverts to the default sans serif font, based on my experience. Using \fontfamily\sfdefault\selectfont instead of \sffamily does work without problems. Why is the use of \sffamily incorrect here?

Best Answer

You can add a definition for \dutchfont:

\documentclass{article}
\usepackage{polyglossia}
\setmainlanguage{english}
\setotherlanguage{dutch}
\newcommand\dutchfont{}
\usepackage{fontspec}
\newfontfamily\mono{Latin Modern Mono}[Scale=2] % scaled for visibility
\begin{document}
foo\mono bar foo\textdutch{bar}
\end{document}

But I would prefer babel over polyglossia.

Edit

Regarding the second example: polyglossia redefines \sffamily to store the "familytype":

 \csgappto{sffamily }{\def\familytype{sf}}

and it uses this familytype to decide if and to which fontfamily it should switch at language boundaries. It is imho difficult to disable this system and polyglossia only knows three standard font families. Also there is no concept of "scriptfamilies", so one can't disable font switches between english and dutch but preserve them between dutch and cyrillic.

The general rules for fonts that should survive language boundaries are: Use the standard families and the standard commands like \bfseries or \large and not settings in fontspec options like [Scale=2]:

\documentclass[]{memoir}
\usepackage{polyglossia}
\setmainlanguage{english}
\setotherlanguage{dutch}

\usepackage{fontspec}
\newcommand\titlefont{\sffamily\Huge} % scaled for visibility

\renewcommand*{\maketitlehooka}{\titlefont} % 

\title{fo\textdutch{ob}ar}

\begin{document}

\sffamily 
\maketitle

\end{document}

If you want to use a special font somewhere one could probably hide it as a special variant in one of the standard families to fool polyglossia.

Related Question