# Swap definition of starred and non-starred command

I don't like the fact that I am required to use \abs* when I want the vertical line of the absolute value to automatically re-size. I pretty much always want it to re-size, so would like to swap the definition of the two commands, but not sure how to do that. My attempt below is commented out as it does not compile.

I can't think of a case where I'd want to use the version that does not resize, but in case some corner case arises in the future I don't want to loose that. Or, is there a good reason to not to do this swap, and go and change all occurrences of \abs to \abs*.

\documentclass{article}

\usepackage{amsmath}
\usepackage{mathtools}

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

%\let\oldabs{\abs}%
%\let\oldabs*{\abs*}%
%\let\abs{oldabs*}%
%\let\abs*{\oldabs}%

\begin{document}
Not sure why this is the default behavior, but it is:
$\abs{\frac{1}{2}}$

Would prefer to get this behavior using \textbackslash{abs},
instead of having to use \textbackslash{abs*}.
$\abs*{\frac{1}{2}}$
\end{document}

-

You can redefine \abs to call the opposite version of the original command:

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


(The reason that your commented try doesn't work is that technically the * is not part of the macro name, but is read by the macro itself using \@ifstar, \@ifnextchar or similar commands.)

-
Very nice trick! Will have it in mind. –  Juan A. Navarro Sep 14 '11 at 9:07

simply define \abs with \def if you do not want to use the star version.

\def\abs#1{\left\lvert#1\right\rvert}

-
If I recall correctly, I think the reason I had used DeclarePairedDelimiter was that would get the correct math spacing. –  Peter Grill Jul 17 '11 at 5:25
@Peter: You're right. \left and \right cause some extra spaces but one may fix this adding \mathopen{} and \mathclose{}, see here. However, I experienced that \left and \right sometimes tend to scale |, (, [ etc. a bit too large. This is why I prefer the use of \DeclarePairedDelimiterY, see my answer, as it allows rescaling by just specifying its optional argument. –  aulste Aug 11 '11 at 14:03
@wueaul: that depends to the setting of \delimitershortfall or \delimiterfactor –  Herbert Aug 11 '11 at 14:09
Thanks for this tip :-) –  aulste Aug 11 '11 at 14:16

I've had the same problem and wrote a work-around for this: a macro \DeclarePairedDelimiterY, see this question. But note you have to replace the line

{\csname#1Temp*\endcsname{##2}}%


with

{\csname#1Temp\endcsname*{##2}}%


Then you can define \abs by

\DeclarePairedDelimiterY{abs}{\lvert}{\rvert}


where the backslash of \abs is omitted. Then:

• \abs{x} expands to \left\lvert x\right\rvert
• \abs[normal]{x} expands to \lvert x\rvert
• \abs[big]{x} expands to \bigl\lvert x\bigr\rvert
• \abs[Big]{x} expands to \Bigl\lvert x\Bigr\rvert
• \abs[bigg]{x} expands to \biggl\lvert x\biggr\rvert
• \abs[Bigg]{x} expands to \Biggl\lvert x\Biggr\rvert

In contrast to the original \DeclarePairedDelimiter command, you leave out the backslash of \big, \Big etc.

(Unluckily I didn't find your question prior to posting my own one...)

-

You can change the internal definition

\documentclass{article}

\usepackage{amsmath}
\usepackage{mathtools}

\makeatletter
\MHInternalSyntaxOn
\renewcommand*\DeclarePairedDelimiter[3]{%
\@ifdefinable{#1}{
\@namedef{MT_delim_\MH_cs_to_str:N #1 _star:}##1
{\mathopen{}\mathclose\bgroup\left#2 ##1 \aftergroup\egroup\right #3}%
\@xp\@xp\@xp
\newcommand
\@xp\csname MT_delim_\MH_cs_to_str:N #1 _nostar:\endcsname
[2][\\@gobble]
{
\mathopen{\@nameuse {\MH_cs_to_str:N ##1 l} #2} ##2
\mathclose{\@nameuse {\MH_cs_to_str:N ##1 r} #3}}
\DeclareRobustCommand{#1}{
\@ifstar
{\@nameuse{MT_delim_\MH_cs_to_str:N #1 _nostar:}}
{\@nameuse{MT_delim_\MH_cs_to_str:N #1 _star:}}
}
}
}
\MHInternalSyntaxOff
\DeclarePairedDelimiter\abs{\lvert}{\rvert}
\makeatother

\begin{document}
Not sure why this is the default behavior, but it is:
$\abs{\frac{1}{2}}$

Would prefer to get this behavior using \textbackslash{abs},
instead of having to use \textbackslash{abs*}.
$\abs*{\frac{1}{2}}$
\end{document}

-
Please have a look at this question. –  lockstep Jul 19 '11 at 16:44