Super and subscripts with \DeclarePairedDelimiter
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}
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}
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: