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.