# new command with multiple optional arguments, one of which is bracket size

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}%
}%
}

• The package semantex (disclaimer: I am the author) was created more or less to support syntax like this. Mar 11 at 8:47

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}


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}


• awesome! thanks Mar 10 at 21:06
• @rrrrr I replaced () with []. Mar 10 at 21:06
• maybe this is another question, but would this be easily modified to allow for conditional probabilities, e.g., \condprob_{b}*{a}{c} to get P_b[a | c] where the "|" can scale with the brackets? Mar 10 at 21:18
• @rrrrr Added as wished. Mar 10 at 21:29
• wow thats amazing!! Mar 10 at 21:34