# Redefine vertical bar <|>: How to typeset negative sign correctly?

In my document I have to typeset several points with coordinates.

Example

$$P(-3|-2)$$


Output

As you can see LaTeX handles the second sign as an minus sign (binary operator) whereas it's an negative sign (unary operator). I know that a solution would be to enclose the second sign in curly braces like $$P(-3|{-}2)$$ (Source).

How can I redefine the vertical bar so that LaTeX sets the spaces correctly?

I don't want to change it manually by adding curly braces because I have hundreds of coordinates to change (collection of exercises). As far as I know I have to add \mathclose{} and \mathopen{} to the vertical bar (Source).

• you can just use \lvert – daleif Aug 11 '16 at 10:07
• I don't want to change it manually. I am looking for a redefine-command or a macro. – Sr. Schneider Aug 11 '16 at 10:07
• It may break other things, but \catcode|=\active \def|{\mathopen\vert\mathclose} – Steven B. Segletes Aug 11 '16 at 10:08
• Take it as a lesson that is it never a good idea to use | as a syntax. In most manuscripts I edit, one of my goals are: no | when I'm done. | is used for for many things that, as Steven mentions, of you need to change something for a single use, then thee will be collateral damage. – daleif Aug 11 '16 at 10:10
• So $$P(-3\lvert-2)$$ would be the official syntax? I am asking because there are several ways to typeset a vertical bar in LaTeX. – Sr. Schneider Aug 11 '16 at 10:20

You could do $$P(-3|{-2})$$, but it would be error prone.

You're better to define a macro for your coordinates, so you are not tied to a particular notation. Suppose somebody turns up ordering you to use a semicolon for separating coordinates: would you like to go through your long document and changing all bars appearing in that context to semicolons? I wouldn't. ;-)

\documentclass{article}

\newcommand{\coord}[3]{%
#1% the point's name
(#2\mathclose{}\,\vert\,\mathopen{}#3)%
}

\begin{document}

A point $$\coord{P}{-3}{-2}$$ (good)

A point $$P(-3|{-2})$$ (bad)

\end{document}


The empty \mathopen and \mathclose atoms match the parentheses and help into keeping things properly segregated.

You're not compelled to use \, on either side of \vert, but I believe it's better. In any case, just changing the definition of \coord would solve the tough situation described above.

You can still use an input style resembling the output, with the help of xparse:

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\coord}{ u( u| u) }{%
\IfValueT{#1}{#1}% the point's name (optional)
(#2\mathclose{}\,\vert\,\mathopen{}#3)%
}

\begin{document}

A point $$\coord P(-3|-2)$$

A point $$\coord(-3|-2)$$

\end{document}


• Just out of curiosity, what is a test case where the \mathclose{} becomes relevant? – daleif Aug 11 '16 at 10:30
• @daleif I like symmetry. ;-) – egreg Aug 11 '16 at 10:34
• The problem with a missing \mathopen comes up far more often than a missing \mathclose, so I just wanted an example of the later for my mental archive. – daleif Aug 11 '16 at 10:46
• @daleif I can't think to any real world case, since the first coordinate will always end with an ordinary atom. So \mathclose{} is not really necessary, I agree. – egreg Aug 11 '16 at 10:50
• @daleif Then I can think to some postfix sign: somebody uses 0-` to denote a negative number close to zero. – egreg Aug 11 '16 at 12:05