You have to compare the current value of \f@family
and of \sfdefault
: if they match, then you're typesetting in sans serif type.
Basically you do
\ifnum\pdf@strcmp{\f@family}{\sfdefault}=\z@
<we are using sans serif>
\else
<we are not using sans serif>
\fi
Example:
\usepackage{pdftexcmds}
\makeatletter
\DeclareRobustCommand{\film}[1]{%
\ifnum\pdf@strcmp{\f@family}{\sfdefault}=\z@
\fakesc{#1}%
\else
\textsc{#1}%
\fi
}
\makeatother
Just for the fun of it, here's an implementation that uses l3regex
:
\documentclass{article}
\usepackage{fontspec}
\usepackage{xparse}
\setmainfont{Linux Libertine O} % has small caps
\setsansfont{Alegreya Sans} % has no small caps
\ExplSyntaxOn
\NewDocumentCommand{\film}{ m }
{
\simifilm_film:n { #1 }
}
\tl_new:N \l__simifilm_fakesc_tl
\cs_new_protected:Npn \simifilm_film:n #1
{
\str_if_eq_x:nnTF { \use:c { f@family } } { \sfdefault }
{
\simifilm_fakesc:n { #1 }
}
{
\textsc{ #1 }
}
}
\cs_new_protected:Npn \simifilm_fakesc:n #1
{
\use:c { protected@edef } \l__simifilm_fakesc_tl { #1 }
\regex_replace_all:NnN
\c_simifilm_lowercase_regex % search any run of lowercase letters
{ \c{simifilm_reduce:n}\cB\{\1\cE\} } % replace it with \simifilm_reduce:n{<run>}
\l__simifilm_fakesc_tl
\tl_use:N \l__simifilm_fakesc_tl
}
\cs_new_protected:Npn \simifilm_reduce:n #1
{
{ \fontsize{ \fp_eval:n { .8 * \use:c { f@size } } }{0}\selectfont \text_uppercase:n {#1} }
}
% update the constant for accommodating the accented characters you need
\regex_const:Nn \c_simifilm_lowercase_regex { ( [ a-z é ]+ ) }
\ExplSyntaxOff
\begin{document}
\film{Stagecoach} is a great movie
\sffamily
\film{La Chevauchée fantastique} is the French title
\end{document}
Accented characters are supported, but you need to add the needed ones in the search regex.
\f@family
.