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:


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?

  • A minor nit: I believe you should use thickspaces rather than thinspaces around \middle\vert, i.e, you should write \;\middle|\;.
    – Mico
    Commented Jan 19, 2016 at 13:24
  • Why not \newcommand{\cProb}[2]{\operatorname{P}\left[ #1 \mid #2 \right]} \newcommand{\argdot}{\, \cdot}?
    – Sigur
    Commented 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
    Commented Jan 19, 2016 at 13:57
  • the thickspace seems too wide (in my opinion)
    – ndrizza
    Commented 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
    Commented Jan 19, 2016 at 16:11

2 Answers 2


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


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



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






enter image description here


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

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

\def\myspace@i{\ifx\myspace@token\myspace\else\thinspace\fi }



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

$x\myspace y$





You must log in to answer this question.

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