1

I use \DeclarePairedDelimiter from the mathtools package a lot, and I almost always want the delimiters to be automatically resized. Therefore, I switch the definitions of the star and non-star versions of the command as described in this question. The problem is that doing that completely destroys the optional size parameter:

Here's a MWE:

\documentclass{article}

\usepackage{amsmath} \usepackage{mathtools}

\DeclarePairedDelimiter\abs{\lvert}{\rvert}

\makeatletter \let\oldabs\abs \def\abs{@ifstar{\oldabs}{\oldabs*}} \makeatother

\begin{document} [ \abs{\frac{1}{x}} \quad \abs*{\frac{1}{x}} \quad \abs[\Bigg]{\frac{1}{x}} ] \end{document}

Is there a way to swap the star and non-star definitions without breaking the size parameter?

Shai Avr
  • 715
  • 1
    The reason is that the star version don't allow optional args. After you switch the star and unstar version, in order to use the optional args, you need the star in front of that. Try \abs*[\Bigg]{\frac{1}{x}}. – Tom Jan 28 '24 at 18:57

1 Answers1

3

You can use \RenewDocumentCommand to get the desired behavior.

\documentclass{article}

% \usepackage{amsmath} % loaded by mathtools \usepackage{mathtools}

\DeclarePairedDelimiter\abs{\lvert}{\rvert} \NewCommandCopy{\oldabs}{\abs} \RenewDocumentCommand{\abs}{som}{% \IfBooleanTF{#1} {\oldabs{#3}} {% \IfNoValueTF{#2} {\oldabs*{#3}} {\oldabs[#2]{#3}}% }% }

\begin{document} [ \abs{\frac{1}{x}} \quad \abs*{\frac{1}{x}} \quad \abs[\Bigg]{\frac{1}{x}} ] \end{document}

mbert
  • 4,171