1

This question concerns the "best practice" for fontspec with changing OpenType features. I use only lualatex, although I suppose that this also pertains to xelatex.

I mean in terms of compiler efficiency, as well as good programming practice. A few KiB more or less in cache, or a few milliseconds more or less in compile time, are unimportant. I use text only, no math, if that makes much of a difference.

In the first MWE, the font is loaded without selecting any features, then OldStyle is added before any text is printed, so that it becomes the default:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}
\AtBeginDocument{\addfontfeature{Numbers=OldStyle}}
\begin{document}
Hello 196 World.\par
{\addfontfeature{Numbers=ResetAll,Numbers=Lining}Hello 196 Again, World.}\par
\end{document}

In the second MWE, the font is loaded with OldStyle selected:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}[Numbers=OldStyle]
\begin{document}
Hello 196 World.\par
{\addfontfeature{Numbers=ResetAll,Numbers=Lining}Hello 196 Again, World.}\par
\end{document}

In the third MWE, the font is loaded with OldStyle selected, but a new font is defined with Lining. Then the font is changed:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}[Numbers=OldStyle]
% I realize that this font uses Lining by default, but for MWE purposes:
\newfontfamily\linemup{Latin Modern Roman}[Numbers=Lining]
\begin{document}
Hello 196 World.\par
{\linemup Hello 196 Again, World.}\par
\end{document}

Which of the above is technically best? They all produce the same print. My device runs a jailed Linux subsystem, where the larger system is often doing other things, so a direct comparison is hard to interpret.

6
  • 1
    The third way is definitely the recommended one, although I'm not sure I can give you a solid argument why.
    – Alan Munn
    Oct 15, 2023 at 20:38
  • @AlanMunn Indeed, I got that impression from browsing around. But only an impression.
    – rallg
    Oct 15, 2023 at 20:46
  • 2
    your examples are misleading as you only use \addfontfeature once. If you had several groups, the third form would just set up the font once, then have a relatively quick font change. the first forms do a relatively expensive feature setup each time. Oct 15, 2023 at 21:28
  • A related, although old question: Is using \addfontfeatures (much?) less efficient than defining a \newfontface?
    – Alan Munn
    Oct 15, 2023 at 21:38
  • 1
    @AlanMunn Am I misremembering/misreading? I thought the current fontspec manual encouraged creating a new font if the feature was required repeatedly, but not for a one-off change. Are there any downsides to adding fonts for one-off cases?
    – cfr
    Oct 16, 2023 at 0:41

1 Answer 1

1

As the above comments revealed, the best practice will depend on whether the change is used repeatedly, or rarely. I am not aware of any special cross-over criterion between frequency and rarity, but in my own case, the situation is clear:

I use a lot of text (not math) with OldStyle numerals as default. But on the Copyright page, it is better to print the copyright year, ISBN, and possibly Library Catalog number, with lining numerals. That would be "rare" and suited to local change.

Page numbers would normally use the main font (with OldStyle numerals). But if I wanted Lining numerals instead, then they would be printed hundreds of times. This would be frequent, justifying its own font definition with Numerals=Lining as its default.

I imagine that there are others who could use this information, so I wrote it as an answer. If incorrect, please comment, and I will change or remove it. Or post a better answer!

1
  • 1
    This seems like a reasonable answer, although I think that conceptually it's better to always declare a new font family even if there's not a real processing cost. It's also one less bit of low-level syntax to remember.
    – Alan Munn
    Oct 16, 2023 at 19:45

You must log in to answer this question.

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