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}%
  }%
}
1
  • The package semantex (disclaimer: I am the author) was created more or less to support syntax like this.
    – Gaussler
    Mar 11 at 8:47

1 Answer 1

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

5
  • awesome! thanks
    – rrrrr
    Mar 10 at 21:06
  • 1
    @rrrrr I replaced () with [].
    – egreg
    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?
    – rrrrr
    Mar 10 at 21:18
  • 1
    @rrrrr Added as wished.
    – egreg
    Mar 10 at 21:29
  • wow thats amazing!!
    – rrrrr
    Mar 10 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.