3

I have the command:

\newcommand{\norm}[2][]{%
\ifx\nosuchcommandipromise#1\nosuchcommandipromise
  {\lVert #2 \rVert}%
\else
  {\lVert #2 \rVert}_{#1}%
\fi
}

and I want to switch to using \DeclaredPairedDelimiter or one of its variants. But - it seems those do not allow for defining commands with optional arguments.

What am I missing? Or - how should I do this?

2
  • The code you want to be optional can be added using _{foo}, I don't see why you would need that with \DeclarePairedDelimiter.
    – Skillmon
    Jul 25, 2017 at 15:32
  • 2
    @Skillmon: I just want to be able to say \norm[\infty]{\phi}. Perfectly reasonable desire IMHO.
    – einpoklum
    Jul 25, 2017 at 16:17

1 Answer 1

5

Here is a solution: a \mynorm with two optional arguments and one mandatory: the first optional argument may be *(appended to the name of the command) or [\big] or [\Big], &c. The second optional argument is the subscript, in its ‘natural’ position (after the mandatory argument).

However, I suggest another construction with \DeclarePairedDelimiterXPP for the standard norm, so you don't have to type the subscript. I give commands for the 1-norm, the 2-norm the p-norm and the sup-norm.

\documentclass{article}
\usepackage{mathtools}
\usepackage{xparse, etoolbox}

\newcommand*{\dd}{\mathop{\kern0pt\mathrm{d}}\mkern-2mu{}}

\DeclarePairedDelimiter{\normaux}\lVert\rVert
\NewDocumentCommand\mynorm{somO{}}{%
\IfBooleanTF{#1}
{\normaux*{#3}_{#4}}%
{\IfNoValueTF{#2}{\normaux{#3}_{#4}}{\normaux[#2]{#3}_{#4}}}%
}%

\DeclarePairedDelimiterXPP\onenorm[1]{}\lVert\rVert{_1}{\ifblank{#1}{\:\cdot\:}{#1}}%
\DeclarePairedDelimiterXPP\twonorm[1]{}\lVert\rVert{_2}{\ifblank{#1}{\:\cdot\:}{#1}}
\DeclarePairedDelimiterXPP\pnorm[1]{}\lVert\rVert{_p}{\ifblank{#1}{\:\cdot\:}{#1}}
\DeclarePairedDelimiterXPP\supnorm[1]{}\lVert\rVert{_\infty}{\ifblank{#1}{\:\cdot\:}{#1}}

\begin{document}

$\mynorm{\dfrac XY}[2]\qquad\mynorm*{\dfrac XY}[2]$\bigskip

$\mynorm[\Big]{X^Y}[∞]\qquad\mynorm{X^Y}[2]\qquad\mynorm[\big]{X^Y}$\bigskip

$ \supnorm{f + g} \le \supnorm{f} + \supnorm{g}$\bigskip

$\displaystyle\pnorm{f} = \biggl(\int_0^1 f(t)\dd t \biggr)^{\!\!\frac{1}{p}}$

\end{document} 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .