1

This question is an extension of conditionals inside newcommand with empty argument

I have a macro that looks like

\newcommand{\pcs}[3]{{#1}_{#2}^{(#3)}}

It is the third input where I would like to perform conditionals. If #3 is empty, I want nothing (not even the round brackets).

A desired output for

\begin{itemize}
\item $\pcs{a}{b}{c}$
\item $\pcs{a}{b}{}$
\end{itemize}

should look like

enter image description here

3
  • 2
    use etoolbox, then \ifblank{#3}{}{^{(#3)}} might be what you are after. Note that here I'd use ^{\smash{(}#3\smash{)}} as the ^{(...)} often gets excessively large and disturbs line spacing.
    – daleif
    Commented Sep 18, 2023 at 14:19
  • 1
    You don't need to load etoolbox with a reasonable up to date LaTeX installation. You can use \IfBlankF{#3}{^{(#3)}} instead.
    – Skillmon
    Commented Sep 18, 2023 at 14:28
  • @Skillmon I know it had something similar, just could not remember if it was public
    – daleif
    Commented Sep 18, 2023 at 14:40

1 Answer 1

5

If your last argument is only optional you might as well define it as an optional argument.

\documentclass{article}

\NewDocumentCommand \pcs { m m o } {{#1}_{#2}\IfValueT{#3}{^{(#3)}}}

\begin{document}
$\pcs{a}{b}[c]$ and $\pcs{a}{b}$
\end{document}

enter image description here

1
  • This is really useful and convenient. Thanks a lot!
    – Sayan
    Commented Sep 19, 2023 at 14:40

You must log in to answer this question.

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