26

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?

6
  • @jonalv: Please replace the ifthen package with etoolbox as I did in my answer.
    – lockstep
    Mar 28, 2011 at 14:22
  • @lockstep: Okay, done, that gives another error message... It still seem highly related to all the if else stuff going on though.
    – jonalv
    Mar 28, 2011 at 14:29
  • @jonalv: My example may not do what you want, but it did not give error messages. Don't cut-and-past parts of it, but try to compile it "as is".
    – lockstep
    Mar 28, 2011 at 14:37
  • @lockstep: I started by trying to run your entire example. It gave the errors explained in question. You claim it does not. Since we can't agree on whether it gives errors or not we are in trouble, (maybe we are running different versions?) and I decided to try and write a solution on my own based on the answers on this question here. Yes I picked some inspiration from you answer as well but I mainly wanted to follow up the answers to this question and try to do something with them. Perhaps I failed at that? I am sorry if I have hurt your feelings, that was not my intent.
    – jonalv
    Mar 28, 2011 at 14:44
  • @jonalv: Sorry for being snippy. I updated my example one more time and triple-checked for errors, but there are none. My "solution" (I'm testing for phv now) does approximately what you want, but the ppl font features only "faked" small caps.
    – lockstep
    Mar 28, 2011 at 14:49

2 Answers 2

12

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
}
2
  • In your example, you \edef the current font to the string \currentfont. Can that later be used as a shorthand to restore the font, if (unknown) intermediate changes have been made? Something like \font{\currentfont} ? Mar 26, 2013 at 14:02
  • @StevenB.Segletes: Restoring the font should be possible in such or a similar way. I would need to have another look at this in order to be sure. Maybe a \let is better here. Mar 26, 2013 at 16:55
18

Similar to Martin's example, and depending on what exactly you want to test for, you can do something like

\documentclass{article}
\usepackage{ifthen}
\usepackage{helvet}
\makeatletter
\newcommand{\showfont}{encoding: \f@encoding{},
  family: \f@family{},
  series: \f@series{},
  shape: \f@shape{},
  size: \f@size{}
}
\newcommand{\iffont}[3]{\ifthenelse{\equal{\f@family}{#1}}{#2}{#3}}
\makeatother
\begin{document}
\showfont

\iffont{cmr}{
  Font was cmr, switching to Helvetica.
  \renewcommand{\familydefault}{\sfdefault} \normalfont
}{
}

Here's another line of text.
\end{document}

to get

enter image description here

Change the \f@family in the \iffont command to whatever other font attribute you want to check for if needed. Reference: LaTeX2e Font Selection

7
  • @Mike Renfro: I thought I had understood why your code is working but clearly not. I think my example should work but it doesn't, why is that?
    – jonalv
    Mar 28, 2011 at 20:50
  • First, I'm not certain your \ifthenelse is braced correctly (it takes three braced arguments: a thing to test, a thing to do if true, and a thing to do if false). Second, my \showfont shows the font family as pplj, not ppl. Third, if your real goal is to get small-caps Palatino bold, then my LaTeX warns that it had to substitute a non-small-caps variant, since there's apparently no small-caps version at all. Mar 28, 2011 at 23:02
  • @Mike Renfro: Ah thanx for the tip on \ifthenelse I have fixed that now, and I switched to pplj you are right there, one letter seems to had fallen off somewhere in the process... However it's still not working for me, now it can't find the \equal control sequence. What more am I doing wrong?
    – jonalv
    Mar 29, 2011 at 8:03
  • @Mike Renfro: Oh, as for palatino small-caps bold I am looking at it printed on paper right now (and I have both bold and non bold next to each other and there is a difference) so it's very strange that you don't have them...
    – jonalv
    Mar 29, 2011 at 9:56
  • 1
    Honestly, I got lockstep's sample in your other question to run without any problems, if I understood the original problem correctly (bold small caps Roman numerals for chapter numbers, old style Arabic numbers everywhere else). If his code doesn't run for some reason, all I can recommend is making sure you don't have an ancient TeX install, and stripping his example down to the minimum possible code and seeing where the errors go away. Mar 30, 2011 at 2:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .