# \DeclarePairedDelimiter with \left and \right

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

• Why should it work? – egreg Jan 29 '15 at 17:12
• @egreg: What do you mean? Is it not obvious that \DeclareAutoPairedDelimiter should result in the same thing as \DeclarePairedDelimiter? – Mehrdad Jan 29 '15 at 17:14

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}

• 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 – Mehrdad Jan 29 '15 at 17:49