Conditional bracketing in macro
This checks if +
or -
is in the argument.
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\cat}{om}
{
\IfValueT{#1}{ \is_cat_prefix:n { #1 } }
\mathbf{#2}
}
\cs_new_protected:Nn \is_cat_prefix:n
{
\str_if_in:nnTF { #1 } { + }
{ (#1) } % there is +
{
\str_if_in:nnTF { #1 } { - }
{ (#1) } % there is -
{ #1 }
}
}
\ExplSyntaxOff
\begin{document}
\(\cat{Bord}\), \(\cat[n]{Fus}\), \(\cat[2]{Rep}\), \(\cat[n+1]{Fus}\), \(\cat[n-k-2]{Rep}\)
\end{document}
I use listofitems
here to check for the literal presence of +
or -
in argument #1
(others can be added).
\documentclass{article}
\usepackage{listofitems}
\newcommand{\cat}[2][\relax]{%
\setsepchar{+||-}%
\readlist\checksgns{#1}%
\ifnum\listlen\checksgns[]>1\relax(#1)\else#1\fi\mathbf{#2}}
\begin{document}
\(\cat{Bord}\), \(\cat[n]{Fus}\), \(\cat[2]{Rep}\), \(\cat[n+1]{Fus}\), \(\cat[n-k-2]{Rep}\)
\end{document}