2

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
    Why should it work? – egreg Jan 29 '15 at 17:12
  • 1
    @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
8

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

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.