Math-Mode – How to Prevent \mathchoice from Resetting Bounds of Its Contents

bounding boxmath-modesuperscripts

I have a series of norm macros that I want to expand to \left\| ...\right\| in display math but remain as \| ... \| in inline math. Each norm has a subscript unique to the macro:

\newcommand {\normbrak} [2]                            % display/inline -aware norm brackets
    {\mathchoice{\left\|#1\right\|_{#2}}{\|#1\|_{#2}}{\|#1\|_{#2}}{\|#1\|_{#2}}}
\newcommand {\inorm}    [1] {\normbrak{#1}{1}}         % 1-norm (nuclear norm)
\newcommand {\iinorm}   [1] {\normbrak{#1}{2}}         % 2-norm (operator norm)
\newcommand {\fronorm}  [1] {\normbrak{#1}{\text{F}}}  % Frobenius norm
\newcommand {\infnorm}  [1] {\normbrak{#1}{\infty}}    % infinity norm
\newcommand {\Linorm}   [1] {\normbrak{#1}{L^1}}       % L1 norm
\newcommand {\Liinorm}  [1] {\normbrak{#1}{L^2}}       % L2 norm
\newcommand {\Linfnorm} [1] {\normbrak{#1}{L^\infty}}  % L-infinity norm

In certain places, I wish to write, e.g., \fronorm{\hat H}^{-1} to get a superscript on the norm as well. My problem is that \mathchoice appears to reset the positioning of superscripts and subscripts, yielding

bad norm superscript

instead of the proper

good norm superscript

One possible solution is to rewrite every macro as, e.g.

\newcommand {\normbrak} [3] {%
    \mathchoice
        {\left\|#1\right\|_{#2}\IfNoValueF {#3} {\sp{#3}}}
        {\|#1\|_{#2}\IfNoValueF {#3} {\sp{#3}}}
        {\|#1\|_{#2}\IfNoValueF {#3} {\sp{#3}}}
        {\|#1\|_{#2}\IfNoValueF {#3} {\sp{#3}}}%
}

\NewDocumentCommand {\fronorm} {me{^}} {%
    \normbrak{#1}{\text{F}}{#2}%
}

but this seems messy and redundant.

Is there a way to stop \mathchoice from ratching the bounds of its contents, or a macro other than \mathchoice that would work here?

Best Answer

You have to absorb the possible superscript before calling \mathchoice.

Here's a way to do it, with some fixes:

  1. use \lVert and \rVert instead of \| (try \|-x\| to see why);
  2. use \mathrm{F} instead of \text{F} (try in a theorem statement to see why).
\documentclass{article}
\usepackage{amsmath}

\NewDocumentCommand{\normbrak}{mme{^}}{%
  \mathchoice{\makenormbrak{\left}{\right}{#1}{#2}{#3}}
             {\makenormbrak{}{}{#1}{#2}{#3}}
             {\makenormbrak{}{}{#1}{#2}{#3}}
             {\makenormbrak{}{}{#1}{#2}{#3}}%
}

\NewDocumentCommand{\makenormbrak}{mmmmm}{%
  #1\lVert #3 #2\rVert_{#4}\IfValueT{#5}{^{#5}}%
}

\newcommand {\inorm}    [1] {\normbrak{#1}{1}}         % 1-norm (nuclear norm)
\newcommand {\iinorm}   [1] {\normbrak{#1}{2}}         % 2-norm (operator norm)
\newcommand {\fronorm}  [1] {\normbrak{#1}{\mathrm{F}}}% Frobenius norm
\newcommand {\infnorm}  [1] {\normbrak{#1}{\infty}}    % infinity norm
\newcommand {\Linorm}   [1] {\normbrak{#1}{L^1}}       % L1 norm
\newcommand {\Liinorm}  [1] {\normbrak{#1}{L^2}}       % L2 norm
\newcommand {\Linfnorm} [1] {\normbrak{#1}{L^\infty}}  % L-infinity norm

\begin{document}

\[
\fronorm{\hat{H}}^{-1}
\]
\begin{center}% for comparison
$\fronorm{\hat{H}}^{-1}$
\end{center}

\end{document}

enter image description here

This image, however, clearly shows why using \left and \right is very dangerous, because it leads to very oversized delimiters.

Related Question