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?

2 Answers 2

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.

6
  • 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... Commented Mar 6, 2014 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. Commented Mar 6, 2014 at 13:28
  • These are right only if not changed: you can't be sure that they'll always expand in the desired way Commented Mar 6, 2014 at 13:57
  • @JosephWright Depends what's "right". They'll show if the current setting was accomplished by \slshape. Commented Mar 6, 2014 at 14:31
  • @tohecz Yes, but not if the current setting has absolute code sl, for example: depends what you want to test for Commented Mar 6, 2014 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

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.