# Super and subscripts with \DeclarePairedDelimiter

I defined a macro for the supremum norm by

\DeclarePairedDelimiter{\supnorm}{\|}{\|_\infty}


which works nicely. However, as soon as I want to place a superscript like

\supnorm{f}^2


things look funny, as the 2 appears after the infty-sign. However, I would like to look things more like what you get by

\|f\|_\infty^2


Of course, one can achieve this by using

\newcommand{\supnorm}[1]{\|#1\|_\infty}


but then one looses the nice functionality of \DeclarePairedDelimiter, which I really started to like a lot. Any ideas how one can combine the benefits of both?

## Update:

Now with all the options provided by \DeclarePairedDelimiter! See the new definition (without using xparse):

\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}
\makeatletter
\newcommand{\@supnormstar}[1]{\norm*{#1}_\infty}
\newcommand{\@supnormnostar}[2][]{\norm[#1]{#2}_\infty}
\newcommand{\supnorm}{\@ifstar\@supnormstar\@supnormnostar}
\makeatother


For example, the following code:

$\supnorm{\frac{\sqrt5-1}{2}}^2$
$\supnorm*{\frac{\sqrt5-1}{2}}^2$
$\supnorm[\big]{\frac{\sqrt5-1}{2}}^2$


will give you:

• Bad idea, you loose the options provides by \DeclarePairedDelimiter, and always scaling fences is a bad idea. – daleif Oct 7 '13 at 8:08
• @daleif: good idea, answer've been updated – Francis Oct 7 '13 at 8:51

You need to get the \infty outside of the grouping. And that will involve redoing the options:

\documentclass[a4paper]{memoir}
\usepackage{mathtools,xparse}
\DeclarePairedDelimiter{\supnormX}{\lVert}{\rVert}
\DeclareDocumentCommand\supnorm{ s o m }{%
\IfBooleanTF{#1}{% starred
\supnormX*{#3}_\infty
}{% not starred
\IfNoValueTF{#2}{% no []
\supnormX{#3}_\infty
}{% data in []
\supnormX[#2]{#3}_\infty
}
}
}
\begin{document}
$\supnorm{A}^2$
\end{document}

• you're listed as one of the maintainers of mathtools. may i suggest that generalizing the definition of \DeclarePairedDelimiter there would be a worthy project. – barbara beeton Oct 7 '13 at 13:31
• Hmm, I'm a bit reluctant to make it depend on xparse and also, why only subscripts..... I'll think about it. – daleif Oct 7 '13 at 17:05
• hmmm. i didn't mean to imply subscripts only -- certainly superscripts should be dealt with, and i'm not sure whether there are other variations that might be relevant. and i understand why you might want to steer clear of xparse at this moment (and don't disagree). thinking is good. thanks. – barbara beeton Oct 7 '13 at 17:42

Here's a different implementation of \DeclarePairedDelimiter for which I use a different name.

\documentclass[a4paper]{memoir}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareDocumentCommand{\xDeclarePairedDelimiter}{mmmO{}}
{
\NewDocumentCommand{#1}{sO{}m}
{
\IfBooleanTF{##1}
{
\egreg_paired_delimiter_expand:nnnn {#2}{#3}{##3}{#4}
}
{
\egreg_paired_delimiter_fixed:nnnnn {##2}{#2}{#3}{##3}{#4}
}
}
}
\cs_new_protected:Npn \egreg_paired_delimiter_expand:nnnn #1 #2 #3 #4
{% Fix the spacing issue with \left and \right (D. Arsenau, P. Stephani and H. Oberdiek)
\mathopen{}
\mathclose\c_group_begin_token
\left#1
#3
\group_insert_after:N \c_group_end_token
\right#2
\tl_if_empty:nF {#4} { \c_math_subscript_token {#4} }
}
\cs_new_protected:Npn \egreg_paired_delimiter_fixed:nnnnn #1 #2 #3 #4 #5
{
\mathopen{#1#2}#4\mathclose{#1#3}
\tl_if_empty:nF {#5} { \c_math_subscript_token {#5} }
}
\ExplSyntaxOff

%% the final optional argument to \xDeclarePairedDelimiter
%% is a subscript to the right fence
\xDeclarePairedDelimiter{\supnormX}{\lVert}{\rVert}[\infty]

\begin{document}
$\supnormX{A}^2\quad \supnormX[\big]{A}^2\quad \supnormX[\Big]{A}^2\quad \supnormX[\bigg]{A}^2\quad \supnormX[\Bigg]{A}^2\quad \supnormX*{\frac{A}{2}}^2$
\end{document}


Since daleif has shown interest in extending this idea, I add a way to define the macros with a key-value interface. The main (internal) functions remain the same.

\documentclass[a4paper]{memoir}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareDocumentCommand{\KDeclarePairedDelimiter}{mm}
{
\__egreg_delimiter_clear_keys: % reset to the default
\keys_set:nn { egreg/delimiters } { #2 }
\use:x % we want to expand the values of the token variables set with the keys
{
\exp_not:n {\NewDocumentCommand{#1}{sO{}m} }
{
\exp_not:n { \IfBooleanTF{##1} }
{
\exp_not:N \egreg_paired_delimiter_expand:nnnn
{ \exp_not:V \l_egreg_delimiter_left_tl }
{ \exp_not:V \l_egreg_delimiter_right_tl }
{ \exp_not:n { ##3 } }
{ \exp_not:V \l_egreg_delimiter_subscript_tl }
}
{
\exp_not:N \egreg_paired_delimiter_fixed:nnnnn
{ \exp_not:n { ##2 } }
{ \exp_not:V \l_egreg_delimiter_left_tl }
{ \exp_not:V \l_egreg_delimiter_right_tl }
{ \exp_not:n { ##3 } }
{ \exp_not:V \l_egreg_delimiter_subscript_tl }
}
}
}
}

\keys_define:nn { egreg/delimiters }
{
left      .tl_set:N = \l_egreg_delimiter_left_tl,
right     .tl_set:N = \l_egreg_delimiter_right_tl,
subscript .tl_set:N = \l_egreg_delimiter_subscript_tl,
}

\cs_new_protected:Npn \__egreg_delimiter_clear_keys:
{
\keys_set:nn { egreg/delimiters } { left=.,right=.,subscript={} }
}

\cs_new_protected:Npn \egreg_paired_delimiter_expand:nnnn #1 #2 #3 #4
{% Fix the spacing issue with \left and \right (D. Arsenau, P. Stephani and H. Oberdiek)
\mathopen{}
\mathclose\c_group_begin_token
\left#1
#3
\group_insert_after:N \c_group_end_token
\right#2
\tl_if_empty:nF {#4} { \c_math_subscript_token {#4} }
}
\cs_new_protected:Npn \egreg_paired_delimiter_fixed:nnnnn #1 #2 #3 #4 #5
{
\mathopen{#1#2}#4\mathclose{#1#3}
\tl_if_empty:nF {#5} { \c_math_subscript_token {#5} }
}
\ExplSyntaxOff

\KDeclarePairedDelimiter{\supnormX}{
left=\lVert,
right=\rVert,
subscript=\infty
}

\begin{document}
$\supnormX{A}^2\quad \supnormX[\big]{A}^2\quad \supnormX[\Big]{A}^2\quad \supnormX[\bigg]{A}^2\quad \supnormX[\Bigg]{A}^2\quad \supnormX*{\frac{A}{2}}^2$
\end{document}

• You missed mathtools build in fix to the \left...\right spacing. – daleif Oct 14 '13 at 13:11
• @daleif Just load mleftright and use \mleft and \mright. ;-) Seriously, I find your approach in mathtools too complicated. – egreg Oct 14 '13 at 13:49
• Then again you rely on many external packages. – daleif Oct 14 '13 at 15:50
• @daleif Fixing it with the same trick as mleftright is quite easy. I added it. – egreg Oct 14 '13 at 15:58
• Isn't that what I do in mathtools? – daleif Oct 14 '13 at 19:06