5

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

enter image description here

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:

enter image description here

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
4
  • 2
    Does your document employ only \textsc, or does it also employ \scshape?
    – Mico
    Mar 24, 2021 at 19:54
  • In my specific document, I would need it to work only with \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 of sc.
    – schoekling
    Mar 24, 2021 at 19:59
  • 1
    I added an alternative approach to both my first solution and your solution, using the \etoolbox package.
    – TiMauzi
    Mar 24, 2021 at 23:37
  • @TiMauzi That solved the issue in the headings, thank you! Have you tried compiling in pdfLaTeX? It works fine for me. XeLaTeX can't handle \lsstyle, so that's expected to fail. One last thing: The etoolbox solution somehow doesn't do it's job in my \scshaped 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!
    – schoekling
    Mar 25, 2021 at 0:13

1 Answer 1

6

Please take a look at the following MWE:

\documentclass{scrartcl}

\usepackage{ifthen}

\makeatletter
\newcommand{\ifscshape}[2]{%
    \ifthenelse{\equal{\f@shape}{sc}}{#1}{#2}%
}
\makeatother

\newcommand{\cec}{\ifscshape{\uppercase{Cec}}{\textsc{Cec}}}

\begin{document}
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.}
\end{document}

I used the package ifthen here, comparing \f@shape (with the current font shape as output) to the string sc by using the \equal command. Within your text, you should put {} after the \cec command, so a space is placed after your emphasized text.

Note: Within the command \ifscshape, I changed your original first argument CEC to \uppercase{Cec} for better presentation and distinction from \textsc. It works either way, though.

Edit 1:

If the command should be working within sectioning commands, the usage of the package etoolbox is the better alternative:

\documentclass{scrartcl}

\usepackage{etoolbox}

\makeatletter
\newcommand{\ifscshape}[2]{%
    \ifdefstring{\f@shape}{sc}{#1}{#2}%
}
\makeatother

\newcommand{\cec}{\ifscshape{CEC}{\textsc{Cec}}}

\begin{document}
\section{The Acronym \cec}
This is the section's content using \cec{} or \textsc{also \cec}.
\end{document}

Instead of \ifthenelse, the command \ifdefstring is used, comparing the content of a command to a given string. In general, this approach seems to be more stable than the approach with the \ifthen package. Of course, a small caps variant should be supported by the used section font.

Edit 2:

Adjusting the second solution to the second MWE of @schoekling, the following should work:

\documentclass{scrbook}

\usepackage{microtype,xspace,libertine}

\setkomafont{disposition}{\normalcolor\rmfamily}
\addtokomafont{chapter}{\scshape\lsstyle}

\usepackage{etoolbox}
\makeatletter
\newcommand{\scacronym}[1]{%
    \ifdefstring{\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}

Output for the 3rd MWE.

Note: This approach seems to work if the document is compiled with LuaLaTeX, but not with e.g. XeLaTeX or PDFLaTeX.

8
  • 1
    Exactly what i was looking for! :D I originally forgot about the {} because I used \xspace in my original document, whoops. I also just realized I could make the macro more emph-like and skip the whole \cec step by modifying your macro to \newcommand{\scacronym}[1]{\ifthenelse{\equal{\f@shape}{sc}}{\uppercase{#1}}{\textsc{#1}}\xspace}. Thanks!
    – schoekling
    Mar 24, 2021 at 21:42
  • 1
    @schoekling To be honest, I first had your "modified" answer in mind. I changed it, so it fits your definition better - but you figured out my original approach myself perfectly then.
    – TiMauzi
    Mar 24, 2021 at 21:44
  • 1
    Sorry, it looks like I got excited there too early... The macro doesn't work in sectioning commands, any idea why?
    – schoekling
    Mar 24, 2021 at 22:39
  • 1
    @schoekling It seems like the \ifthen package isn't as stable as e.g. the \etoolbox package when it comes to usages outside of plain texts. See this: tex.stackexchange.com/a/13867/205359
    – TiMauzi
    Mar 24, 2021 at 23:40
  • 1
    Note that there are occasional pitfalls around the various \f@ macros lurking. Sometimes the command is defined with \newcommand and sometimes with \newcommand*, sometimes a \@empty will be added to the definition. I dealt with a similar issue in March and ended up creating commands (using xparse and expl3 syntax which I wrote about at preppylion.com/identifying-the-current-font
    – Don Hosek
    May 4, 2021 at 22:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .