# Manually resize a grouping macro

I know there's a braket package that will probably do all this for me but I've defined my own macro for matrix elements in Dirac notation:

\makeatletter
\def\me{\@ifstar\@me\@@me}
\def\@me#1#2#3{\ensuremath{\left\langle#1\middle|#2\middle|#3\right\rangle}}
\def\@@me#1#2#3{\ensuremath{\langle#1|#2|#3\rangle}}
\makeatother


The name could be better, I agree. Anyway, when I use the starred version with a daggered operator, I get horrible output:

Now I was wondering if there's a way to introduce the effects of \big and the like on such a command, so I can manually adjust the symbols when necessary. I was thinking of introducing an optional argument to control the size with and my current solution

\makeatletter
\def\me{\@ifstar\@me{\@ifnextchar\bgroup{\@@@me}{\@@me}}}
\def\@me#1#2#3{\ensuremath{\left\langle#1\middle|#2\middle|#3\right\rangle}}
\def\@@me#1#2#3{\ensuremath{\langle#1|#2|#3\rangle}}
\def\@@@me#1#2#3[#4]{\ensuremath{#4\langle#1#4|#2#4|#3#4\rangle}}
\makeatother


works, but I was wondering if a more elegant way exists (what I've done feels a bit like using icewater in a plastic bag to cool your laptop - it works, but it looks a little ridiculous and you can't help thinking it might be a bad idea).

Edit: Okay, it doesn't actually work. The unstarred version with the optional argument works and the starred version also still works, but the unstarred version without the optional argument gives an error.

• \DeclarePairedDelimiterX from mathtools provides \me{..}, \me*{..}, \me[\big]{..}, \me[\Big]{..}, etc. – Manuel Feb 6 '15 at 11:12

## 1 Answer

Please always post test documents not fragments.

I think you were nearly at the first form below, but probably I'd use the second which doesn't use \ensuremath and does take care to use the open and close forms not just \big\langle which will not get the same spacing.

\documentclass{article}

\makeatletter
\def\me{\@ifstar\@me{\@ifnextchar[{\@@@me}{\@@me}}}
\def\@me#1#2#3{\ensuremath{\left\langle#1\middle|#2\middle|#3\right\rangle}}
\def\@@me#1#2#3{\ensuremath{\langle#1|#2|#3\rangle}}
\def\@@@me[#1]#2#3#4{\ensuremath{#1\langle#2#1|#3#1|#4#1\rangle}}

\newcommand\xme[4][\relax]{%
\ifx#1\relax\langle\else
\ifx*#1\left\langle
\else\mathopen{#1\langle}%
\fi\fi
#2%
\ifx#1\relax\mathrel{|}\else
\ifx*#1\middle|%
\else\mathrel{#1|}%
\fi\fi
#3%
\ifx#1\relax\mathrel{|}\else
\ifx*#1\middle|%
\else\mathrel{#1|}%
\fi\fi
#4%
\ifx#1\relax\rangle\else
\ifx*#1\right\rangle
\else\mathclose{#1\rangle}%
\fi\fi
}
\makeatother

\begin{document}

$\me{a}{B^B}{c}$

$\me*{a}{B^B}{c}$

$\me[\Bigg]{a}{B^B}{c}$

$\xme{a}{B^B}{c}$

$\xme[*]{a}{B^B}{c}$

$\xme[\Bigg]{a}{B^B}{c}$

\end{document}

• Thanks, this works like I want it to. I'm not sure I like the spacing from the open and close forms though. There's a bit too much of it around the verts and too little after the \langle and before the \rangle. As an example, the LHS in the following image uses \big while the RHS uses \xme[\big]{.}{.}{.}: i.imgur.com/uoVkqZx.png . The spacing on the LHS is too narrow but consistent, whereas the spacing on the RHS is wider but inconsistent. This looks off to me. How would you adjust that? – Wouter Feb 6 '15 at 12:27
• @Wouter well the unscaled \langle and \rangle are automatically \mathopen and \mathclose \Big loses that and makes them \mathord so normally you use \bigl and \bigr which are exactly \mathopen{\big#1} and \mathclose{#1} You can always add some \! spacing if you prefer but making the horizontal space match the vertical scale can be tricky as you don't have direct control over that it is handled by the font metrics – David Carlisle Feb 6 '15 at 12:33
• Hmm, I tried using \mathrel and \mathbin instead of \mathopen. Even though that's not what they're for, they did make the spacing more consistent, but still too wide for my taste. Oh well, I'll figure something out, maybe I'll try with some manual spacing like you say. Thanks again ;) – Wouter Feb 6 '15 at 13:03
• @DavidCarlisle -- any way to make these use \B|bigl and \B|bigr which should improve the spacing? – barbara beeton Feb 6 '15 at 16:47
• @barbarabeeton that is exactly what they do do (or at least they use \mathopen{\Big\langle} and \mathclose{\Big\rangle} which is the same thing but makes it easy to use input of [\Big] – David Carlisle Feb 6 '15 at 16:52