# Check if fontseries exists

I want to check if the actual font used has a special fontseries installed, for example:

%check for semibold
\ifx\fontseries{sb} % doesnt work
\def\textsb#1{\fontseries{sb}\selectfont #1}
\else
\let\textsb\textbf
\fi


I would appreciate an answer without use of extra package.

-
You might look at the code of nfssext or nfssext-cfr to see how the 'Font Installation' manual does this. However, that solution, unlike egreg's depends on fonts using Karl Berry names. –  cfr Aug 18 '14 at 2:09
My font and I think most fonts using Karl Berry names so also the answer from Will is pretty good and works fine. –  Tim S. Aug 18 '14 at 10:55

\documentclass{article}

\usepackage{libertine} % Libertine has sb

\makeatletter
\DeclareRobustCommand{\sbseries}{%
\@tempswafalse
\sbox\z@{%
% Disable warnings for missing fonts
\let\@font@warning\@gobble
% Save the current value of \@defaultsubs
\let\@tempsubs\@defaultsubs
% try sb
\fontseries{sb}\selectfont
% the value of \@defaultsubs might have changed
\global\let\@defaultsubs\@tempsubs
% define a temporary macro for \ifx
\escapechar\m@ne
\edef\@tempa{\string\sb}%
% get the computed current series
% see \define@newfont in latex.ltx
\expandafter\expandafter\expandafter
\split@name\expandafter\string\font@name\@nil
% compare the current series with sb
\ifx\f@series\@tempa
\aftergroup\@tempswatrue
\fi
}%
\if@tempswa
% sb exists
\fontseries{sb}\selectfont
\else
\bfseries
\fi
}
\DeclareTextFontCommand{\textsb}{\sbseries}
\makeatother

\begin{document}

abc

\textsb{abc}

\textbf{abc}

\fontfamily{cmr}\selectfont
abc

\textsb{abc}

\textbf{abc}

\end{document}


-

egreg's answer is very good and probably better than what I'm about to suggest.

\documentclass[a4paper]{article}
\usepackage{libertine}% has sb
\makeatletter
\def\checkseries#1#2{%
{\ifcsname\f@encoding/\f@family/#1/\f@shape\endcsname
\fontseries{#1}%
\else
\fontseries{\bfdefault}%
\fi
\selectfont #2}%
}
\makeatother
\begin{document}
rm \checkseries{sb}{sb?} \textbf{bf}
\par\fontfamily{cmr}\selectfont
rm \checkseries{sb}{sb?} \textbf{bf}
\end{document}


In fact, the main reason for me answering here is to query whether this method is flawed. Sometimes simpler is better, but not always!

-
Well, I guess this is a better way indeed. –  egreg Aug 18 '14 at 9:16
Both answers work well, honestly I'm not sure which one to accept.. I think egreg's because he answered earlier and used textsb but I prefer a mixed code :-) –  Tim S. Aug 18 '14 at 10:58