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?

jub0bs
  • 58,916

2 Answers2

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.

yo'
  • 51,322
  • 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 06 '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 06 '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 06 '14 at 13:57
  • @JosephWright Depends what's "right". They'll show if the current setting was accomplished by \slshape. – yo' Mar 06 '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 06 '14 at 14:34
  • For information, I wanted to test whether the font shape was either italic or slanted, and apply some italic correction in that case. I don't really care what the names of those shapes are, but I wanted a robust solution. I ended up defining a long macro based on \f@shape and \ifx comparing it to both \itdefault and \sldefault. Seems to work fine. – jub0bs Mar 07 '14 at 10:22
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