5

I need to perform a test on the current font shape. More specifically, I need to detect whether the current font shape is italic or slanted.

Joseph Wright already provided a solution in his answer to Detect which text "mode" (normal, italic, bold, etc.) is currently in use?. For instance, if you define

\newcommand*{\my@test@it}{it}

then

\ifx\f@shape\my@test@it

will be true if the current font shape is italics.

However, as I'm trying to avoid code duplication as much as possible, I'm wondering whether there really is a need to define macros that expands to it (for italics), sl (for slanted), etc. I suspect there may already exist such macros, perhaps defined in the LaTeX kernel. Is that the case? If so, what are those macros called?

6

I suppose that you are looking for \bfdefault, \itdefault and stuff:

$ for x in bf it sl md up rm sf tt; do texdef -t latex ${x}default; done;

\bfdefault:
\long macro:->bx

\itdefault:
\long macro:->it

\sldefault:
\long macro:->sl

\mddefault:
\long macro:->m

\updefault:
\long macro:->n

\rmdefault:
\long macro:->cmr

\sfdefault:
\long macro:->cmss

\ttdefault:
\long macro:->cmtt

Be careful since they are all \long.

  • That looks like what I'm looking for, but \slshape\ifx\f@shape\sldefault fails. I guess it's got to do with this \sldefault being long whereas \f@shape isn't... – jub0bs Mar 6 '14 at 13:12
  • According to this comment by Joseph, I'm on the right track. I'll figure something out. I'll accept your answer. Thanks. – jub0bs Mar 6 '14 at 13:28
  • These are right only if not changed: you can't be sure that they'll always expand in the desired way – Joseph Wright Mar 6 '14 at 13:57
  • @JosephWright Depends what's "right". They'll show if the current setting was accomplished by \slshape. – yo' Mar 6 '14 at 14:31
  • @tohecz Yes, but not if the current setting has absolute code sl, for example: depends what you want to test for – Joseph Wright Mar 6 '14 at 14:34
1

Can easily be extended to what you need

\documentclass{article}
\usepackage[T1]{fontenc}
\makeatletter
\def\getFSh#1/#2/#3/#4\@nil{#4}
\def\getFSe#1/#2/#3/#4\@nil{#3}
\begin{document}
\itshape
\curr@fontshape --> \expandafter\getFSh\curr@fontshape\@nil

\slshape
\curr@fontshape --> \expandafter\getFSh\curr@fontshape\@nil

\bfseries
\curr@fontshape --> \expandafter\getFSe\curr@fontshape\@nil\relax, 
                    \expandafter\getFSh\curr@fontshape\@nil
\end{document}

enter image description here

  • Not a direct answer to my question, but still useful. Thanks. – jub0bs Mar 6 '14 at 13:08
  • @Jubobs See nfssext-cfr and nfssext for examples using this sort of approach. – cfr Mar 7 '14 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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