# Combining features with fontspec's addfontfeature command

Let's suppose I have a font with two features, feat1 and feat2. Now, based on the name, intuitively, it would seem that \addfontfeature adds a particular feature to the current configuration:

% feat1 & feat2 OFF
{
% feat1 ON, feat2 OFF
{
% feat1 & feat2 ON
}
% feat1 ON, feat2 OFF
}
% feat1 & feat2 OFF


However, as far as I am aware, this isn't what happens. Rather, the command works as such:

% feat1 & feat2 OFF
{
% feat1 ON, feat2 OFF
{
% feat1 OFF, feat2 ON
}
% feat1 ON, feat2 OFF
}
% feat1 & feat2 OFF


What makes the actual behavior of \addfontfeature inconvenient is when a package uses it to add a feature of a font

Two \addfontfeature are additive:

\documentclass{scrartcl}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\begin{document}123
123}}

\end{document}


But there can be features that cancel each other.

• This is rather confounding then. In the last question you helped me with, a command appeared to have precisely non-additive behavior – Kevin Keith Apr 7 '17 at 20:56
• No the problem in the other question was not the number of \addfontfeature, but that the A used a different sets of features than the 8. So you actually used two fonts and there can be no kerning between to separate fonts. – Ulrike Fischer Apr 7 '17 at 21:02
• Okay, so then fontspec treats two characters as being of the same font, if and only if they share precisely the same features? – Kevin Keith Apr 7 '17 at 21:06
• Yes. When you add a feature a new font is created. You can see it in the log. – Ulrike Fischer Apr 7 '17 at 21:07

No, it does not work that way. Only clashing options do exclude each other. See this example

\documentclass[10pt]{article}
\usepackage[nofoot]{geometry}
\usepackage{fontspec}

\setmainfont[Ligatures={TeX}, SmallCapsFeatures={%
LetterSpace=7,Letters=SmallCaps},%
Kerning=Uppercase,%

\begin{document}
\noindent
Test  out 123\\ {\addfontfeature{Ligatures=Rare} test in first ct 123\\ {\addfontfeature{Numbers=OldStyle} test  in second ct st 123} test in first ct} \\ test out 123
\end{document}


Which yields:

This is because the Ligature and Numbers are separate features: they don't clash.

But look at this example, where I redefine the Numbers feature

\documentclass[10pt]{article}
\usepackage[nofoot]{geometry}
\usepackage{fontspec}

\setmainfont[Ligatures={TeX}, SmallCapsFeatures={%
LetterSpace=7,Letters=SmallCaps},%
Kerning=Uppercase,%

\begin{document}
\noindent
Test  out 123\\
{
\addfontfeature{Numbers=OldStyle} test in first ct 123\\
{
\addfontfeature{Numbers=Lining} test  in second ct st 123
} \\
test in first ct 123} \\
test out 123
\end{document}


It will behave just as you said:

Since it's so well written I recommend you take a look at the fontspec documentation.

EDIT

I understand that you may not have the latest version/have some problems with your installation, since it works fine for me.

Since I thought you posted an example about colour, for completeness sake I did the same:

\documentclass[10pt]{article}
\usepackage[nofoot]{geometry}
\usepackage{fontspec}

\setmainfont[Ligatures={TeX}, SmallCapsFeatures={%
LetterSpace=7,Letters=SmallCaps},%
Kerning=Uppercase,%

\begin{document}
\noindent
Test  out 123\\
{
\addfontfeature{Numbers=OldStyle} test in first ct 123\\
{
\addfontfeature{Color=FF0000} test  in second ct st 123
} \\
test in first ct 123} \\ test out 123
\end{document}


Yielding

Seems that fontspec behaves as expected, under certain conditions. MWE compiled with LuaLaTeX in TeXlive 2016:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\begin{document}
It was a {\addfontfeature{RawFeature=+smcp,RawFeature=+onum}dark 269} and stormy night.
\end{document}


In the above, the single \addfontfeature correctly applies both small caps and old style numerals.

Now, it may depend on which features you are using. According to fontspec documentation, some features play against others, so it may be that the ones you chose are fighting each other. For example, you cannot add both OldStyle and Lining numerals, because they fight; one or the other wins, depending on how and where you call the code. Can't tell, because you didn't say what they were. However, I don't think that's what happened in your case.

There have been many recent changes to fontspec in the past couple of months, including very recently. I do know (because I aksed the package maintainer) that there was an issue regarding the internal usage of plus and minus to switch some features on and off. Recently corrected (possibly not yet available). It could be that you have stumbled into such an issue.

So, first verify that my above code does work. If it does not work for you, then something in your installation needs to be changed. If it does work for you, then come back and tell us which specific font features you are using, and which specific font is in use.

• that's exactly the cased the OP was trying to avoid:-) see Ulrike's answer to the (not mentioned:-) previous question tex.stackexchange.com/a/362657 where she replaced two calls to \addfontfeature by one call, adding two features. – David Carlisle Apr 7 '17 at 20:52
• @DavidCarlisle There is also a way to "remove" features, but it involves looking into fontspec's internal code, and I have been warned not to use it by WR, as it is not intended for that purpose and subject to change. – user103221 Apr 7 '17 at 20:59
• what is bizarre about all this is that I looked at realscripts's implementation, and there doesn't appear to be any reason why it should behave non-additively. – Kevin Keith Apr 7 '17 at 21:01
• @KevinKeith see the log output in my answer to the previous question, font features define variant fonts so they may be the same otf file, but to tex they are unrelated fonts once loaded – David Carlisle Apr 7 '17 at 21:12