26

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}
lockstep
  • 250,273
Peter Grill
  • 223,288
  • An old thread, but since it shows up every now and then, I don't think you really want to automatically resize \abs{\hat{T}} like this (click) – egreg Sep 09 '22 at 09:31
  • @egreg: Totally agree. At the time it seemed like a good idea, but in hindsight it wasn't. However, the tricks one learns, even when it isn't really a good idea to do it, do come in handy sometimes. – Peter Grill Sep 12 '22 at 06:46

5 Answers5

30

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.)

Caramdir
  • 89,023
  • 26
  • 255
  • 291
8

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

\def\abs#1{\left\lvert#1\right\rvert}
  • 3
    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 05: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 –  Aug 11 '11 at 14:09
3

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...)

aulste
  • 309
2

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}
Marco Daniel
  • 95,681
2

You can patch the internal definition

\documentclass[12pt]{article}

\usepackage{mathtools}
\usepackage{etoolbox}


\DeclarePairedDelimiter{\avg}{\langle}{\rangle}

\newcommand\swapifbranches[3]{#1{#3}{#2}}
\makeatletter
\MHInternalSyntaxOn
\patchcmd{\DeclarePairedDelimiter}{\@ifstar}{\swapifbranches\@ifstar}{}{}
\MHInternalSyntaxOff
\makeatother

\DeclarePairedDelimiter{\mean}{\langle}{\rangle}


\begin{document}

$\avg{\displaystyle\sum_0^1}$  $\avg*{\displaystyle\sum_0^1}$

$\mean{\displaystyle\sum_0^1}$ $\mean*{\displaystyle\sum_0^1}$

\end{document}

the result

  • Isn't that going to change all subseuent macros that use the start option? I only want to change \abs, not all macros that have a starred variant? – Peter Grill Jun 19 '17 at 01:25
  • @PeterGrill it will patch the \DeclarePairedDelimiter macro in such a way that it will define macros whose starred and non-starred behaviour swapped compared to macros defined by the \DeclarePairedDelimiter before the patch. – user1420956 Jun 19 '17 at 08:17
  • 1
    I feel as if that is going to lead to lots on potential confusion. Might be better to define a separate \DeclareSwappedPairedDelimeter so that the definitions are not dependent on where they occur in the file(s). – Peter Grill Jun 19 '17 at 10:36