4

Why does this not work?

\documentclass{article}
\usepackage{mathtools}
\usepackage{amsmath}
\begin{document}
 \newcommand{\DeclareAutoPairedDelimiter}[3]{\DeclarePairedDelimiter{#1}{\left#2}{\right#3}}
 \DeclareAutoPairedDelimiter{\p}{(}{)}  % but works fine if I say \DeclarePairedDelimiter
 $\p{2} \p*{4}$
\end{document}

The error I get is

./Test.tex:7: Extra }, or forgotten \right
./Test.tex:7: Missing } inserted
./Test.tex:7: Missing delimiter (. inserted)
./Test.tex:7: Missing delimiter (. inserted)
./Test.tex:7: Extra }, or forgotten \right
./Test.tex:7: Missing } inserted
2
  • 3
    Why should it work?
    – egreg
    Jan 29, 2015 at 17:12
  • 1
    @egreg: What do you mean? Is it not obvious that \DeclareAutoPairedDelimiter should result in the same thing as \DeclarePairedDelimiter?
    – user541686
    Jan 29, 2015 at 17:14

1 Answer 1

10

It can't work, because doing

\DeclarePairedDelimiter{\p}{\left(}{\right)}

is wrong.

You probably want something like

\newcommand{\DeclareAutoPairedDelimiter}[3]{%
  \expandafter\DeclarePairedDelimiter\csname Auto\string#1\endcsname{#2}{#3}%
  \DeclareRobustCommand{#1}{\csname Auto\string#1\endcsname*}}

You appear to share the commonly held belief that always adding \left and \right is good. It isn't.

A complete example, with a more robust version that avoids possible problems with \escapechar.

\documentclass{article}
\usepackage{mathtools}

\newcommand{\DeclareAutoPairedDelimiter}[3]{%
  \expandafter\DeclarePairedDelimiter\csname Auto\string#1\endcsname{#2}{#3}%
  \begingroup\edef\x{\endgroup
    \noexpand\DeclareRobustCommand{\noexpand#1}{%
      \expandafter\noexpand\csname Auto\string#1\endcsname*}}%
  \x}

\DeclareAutoPairedDelimiter{\p}{(}{)}

\show\p

\begin{document}
\[
\p{\frac{a}{b}}
\]
\end{document}
2
  • Oh shoot, I actually just realized I would need \lparen and \rparen if I wanted to do literally what I had (which I realize may not be a good idea, but that's not the question here)... huh, interesting. +1 thanks
    – user541686
    Jan 29, 2015 at 17:49
  • (explanation) Roughly speaking, we know that after DeclarePairedDelimiter{\Autop}{(}{)}, then \Autop*{...} will resize the delimiter, so we do that and then \DeclareRebustCommand{\p}{\Autop*} so that \p{...} expands to \Autop*{...}
    – user202729
    May 15, 2022 at 3:32

Not the answer you're looking for? Browse other questions tagged .