2

Building on this question and this one and this one, I'd like to make a new command that has a default behavior for a single primary argument but has two additional arguments that specify (i) bracket size and (ii) subscript. For example, I have a command

\newcommand{\prob}[2][]{\mathsf{P} \ifx\\#1\\\else_{#1}\fi \!\left[#2\right]}

for specifying probability operator that I use like \prob{X\leq x} or \prob[X\sim F]{X\leq x}, but I'd like to modify the bracket size as well. Is there a nice way to do this with \DeclarePairedDelimiterXPP? Ideally I would use like: \prob_{X\sim F}[\big]{X\leq x} and equivalently \prob[\big]_{X\sim F}{X\leq x}.

One approach (but requires order and specifying all arguments):

\DeclarePairedDelimiter{\LR}{[}{]}
\DeclareDocumentCommand \prob { o m m } {% optional mandatory mandatory: https://tex.stackexchange.com/questions/1742/automatic-left-and-right-commands
  \IfNoValueTF {#1} {%
    \mathsf{P}_{#2}\LR{#3}%
  }{%
    \mathsf{P}_{#2}\LR[#1]{#3}%
  }%
}
rrrrr
  • 503

1 Answers1

2

I guess that a good syntax might be

\prob{a=1}                % no subscript, standard size
\prob[\big]{a=1}          % no subscript, \big size
\prob*{a=\frac{1}{2}}     % no subscript, automatic sizing
\prob_{b}{a=1}            % subscript, standard size
\prob_{b}[\big]{a=1}      % subscript, \big size
\prob_{b}*{a=\frac{1}{2}} % subscript, automatic sizing

because this is the order in which the various elements naturally appear.

It is not necessary to specify all arguments, because \prob can test for the subscript and then pass control to another command that typesets the rest grabbing its argument using \DeclarePairedDelimiter.

\documentclass{article}
\usepackage{mathtools}

\DeclarePairedDelimiter{\probargument}{[}{]} \NewDocumentCommand{\prob}{e{}}{% \mathsf{P}\IfValueT{#1}{{#1}}\probargument }

\begin{document}

\begin{gather} \prob{a=1} \ % no subscript, standard size \prob[\big]{a=1} \ % no subscript, \big size \prob{a=\frac{1}{2}} \ % no subscript, automatic sizing \prob_{b}{a=1} \ % subscript, standard size \prob_{b}[\big]{a=1} \ % subscript, \big size \prob_{b}{a=\frac{1}{2}} % subscript, automatic sizing \end{gather}

\end{document}

enter image description here

With some more xparse trickery, you can have an argument like {a=1|b=1} to denote conditional probability. Spaces around | are ignored.

\documentclass{article}
\usepackage{mathtools}

\DeclarePairedDelimiterX{\probargument}[1]{[}{]}{\probargumentA{#1}} \NewDocumentCommand{\prob}{e{}}{% \mathsf{P}\IfValueT{#1}{{#1}}\probargument } \NewDocumentCommand{\probargumentA}{>{\SplitArgument{1}{|}}m}{% \probargumentB#1% } \NewDocumentCommand{\probargumentB}{mm}{% \IfNoValueTF{#2}{% not conditional #1% }{% conditional #1;\delimsize|;#2% }% }

\begin{document}

\begin{gather} \prob{a=1} \ % no subscript, standard size \prob[\big]{a=1} \ % no subscript, \big size \prob{a=\frac{1}{2}} \ % no subscript, automatic sizing \prob_{b}{a=1} \ % subscript, standard size \prob_{b}[\big]{a=1} \ % subscript, \big size \prob_{b}{a=\frac{1}{2}} \ % subscript, automatic sizing \prob{a=1|b=1} \ % no subscript, standard size \prob[\big]{a=1|b=1} \ % no subscript, \big size \prob{a=\frac{1}{2}|b=1} \ % no subscript, automatic sizing \prob_{b}{a=1|b=1} \ % subscript, standard size \prob_{b}[\big]{a=1|b=1} \ % subscript, \big size \prob_{b}{a=\frac{1}{2}|b=1} % subscript, automatic sizing \end{gather}

\end{document}

enter image description here

egreg
  • 1,121,712