Old question, but it got bumped to the front page…
The character printed by |
, \mid
, \lvert
and \rvert
is always the same, but with different math class:
|
is an ordinary symbol;
\mid
is a relation symbol;
\lvert
is an opening symbol;
\rvert
is a closing symbol.
Access to the last two requires loading amsmath
. Except \mid
they can all be used in the context of delimiters. For instance, if you need a taller bar as a relation symbol, you can use \bigm|
; an example usage could be in
\lvert a\rvert \bigm| b
because \bigm\mid
results in an error.
I'd deem a|b
to denote divisibility a wrong usage on mathematical grounds, because this is a relation. Personally I always use \mid
for this case, but I acknowledge that people might find the spacing excessive.
\documentclass{article}
\usepackage{amsmath}
\newcommand{\divides}{%
\mathrel{%
\nonscript\mspace{-\thickmuskip}%
\nonscript\mspace{0.5\thinmuskip}%
|
\nonscript\mspace{0.5\thinmuskip}%
\nonscript\mspace{-\thickmuskip}%
}%
}
\begin{document}
$a\mid b$ $A_{a\mid b}$ (with \verb+\mid+)
$a\divides b$ $A_{a\divides b}$ (with \verb+\divides+)
\end{document}
The \nonscript
bits are for avoiding the spaces (positive or negative) to be inserted when TeX wouldn't.
I'd endorse using a semantic command for divisibility, even if it is just
\newcommand{\divides}{\mid}
because this will allow change one's mind at any time (which is what I do for bigger projects).
A possible improvement is coping with the \big
problem above:
\documentclass{article}
\usepackage{amsmath}
\NewDocumentCommand{\divides}{s}{%
\mathrel{%
\nonscript\mspace{-\thickmuskip}%
\nonscript\mspace{0.5\thinmuskip}%
\IfBooleanT{#1}{\big}|
\nonscript\mspace{0.5\thinmuskip}%
\nonscript\mspace{-\thickmuskip}%
}%
}
\begin{document}
$a\mid b$ $A_{a\mid b}$ (with \verb+\mid+)
$a\divides b$ $A_{a\divides b}$ (with \verb+\divides+)
$\lvert a\rvert\divides* b$
\end{document}
I'd not generalize to other sizes, because of the symbol's meaning.
In general |
should be avoided for the absolute value: compare
$|-1+x|=|x-1|$
$\lvert -1+x\rvert=\lvert x-1\rvert$
The top one is definitely wrong: since |
is ordinary, the minus sign finds two ordinary symbols around it and so it typesets as a binary operatio symbol; with the correct syntax, \lvert
is an opening, so the minus sign stays unary as it should.
|
does not have.