Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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 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 at 10:55

2 Answers 2

up vote 7 down vote accepted
\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}

enter image description here

share|improve this answer

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!

share|improve this answer
    
Well, I guess this is a better way indeed. –  egreg Aug 18 at 9:16
1  
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 at 10:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.