What would be the simplest way to create a macro like \ifscshape{sc case}{else}
(similar to e.g. \ifdraft{}{}
), so that this
\newcommand{\cec}{\ifscshape{CEC}{\textsc{Cec}}}
In normal text, the acronym \cec{} should be printed in small caps.
\textsc{But if surrounded by small caps, \cec{} should be printed in uppercase.}
would output something like this
similarly to how \emph
interacts with italics?
Edit:
I tried to use \scacronym
(an adaptation from @TiMauzi's macro in their answer), which works like a charm in regular text, but breaks inside titles.
MWE
\documentclass{scrbook}
\usepackage{microtype,xspace,libertine}
\setkomafont{disposition}{\normalcolor\rmfamily}
\addtokomafont{chapter}{\scshape\lsstyle}
\usepackage{ifthen}
\makeatletter
\newcommand{\scacronym}[1]{%
\ifthenelse{\equal{\f@shape}{sc}}{\uppercase{#1}}{\textsc{#1}}\xspace}
\makeatother
\begin{document}
\chapter{Shapes \& Types of \scacronym{Cec}}
In normal text, the acronym \scacronym{Cec} should be printed in small caps.\\
\textsc{But if surrounded by small caps, \scacronym{Cec} should be printed in uppercase.}
\end{document}
When I try to compile this, I get a huge number of similar errors, some shown below. What's happening here and how can I fix this?
Edit 2:
@TiMauzi's updated solution using etoolbox
solved above issue:
However, if this macro should apply in the TOC entries and headers/footers, it's better to define it using \DeclareRobustCommand
instead of \newcommand
.
\makeatletter
\DeclareRobustCommand{\scacronym}[1]{%
\ifdefstring{\f@shape}{sc}{\uppercase{#1}}{\textsc{#1}}\xspace}
\makeatother
\textsc
, or does it also employ\scshape
?\scshape
actually. I need the uppercase variant in chapter titles, that are formatted with\addtokomafont{chapter}{\scshape\lsstyle}
. I did not think that would make a difference, so I used\textsc
for limiting the scope ofsc
.\etoolbox
package.\lsstyle
, so that's expected to fail. One last thing: Theetoolbox
solution somehow doesn't do it's job in my\scshape
d TOC entries though, and prints 'Cec' not 'CEC'. Any ideas on that? If not, maybe I should post a follow-up... But your answer definitely answers the original question, so thanks again!