[Tex/LaTex] Proper way to change a font temporarily with fontspec


I have been using fontspec in a lualatex document where as instructed by the manual I set my main/sans/mono font in my preamble with some default features.

However at some point I wish to temporarily redefine the mono font (in my particular case in a longtabu, but any thing which spans multiple page behave the same way.)

an additional feature of the document is that I have my email address in my header inside a \url{}.

trying things out I enclosed my longtabu in a group and call setmonofont in the group before the table. This works fine, the text in mono font in the table is changed appropriately, the text in monofont outside the table is as defined in the preamble, but the email address in the header of the first page containing the multiple page table is changed to the redefined monofont (the header of the second page has the correct font).

I originally thought it was a bug in longtabu, then reproduced it with longtable, but then also reproduced it with simply have a group with text spanning 2 pages.

I guess it could also be a bug in fancyhdr but the same behaviour occurs with scrpage2. So either it is a bug in fontspec or I am doing something wrong. The manual specifically says that the \set{main,sans,mono}font command should happen in the preamble. If this is the case, how can I redefined or addfeatures to a set font temporarily.

I do not want to use the \addfeatures{} in the group indescrimitately as I only want to affect the text in \texttt and in \url within that group hence the redefinition of the monofont.

compiling the following MWE, you can see that on page 1, the text is changed as it should, on page 2 however, the text in the page is fine but the header changes as well as the text in the group, while on page 3 the header is back to normal.





\texttt{some text in monospace font}


{\setmonofont{Courier New}

\texttt{some text in monospace font}


\texttt{some text in monospace font}


\setmonofont{Courier New}

\texttt{some text in monospace font}


\texttt{some text in monospace font}


\texttt{some text in monospace font}



\texttt{some text in monospace font}


I apologise for the length of the MWE but I guess in this case it really is a MNWE.

I guess my question is what I am doing wrong, and if the answer is "nothing", how can I fix the bug.

Best Answer

The problem has nothing to do with fontspec. You can build similar examples for pdflatex. The behaviour depends on two things:

  1. TeX builds first the textbody and at shipout adds the header and the footer. So font definition changes on a page which are still active at shipout will affect header and footer of this page.

  2. While building a page TeX looks a bit ahead. So code/text from the next page often has already been processed and executed by TeX when the current page is shipped out. ("asynchronous page building"). So small changes in the text can mean that a \endgroup command or a closing brace which previously was seen after the shipout is now seen before the shipout.

This means you shouldn't rely on grouping to get correct fonts in header and fonts unless the group starts and ends with \newpage.