21

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}
25

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
  • 1
    So you suggest redefining every command manually? :-O – user1420956 Jun 18 '17 at 22:00
8

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

\def\abs#1{\left\lvert#1\right\rvert}
  • 2
    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 – user2478 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...)

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}
  • Please have a look at this question. – lockstep Jul 19 '11 at 16:44
0

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 1: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 8:17
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.