4

I'm trying to create a few commands to handle conditional probabilities. So far I am using the mathtools DeclarePairedDelimiter family of commands. Also, as far as I am aware, mathtools does not allow for breaking lines in the middle of the expressions, during an align environment for instance.

I want to use the convenience of DeclarePairedDelimiterXPP and at the same time, be able to do things manually when I need to span multiple lines. However, I want the spacing to be consistent, regardless of the style that I am using.

So far, I have used this definitions:

\newcommand{\given}{\vert}
\newcommand{\prob}{\mathbb{P}}
\DeclarePairedDelimiterXPP{\Prob}[1]
 {\prob}{\lparen}{\rparen}{}
 {\renewcommand{\given}{\nonscript\;\delimsize\vert\nonscript\;\mathopen{}}#1}

But if I do this, I would like to have exactly the same result if I try these three commands

\begin{align*}
  & \Prob{X \given Y}
  && \Prob*{X \given \sum Y}
  && \Prob[\big]{X \given \sum Y} \\
  & \prob(X \given Y)
  && \prob\left(X \middle\given \sum Y \right)
  && \prob\bigl( X \bigm\given \sum Y \bigr)
\end{align*}

Example

Actually, I care a bit less about the middle case, since I'm probably never going to use \left, \middle and \right again.

If I redefine \given to be \mid, then the first case seems to work, but \mid is not a delimiter, so then I cannot do \bigm\given. Of course I could use \mid or \vert, depending on the case, but I'm trying to create semantic commands.

Is there a way to define \given in such a way that it has the spacing of \mid, but if it is preceded by \bigm or \Bigm for instance, it behaves as a delimiter?

I'm happy to hear any suggestion on how to deal with this issue.

Also, I would like to know whether it makes any difference in this case to define \prob using \newcommand or \DeclareMathOperator, and which one would be advisable and why. I can't see any visible difference in the output.

My best regards.

3
  • 1
    I don't understand where the problem is: the first line (using the re-definition of \given) is fine. Why don't you stick to this way?
    – Bernard
    Feb 8, 2021 at 17:27
  • I want to use both \prob and \Prob, since \Prob does not handle line breakes and that is very common for me. My issue is that \Prob{ X \given Y} and \prob{X \given Y} have different outputs. Feb 8, 2021 at 18:09
  • In this case, I can't see another solution than adding the spacing by hand.
    – Bernard
    Feb 8, 2021 at 19:17

2 Answers 2

5

A couple of hand corrections in the middle column but...

enter image description here

\documentclass{article}

\usepackage{mathtools,amsfonts}
    \DeclareMathDelimiter{\given}
      {\mathbin}{symbols}{"6A}{largesymbols}{"0C}

\newcommand{\prob}{\mathbb{P}}
\DeclarePairedDelimiterXPP{\Prob}[1]
 {\prob}{\lparen}{\rparen}{}
 {\renewcommand{\given}{\;\delimsize\vert\nonscript\;\mathopen{}}#1}
\begin{document}

\begin{align*}
  & \Prob{X \given Y}
  && \Prob*{X \given \sum Y}
  && \Prob[\big]{X \given \sum Y} \\
  & \prob(X \given Y)
  && \prob{\left(X \;\middle\vert\; \sum Y \right)}
  && \prob\bigl( X \bigm\vert \sum Y \bigr)
\end{align*}
\end{document}
5
  • Thank you! This is pretty much what I was hoping for. Feb 8, 2021 at 23:41
  • Just out of curiosity, what are these parameters "6A and "0C? I notice that changing them give different characters, but is there a easy way what are the codes for a given symbol? Feb 9, 2021 at 0:01
  • @victorsouza I just copied the definition of \lvert but used \mathbin instead of \mathopen as the default math class so your simple X\given Y worked. So the numbers are actually just copies but they are the hexadecimal slots for the base | in the symbols font and the first of the series of large | in the largesymbols extension font. So these numbers are right for amsfonts but if you use a different font set you may need to look what slots \lvert uses. Feb 9, 2021 at 0:06
  • @DavidCarlisle Hi. There are two commands \delimsize and \nonscript, which I don't understand in your answer. Please can you explain what they are for? Are they part of a package?
    – Celdor
    Dec 10, 2021 at 11:54
  • Just in case someone else has the same questions, I found an explanation on \nonscript in another TeX question. On the other hand, I had to do small tests to understand \delimsize. It seems it scales inside math operators (such as \vert) to have the same size as delimiters.
    – Celdor
    Dec 11, 2021 at 10:42
4

Let me present a different solution using semantex (disclaimer: I am the author). It works as expected except in the case of auto-scaled parentheses (if you want to make this work, you can write \Prob[par=auto,break]{ X \vphantom{\sum} } instead):

\documentclass{article}

\usepackage{semantex,amsfonts}

\NewVariableClass\MyVar[
    define keys={
        {break}{ right par=. , do output=false },
        {continue}{ return, symbol={}, left par=. },
    },
]

\NewObject\MyVar\Prob{\mathbb{P}}[ arg keyval=false ]

\newcommand\givencommand[1]{
    \nonscript\:
    #1\vert
    \allowbreak
    \nonscript\:
    \mathopen{}
}
\NewObject\MyVar\given{ \givencommand{\SemantexDelimiterSize} }

\begin{document}

\begin{align*}
  & \Prob{X \given Y}
  && \Prob[par=auto]{X \given \sum Y}
  && \Prob[par=\big]{X \given \sum Y} \\
  & \Prob[break]{ X \given } \Prob[continue]{ Y }
  && \Prob[par=auto,break]{ X } \Prob[par=auto,continue]{ \given \sum Y }
  && \Prob[par=\big,break]{ X } \Prob[par=\big,continue]{ \given \sum Y }
\end{align*}

\end{document}

enter image description here

2
  • 1
    Interesting package this semantex. I've been creating my own 'library' of semantic commands! Feb 16, 2021 at 14:22
  • @victorsouza Glad you like it! You should be aware, though, that the LaTeX3 team officially disagrees with one part of the interface, namely its use of optional arguments in {}. They find them extremely unnatural, whereas I find them completely natural – and completely necessary for the semantex interface to work. It is up to yourself who you choose to agree with. :-)
    – Gaussler
    Feb 16, 2021 at 15:05

You must log in to answer this question.

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