2

I have the following two custom commands:

The conditional probability:

\newcommand{\cProb}[2]{\operatorname{P}\left[ #1 \,\middle|\, #2 \right]}

and a dotas a placeholder for arguments in functions:

\newcommand{\argdot}{\,\cdot\,}

Now with this setup, if i write \cProb{B}{A} it produces the follwing nice result:

\operatorname{P}\left[ A \,\middle|\, B \right]

However, if i write \cProb{\argdot}{A} there is one space too much before the conditional probability delimiter in the produced result:

\operatorname{P}\left[ \,\cdot\, \,\middle|\, B \right]

Is there a way to use a space character (other than \,) in math mode that is just counted once if it appears more than one time in a row? Or in general is there a way to produce this behaviour?

5
  • A minor nit: I believe you should use thickspaces rather than thinspaces around \middle\vert, i.e, you should write \;\middle|\;.
    – Mico
    Jan 19, 2016 at 13:24
  • Why not \newcommand{\cProb}[2]{\operatorname{P}\left[ #1 \mid #2 \right]} \newcommand{\argdot}{\, \cdot}?
    – Sigur
    Jan 19, 2016 at 13:56
  • \mid doesn't add enough space, and i need a space before and after \cdot because it may appear in other places.
    – ndrizza
    Jan 19, 2016 at 13:57
  • the thickspace seems too wide (in my opinion)
    – ndrizza
    Jan 19, 2016 at 13:58
  • @Sigur then the []'s scale, but the conditional marker (\mid) does not, that'll end up looking horrible.
    – daleif
    Jan 19, 2016 at 16:11

2 Answers 2

1

You can insert an invisible item and check for it; I add a better definition of \cProb that can be called like

\cProb{B}{A}
\cProb[\big]{B}{A}
\cProb[\Big]{B}{A}
\cProb[\bigg]{B}{A}
\cProb[\Bigg]{B}{A}
\cProb*{B}{A}

where only the last one uses \left and \right. Trust me, it's better not to have automatic delimiter size in general.

\documentclass{article}
\usepackage{mathtools}

\DeclarePairedDelimiterX{\cProbdel}[2]{[}{]}{%
  #1\maybethinspace\delimsize|\,\thinspacemarker#2%
}
\newcommand{\cProb}{\operatorname{P}\cProbdel}
\newcommand{\argdot}{\maybethinspace{\cdot}\,\thinspacemarker}

\newcommand{\maybethinspace}{%
  \ifnum\lastnodetype=3 % look whether the preceding item is a rule
  \else
    \,%
  \fi
}
\newcommand{\thinspacemarker}{\vrule width0pt\relax}

\begin{document}

$\cProb{B}{A}$

$\cProb{\argdot}{A}$

$\cProb{B}{\argdot}$

\end{document}

enter image description here

1

This command \myspace will have the desired behaviour. Not clever enough to recognize other spaces than itself, though.

\documentclass[border=12pt, varwidth]{standalone}

\makeatletter
\newcommand*\myspace{\futurelet\myspace@token\myspace@i}
\def\myspace@i{\ifx\myspace@token\myspace\else\thinspace\fi }

\makeatother
\begin{document}\thispagestyle{empty}

$x\,\,\,\,\,\,\,\,\,\,y$

$x\myspace\myspace\myspace\myspace\myspace\myspace\myspace\myspace\myspace\myspace  y$

$x\myspace y$

$x\,y$

\end{document}

Blockquote

0

You must log in to answer this question.

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