1

Answers to the question Switching back to Computer Modern within XeLaTeX provides a way to switch back to the default font in XeLaTex. However, the solution there doesn't work if I use \mathrm in a formula:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
%\newfontfamily\lmodern{Latin Modern Roman} % If font exists on your system
% Optical sizes need to be set up manually using the [SizeFeatures] option
% or select the font using the regular font selection methods
\newcommand{\lmr}{\fontfamily{lmr}\selectfont} % Latin Modern Roman
\newcommand{\lmss}{\fontfamily{lmss}\selectfont} % Latin Modern Sans
\newcommand{\lmtt}{\fontfamily{lmtt}\selectfont} % Latin Modern Mono
\begin{document}
\[\mathrm{e}^{\mathrm{i}\pi}+1=0\]
{\lmr\[\mathrm{e}^{\mathrm{i}\pi}+1=0\]}
\end{document}

You can see that the letters e and i are still not in the default font. Using some other fonts will make formulas look even worse, such as Consolas.

What should I do to solve it?

1 Answers1

2

If you want to keep Latin Modern as the default text and math font, don't execute \setmainfont instructions. Conversely, if you do want to switch the main text font to Linux Libertine O, you should probably load the unicode-math package and execute \setmathfont{Libertinus Math}. On the other hand, if you want Libertine for text-mode material but Latin Modern for math-mode material, you may want to replace \setmathfont{Libertinus Math} with \setmathfont{Latin Modern Math}[Scale=MatchLowercase].

enter image description here

%% execute this test file under XeLaTeX or LuaLaTeX

\documentclass{article}

\usepackage{unicode-math} \setmainfont{Linux Libertine O}

\begin{document}

\setmathfont{Libertinus Math} $\mathrm{e}^{\mathrm{i}\pi}+1=0$

\setmathfont{Latin Modern Math}[Scale=MatchLowercase] % or Scale=MatchUppercase $\mathrm{e}^{\mathrm{i}\pi}+1=0$

\end{document}


Addendum: As @cabohah points out in a comment, \setmainfont{Linux Libertine O} may not work with some TeX distribution/operating system combinations. If that's an issue for you, it may be preferable to load the libertine, libertinus, or libertinus-otf packages instead of executing \setmainfont and \setmathfont instructions.

Mico
  • 506,678
  • 1
    Why not loading package libertine resp. libertinus resp. libertinus-otf? \setmainfont{Linux Libertine O} usually does not work out of the box with XeLaTeX from Vanilla TeX Live but the packages do. – cabohah Mar 15 '23 at 08:47
  • @cabohah - Thanks for this. I've posted an addendum to mention the points you've made. – Mico Mar 15 '23 at 08:57