# A macro defining macro to swap starred and nonstarred \DeclarePairedDelimiter

In the accepted answer to this question a method of swapping the starred and non-starred versions of the macros from \DeclarePairedDelimiter is presented. For example, for the \abs function we could use

\makeatletter
\DeclarePairedDelimiter\tmp@abs{\lvert}{\rvert}
\def\abs{\@ifstar{\tmp@abs}{\tmp@abs*}}
\makeatother


This is a nice solution, because the results from \DeclarePairedDelimiter are much better than \left and \right (especially when it comes to spacing), and often the starred version is more desirable than the non-starred so it's best to have it as the default.

Is there a way to combine the above method into a macro that does the swapping automatically? It's not too big a deal in this case, only two lines of code per definition, but it's often considered best practice to define a function or macro to encapsulate processes that are done multiple times. In this case I would think that a suitable macro would look something like:

\makeatletter
\newcommand*\DeclarePairedDelimiterSwapped[3]{
\DeclarePairedDelimiter\tmp@delim{#2}{#3}
\def{#1}{\@ifstar{\tmp@delim}{\tmp@delim*}}
}
\makeatletter

\DeclarePairedDelimiterSwapped\abs{\lvert}{\rvert}


I could then go on to define lots of delimiters, one line each. However, the above code produces an error, and in hindsight it was obviously not going to work because \abs is taken as an input and not defined. How can I fix my definition of \DeclarePairedDelimiterSwapped so that it actually defines commands? Alternatively, let me know if this is not an easy fix.

• Trust me: you don't want to swap. Using indiscriminately autosizing delimiters is a good road to bad math typography. May 7 '21 at 20:24

You can do it. But should you? Look at \abs{\hat{f}} and judge for yourself.

\documentclass{article}
\usepackage{mathtools}

\ExplSyntaxOn
\NewDocumentCommand{\DeclarePairedDelimiterSwapped}{mmm}
{% first define \DeclarePairedDelimiter on a different command
\exp_args:Nc \DeclarePairedDelimiter { eddy @ \cs_to_str:N #1 } { #2 } { #3 }
% now define your swapped command
\NewDocumentCommand{#1}{som}
{
\IfBooleanTF { ##1 }
{% * is used, ignore the optional argument
\use:c { eddy @ \cs_to_str:N #1 } { ##3 }
}
{% no *
\IfNoValueTF { ##2 }
{% no optional argument
\use:c { eddy @ \cs_to_str:N #1 } * { ##3 }
}
{% optional argument
\use:c { eddy @ \cs_to_str:N #1 } [ ##2 ] { ##3 }
}
}
}
}
\ExplSyntaxOff

\DeclarePairedDelimiterSwapped{\abs}{\lvert}{\rvert}

\begin{document}

$\abs{\frac{1}{2}} % auto sizing \quad \abs{\hat{f}} % auto sizing \quad \abs*{\hat{f}} % no auto sizing \quad \abs[\big]{1} % big size \quad \abs[\Big]{\frac{1}{2}} % Big size$

\end{document}


This is just one of the reasons why the autosizing feature should be used with great care and not used indiscriminately.

Now that you know, use the plain \DeclarePairedDelimiter and add * only when really needed (that is, very rarely).