[Tex/LaTex] Detect which text “mode” (normal, italic, bold, etc.) is currently in use

conditionalsfontssmall-caps

In my document, I'm often setting upper case words and abbreviations in small caps because it avoids breaking the flow (which upper case words does). Also, many words (like CUDA, for instance) are also entered as commands instead of directly as text.

Up to this point I have manually set these words in small caps where appropriate (like \textsc{\cuda}), but this becomes unnecessarily repetitive. What I would like to be able to do is to define a command like \cuda which prints CUDA in small caps where the text "mode" is normal roman, and sets CUDA in upper cases if the text mode is italics. The reason for doing this is the font I'm using doesn't support small caps in italics.

Hence I would like to be able to detect which text "mode" is currently in use, and act accordingly.

I've included a minimal example below to illustrate my point:

\documentclass{article}

\usepackage{textcase}

\newcommand{\cudaString}{CUDA}
\newcommand{\cuda}{\textsc{\MakeTextLowercase{\cudaString}}}

\begin{document}
Set \cuda in small caps here,
\itshape but do not set \cuda in small caps here.
\end{document}

Best Answer

LaTeX's 'New Font Selection Scheme' (NFSS) stores information about the font in a number of macros. The macro \f@shape stores the shape: n for upright, it for italic and sc for small caps. \f@series stores the series: m for mid-weight and bx for bold, while \f@family stores the family: something like cmr for roman, cmss for sanserif and cmtt for monospaced (it's dependent on the current font).

Thus you can test for small caps using

\makeatletter
\newcommand*{\IfSmallCapsTF}{%
  \ifx\f@shape\my@test@sc
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\newcommand*{\my@test@sc}{sc}
\makeatother

 \IfSmallCapsTF{true-code}{false-code}
Related Question