# 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.

\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{longtable}
\usepackage{url}
%\usepackage{scrpage2}
\usepackage{fancyhdr}
\usepackage{hyperref}
\setmainfont{Iwona}
\setmonofont{Inconsolata}

\pagestyle{fancy}
\begin{document}

\lipsum[1]

\texttt{some text in monospace font}

\lipsum[2]

{\setmonofont{Courier New}
\lipsum[3]

\texttt{some text in monospace font}

\lipsum[4]
}
\newpage
\lipsum[5]

\texttt{some text in monospace font}

\lipsum[6]

\begingroup
\setmonofont{Courier New}
\lipsum[7]

\texttt{some text in monospace font}

\lipsum[8-9]

\texttt{some text in monospace font}

\lipsum[10-11]

\texttt{some text in monospace font}

\lipsum[12]
\endgroup

\lipsum[13]

\texttt{some text in monospace font}

\lipsum[14]
\end{document}


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.

-
Does the XeTeX/LuaTeX answer to this question help? How do I use a particular font for a small section of my document. –  Alan Munn Apr 12 '12 at 23:30
@AlanMunn no I have seen that one but it is simply using a different font with its own environment as opposed to redefining the monfont (in my case). as I wish the \url{} to use the newly defined mono font. –  ArTourter Apr 12 '12 at 23:34

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.

-

When the page break between page 1 and page 2 occurs, the monospaced font is Courier New, and so the \ttfamily declaration issued by \nolinkurl obeys this setting.

I suggest to use a different strategy: say in the preamble

\newfontfamily{\differenttt}{Courier New}
\DeclareTextFontCommand{\textdifferenttt}{\differenttt}


and use \textdifferenttt or \differenttt when you want to use Courier New as monospaced font.

Also change the command names to easier ones. :)

What's the main problem? LaTeX typesets the header when it performs the output routine, which is typically called when LaTeX has accumulated more material than needed to fill a page. With your font changes, the monospaced font used in the header becomes unpredictable: the output routine can be called when the group you're using to confine the new setting of \setmonofont has already ended.

If you want to ensure that Inconsolata is always used in the header, either don't issue \setmonofont in the document or specify \setmonofont{Inconsolata} in the code for the header (which is not what I would recommend, but it should work because the output routine is performed inside a group, similarly to how you reset \setmonofont).

-
I would have understood if page 3 had the changed font but not page 2 as if you add a \url{} of \texttt{} before the \newpage it still behaves the same way. whereas when the group spread between 2 and 3 the header is fine. Also the problem is if I replace the \texttt with \url, your solution doesn't work. –  ArTourter Apr 12 '12 at 23:59
@ArTourter See expanded answer. –  egreg Apr 13 '12 at 9:10

I'm assuming that you want URLs and page numbers in your headers to have a constant font (Inconsolata) and you want to have some text in Courier New. The simplest way to do this is to redefine the URL font and make a new font command for Inconsolata. The following does what you need:

\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{url}
\usepackage{fancyhdr}
\usepackage{hyperref}
\setmainfont{Iwona}
\setmonofont{Inconsolata}
\newfontfamily\nicemono{Inconsolata}
\DeclareTextFontCommand{\textnicemono}{\nicemono}
\newfontfamily\courier{Courier New}
\renewcommand{\UrlFont}{\nicemono}

\pagestyle{fancy}
\begin{document}

\lipsum[1]

\texttt{some text in monospace font}

\lipsum[2]

{\setmonofont{Courier New}
\lipsum[3]

\texttt{some text in monospace font}

\lipsum[4]
}
\newpage
\lipsum[5]

\texttt{some text in monospace font}

\lipsum[6]

\begingroup
\setmonofont{Courier New}
\lipsum[7]

\texttt{some text in monospace font}

\lipsum[8-9]

\texttt{some text in monospace font}

\lipsum[10-11]

\texttt{some text in monospace font}

\lipsum[12]
\endgroup

\lipsum[13]

\texttt{some text in monospace font}

\lipsum[14]
\end{document}

-