Description
Consider the following code (adapted from Enrico's answer):
\documentclass{article}
\usepackage{fontspec}
\newfontfamily\kprm{KpRoman}[NFSSFamily=kprm,Color=red]
\newfontfamily\kpsf{KpSans}[NFSSFamily=kpsf,Color=blue]
\newfontfamily\kptt{KpMono}[NFSSFamily=kptt,Color=green]
\NewDocumentCommand \mykp { } {%
\renewcommand*{\rmdefault}{kprm}%
\renewcommand*{\sfdefault}{kpsf}%
\renewcommand*{\ttdefault}{kptt}%
\normalfont
}
\begin{document}
\rmfamily abcd
\sffamily abcd
\ttfamily abcd
{%
\mykp
\rmfamily abcd
\sffamily abcd
\ttfamily abcd
}% Everything’s fine till here.
\sffamily abcd
\mykp abcd% This produces text in \rmfamily, but it is
% expected in \sffamily of kp.
\end{document}
As described in the commented lines, my aim is to have something that doesn't override the font-families. In my opinion, this is a valid requirement because \rmfamily
, \sffamily
and \ttfamily
are abstractions that interact with font-groups. In this context, \mykp
is just a font group (which should be) interacting with \sffamily
. This interaction should be unaffected by commands like \normalfont
, but that's not the case as demonstrated. When one issues \normalfont
, we automatically switch to the \rmfamily
. As noted by David in his comments below the cited answer, this is difficult in plain LaTeX as it doesn't save this information at all. Some patch work might be necessary in order to achieve that. I am unsure about how safe that would be as these macros are under heavy use by all the users. If with reliable ways that can be achieved in LaTeX itself, I think it would be a great solution, but regardless, maybe fontspec
can provide some alternative commands that can be of some help. I can think of the following macros (both user-side and programmer's side):
-
New commands for family switching that save the current family in a
tl
for later use, e.g.:\FontFamily{ ⟨family⟩ } \RmFamily % saves `rm` in `\CurrentFontFamily` \SfFamily % saves `sf` in `\CurrentFontFamily` \TtFamily % saves `tt` in `\CurrentFontFamily` \ExplSyntaxOn \fontspec_family_set:n { ⟨family⟩ } \fontspec_family_set_rm: % saves `rm` in `\g_fontspec_current_family_tl` \fontspec_family_set_sf: % saves `sf` in `\g_fontspec_current_family_tl` \fontspec_family_set_tt: % saves `tt` in `\g_fontspec_current_family_tl` \ExplSyntaxOff
-
Conditionals
\IfFontFamilyTF { ⟨family⟩ } { ⟨true-code⟩ } { ⟨false-code⟩ } \IfFontFamilyRmTF { ⟨true-code⟩ } { ⟨false-code⟩ } \IfFontFamilySfTF { ⟨true-code⟩ } { ⟨false-code⟩ } \IfFontFamilyTtTF { ⟨true-code⟩ } { ⟨false-code⟩ } \ExplSyntaxOn \fontspec_if_family:nTF { ⟨family⟩ } { ⟨true-code⟩ } { ⟨false-code⟩ } \fontspec_if_rm_family:TF { ⟨true-code⟩ } { ⟨false-code⟩ } \fontspec_if_sf_family:TF { ⟨true-code⟩ } { ⟨false-code⟩ } \fontspec_if_tt_family:TF { ⟨true-code⟩ } { ⟨false-code⟩ } \ExplSyntaxOff
-
Current font-family:
\CurrentFontFamily % prints rm, sf or tt \LetFontFamily ⟨macro1⟩ ⟨macro2⟩ % equates the font-family-command ⟨macro1⟩ to the value stored in ⟨macro2⟩ \ExplSyntaxOn \fontspec_family_current:N ⟨tl-variable⟩ % leaves in the input stream the current font family \fontspec_family_save_current:N ⟨tl-variable⟩ % saves current family in a user-given tl \fontspec_family_save_eq:NN ⟨tl-variable2⟩ ⟨tl-variable2⟩ % equates the value of the font family variable ⟨tl-variable1⟩ to the one saved in ⟨tl-variable2⟩ \ExplSyntaxOff
-
A
\normalfont
command that doesn't override font-family commands.\sffamily\NormalFont % switches to the \sffamily of the normal font. \ExplSyntaxOn \fontspec_family_set_normal: \ExplSyntaxOff