1
\documentclass{article}

\usepackage{unicode-math}
\setmathfont[version=xcharter]{XCharter Math}
\setmathfont[version=fira]{Fira Math}
\newfontfamily\xcharterfont{XCharter}
\newfontfamily\firafont{Fira Sans}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}

\begin{document}
    \xcharterfont \mathversion{xcharter}
    Some text. \(123 \alpha a \text{a}\)
    \begin{theorem}
        This is a theorem. \(\alpha = 1, 2\).
    \end{theorem}
    \firafont \mathversion{fira}
    Some text. \(123 \alpha a \text{a}\)
    \begin{theorem}
        This is a theorem. \(\alpha = 1, 2\).
    \end{theorem}
\end{document}

This generates the following output when compiled with XeLaTeX (no warnings or errors).

output

I want XCharter fonts to be used for Theorem 1 and Fira Sans for Theorem 2. How do I get them? Why are LM fonts used in both the cases (for text) instead of XCharter and Fira?

0

1 Answer 1

1

The amsthm package issues \normalfont in some places, but you want to keep the current family.

\documentclass{article}
\usepackage{unicode-math}
\usepackage{amsthm}
\usepackage{xpatch}

\setmathfont[version=xcharter]{XCharter-Math.otf}
\setmathfont[version=fira]{Fira Math}
\newfontfamily\xcharterfont{XCharter}
\newfontfamily\firafont{Fira Sans}

% patch the amsthm commands to replace \normalfont
% with a similar command that reinstates the current font family
\makeatletter
\xpatchcmd{\@thm}{\normalfont}{\apoorv@keepcurrentfamily}{}{}
\xpatchcmd{\deferred@thm@head}{\normalfont}{\apoorv@keepcurrentfamily}{}{}
\xpatchcmd{\th@definition}{\normalfont}{\apoorv@keepcurrentfamily}{}{}
\xpatchcmd{\th@remark}{\normalfont}{\apoorv@keepcurrentfamily}{}{}
\newcommand{\apoorv@keepcurrentfamily}{%
  \expandafter\normalfont\expandafter\fontfamily\expandafter{\f@family}\selectfont
}
\makeatother

\newtheorem{theorem}{Theorem}
\theoremstyle{definition}
\newtheorem{definition}{Definition}
\theoremstyle{remark}
\newtheorem{remark}{Remark}

\begin{document}

\xcharterfont \mathversion{xcharter}
Some text. \(123 \alpha a \text{a}\)

\begin{definition}
This is a \emph{definition}
\end{definition}

\begin{theorem}
This is a theorem. \(\alpha = 1, 2\).
\end{theorem}

\begin{remark}
This is a remark.
\end{remark}

\firafont \mathversion{fira}
Some text. \(123 \alpha a \text{a}\)

\begin{definition}
This is a \emph{definition}
\end{definition}

\begin{theorem}
This is a theorem. \(\alpha = 1, 2\).
\end{theorem}

\begin{remark}
This is a remark.
\end{remark}

\end{document}

enter image description here

5
  • Thanks. Can I do the same for proofs? I, unfortunately, do not understand how the normal font is being replaced due to a lack of knowledge. So, just by analogy, I tried adding \xpatchcmd{\th@proof}{\normalfont}{\apoorv@keepcurrentfamily}{}{}, but that doesn't work. Feb 27, 2022 at 11:25
  • In short, I want the fonts selected by \<fontname>font to be applied for all environments; all the text and math before a new font is selected. It would be preferable if there is any global command that does this instead of changing fonts for each environment manually. Feb 27, 2022 at 11:29
  • Ah, I just found out that one can use the \setmainfont command outside the preamble as well. This solves my issue. Although it would have been nice if \setmainfont had support for 'versions', similar to \setmathfont. Feb 27, 2022 at 11:54
  • @ApoorvPotnis I'd recommend against resetting the main font inside the document.
    – egreg
    Feb 27, 2022 at 13:18
  • Are there any particular reasons for that? Feb 27, 2022 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .