[Tex/LaTex] How to test for the current font

conditionalsfonts

The situation is the following, I have one font a with one feature and another font b with another feature and I am writing a command that if the current font is a need to switch to font b but if the font is c needs to do nothing. Basically:

if currentfont is a set font b

How can this be accomplished?


EDIT:
Okey I will admit that I asked this question because I was working on a solution for this question and I thought this if else question was a separate question (was I wrong?) but now I am stuck for real… I have spent some time trying to follow the suggestions from the answers here but I keep failing. This example:

\documentclass[oneside]{memoir}
    \usepackage[osf]{mathpazo}
    \usepackage{helvet}
    \usepackage{ifthen}

    \makeatletter
    \renewcommand*{\thechapter}{%
        \ifthenelse{%
            \equal{\f@family}{pplj}%
    }%    
    {%
        {\fontfamily{ppl}\selectfont%
        \textsc{\roman{chapter}}}%
    }%
    {%
        \textsc{\roman{chapter}}%
    }%
}%
\makeatother
\renewcommand{\cftchapterpagefont}{\fontfamily{pplj}\selectfont\bfseries}
\renewcommand{\cftsectionpagefont}{\fontfamily{pplj}\selectfont}
\captionnamefont{\sffamily\scshape\small}
\captiontitlefont{\sffamily\small}

\begin{document}

\tableofcontents

\chapter{The number 4 is more than 2}

\section{The number 4 is}
    If I write chapter \ref{anotherChapter} like this is
    looks oki.
    But the number in the header is a small i not a \textsc{i} and same
    thing in the table of content and the chapter header\dots
\begin{figure}
    \caption{Lorem ipsum}
    \center \LARGE LOREM IPSUM
\end{figure}
\section{More than 4}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur
tempus facilisis nunc, sit amet suscipit ligula fermentum ac. Ut
fringilla, elit eget facilisis venenatis, risus massa viverra sem, a
placerat massa odio a mauris
\chapter{Another chapter}
\label{anotherChapter}
\end{document}

Gives this error (edited):

! Undefined control sequence.
<argument> \equal 
                  {\f@family }{pplj}
l.29 \chapter{The number 4 is more than 2}

?

Seems I have gotten myself into a lot of trouble using these old style numbers… What am I doing wrong?

Best Answer

You can access the current font using the \font command, i.e. using \the\font which expand to e.g. \OT1/cmr/m/n/10 for the normal Computer Modern font. You could save the expansion texts of font a and the compare it to the result of \the\font.

Using TeX if-switches:

% switch to font 'a'
% Save expansion text:
\edef\fonta{\the\font}
%
\makeatletter
\newcommand*{\iffonta}{%
   \begingroup
   \edef\currentfont{\the\font}%
   \ifx\currentfont\fonta
      \endgroup
      \expandafter\@firstoftwo
   \else
      \endgroup
      \expandafter\@secondoftwo
   \fi
}
\makeatother

Then you can use \iffonta{<yes>}{<no>} afterwards. Alternatively simply add the required code directly in the macro and don't use the \expandafter\@firstoftwo/\expandafter\@secondoftwo part:

\newcommand*{\changetofontaiffontb}{%
   \begingroup
   \edef\currentfont{\the\font}%
   \ifx\currentfont\fonta
      \endgroup
      % change to font b
   \else
      \endgroup
   \fi
}
Related Question