Small caps and old style numerals not working with XeTeX in 2021

fontsoldstylenumssmall-capsxetex

As above, I'm having difficulty getting small caps and old style numerals to print properly when using CMU Serif with XeTeX.

I'm aware that this is a topic that has been discussed before but I wasn't able to solve my problem using those previous answers. Previous solutions seem mostly to concern fixing a bug in a MikTeX update, and I use TeX Live.

Here's a MWE of my problem. You can obtain the same version of CMU Serif as me from the link above.

\documentclass{amsart}

\usepackage{fontspec}

\setmainfont[Numbers=OldStyle]{CMU Serif}

\begin{document}

Hello, 1234567890!\\

\textsc{goodbye!}

\end{document}

If I run the above through a xelatex main.pdf, the numbers are not old style, and the 'goodbye' is printed in all lower case letters. However, if I change the line setmainfont[Numbers=OldStyle]{CMU Serif} to setmainfont[Numbers=OldStyle]{Linux Libertine O}, everything works – except that it's not in the font I want! This suggests to me that the font files for CMU Serif are the problem. Is that the case?

My current file for "CMU Serif, Roman" contains 700 glyphs. Is that too few? If it is, I'm having trouble tracking down a more complete set of Computer Modern .ttf files.

Best Answer

Loading fonts by file name is currently the recommended way, but simply loading cmunrm.otf will not find the other font faces, such as bold or italic. To enable these, you may use the following:

\documentclass{article}
\usepackage{fontspec}

\defaultfontfeatures[CMUSerif]{
  Ligatures={Common,TeX},
  Numbers=OldStyle,
  UprightFont=cmunrm,
  BoldFont=cmunbx,
  ItalicFont=cmunti,
  SlantedFont=cmunsl,
  BoldItalicFont=cmunbi,
  BoldSlantedFont=cmunbl,
  Extension=.otf}

\setmainfont{CMUSerif}

\begin{document}
\noindent Computer Modern Serif \textit{Italic} \textsl{Slanted} \textsc{Small Caps \textsl{Oblique}}
\textbf{Bold \textit{Italic} \textsl{Slanted} \textsc{Small Caps}}
\end{document}

You may also save the \defaultfontfeatures command to a file named CMUSerif.fontspec, which will allow you to use \setmainfont{CMUSerif} in your documents.

If you need to do the same for sans-serif or typewriter, the index of filenames is here. Another good alternative is to load \usepackage{newcomputermodern}.

You will not need to bother with this extra setup, although it will still work, if XeTeX or LuaTeX is already configured to look up the font by its display name. You can test whether XeTeX has this font in its index with fc-match "CMU Serif", and whether LuaTeX does with luaotfload-tool --find="CMU Serif".

If your Linux system does not find your fonts automatically, locate the configuration file you need with kpsewhich texlive-fontconfig.conf and symlink or copy it to /etc/fonts/conf.d/, or the appropriate directory on your system. You might also want to refresh your font caches with fc-cache -f -s -v and luaotfload-tool -f -u -p -v. You can also install the fonts as system fonts, to be able to use them in all your applications. On Linux, XeTeX and LuaTeX should also be able to find them in your user font directories, so you should be able to put a copy or symlink to a font directory of your TeX tree in ~/.fonts/.