share
TeX - LaTeXAbsolute Value Symbols
[+330] [9] jamaicanworm
[2012-01-31 23:41:52]
[ math-mode symbols ]
[ https://tex.stackexchange.com/questions/43008/absolute-value-symbols ]

What is the "best LaTeX practices" for writing absolute value symbols? Are there any packages which provide good methods?

Some options include |x| and \mid x \mid, but I'm not sure which is best...

(63) \usepackage{amsmath}...\(\lvert x\rvert\); \mid denotes a relation symbol and is wrong for the absolute value. - egreg
(3) Should I do $\usepackage{mathtools}...\DeclarePairedDelimiter{\vert}{\lv‌​ert}{\rvert} as per your answer to a previous question? tex.stackexchange.com/a/42274/9757 - jamaicanworm
(3) It's a good possibility. It depends on how many absolute values you have in your document; for a couple I wouldn't bother. But \vert is not a good choice, as it's already defined. - egreg
(4) Thanks! Why do people not just use the keyboard | symbol? - jamaicanworm
(30) You can actually use |, but in some situations a certain care is needed. For instance \(|{-1}|=1\) without the braces would come out wrong, while \(\lvert-1\rvert=1\) surely gives correct output. - egreg
Maybe this \bigg\vert Or any kind of combination of \big, \Big, \bigg, \Bigg and \vert. - user29243
I was anout to say to use the physics package, but when including a \frac command, it spits out an error ;( - jjdb
(1) The use of mathtools is a good idea, but use \abs for the name of the new delimiter, as in \DeclarePairedDelimiter{\abs}{\lvert}{\rvert} - Máté Wierdl
It might be my fault, but there is an obvious answer given by egreg (tex.stackexchange.com/users/4427/egreg) to the question tex.stackexchange.com/questions/485750/… which suited me (while the replies to the present post did not). Therefore I think that this reply by egreg should be added at the begining of this post. - Paulo Providencia
[+251] [2012-01-31 23:48:44] Peter Grill [ACCEPTED]

I have been using the code below using \DeclarePairedDelimiter from the mathtools package [1].

Since I don't think I have a case where I don't want this to scale based on the parameter, I make use of Swap definition of starred and non-starred command [2] so that the normal use will automatically scale, and the starred version won't:

enter image description here

If you want it the other way around comment out the code between \makeatother...\makeatletter.

\documentclass{article}
\usepackage{mathtools}

\DeclarePairedDelimiter\abs{\lvert}{\rvert}%
\DeclarePairedDelimiter\norm{\lVert}{\rVert}%

% Swap the definition of \abs* and \norm*, so that \abs
% and \norm resizes the size of the brackets, and the 
% starred version does not.
\makeatletter
\let\oldabs\abs
\def\abs{\@ifstar{\oldabs}{\oldabs*}}
%
\let\oldnorm\norm
\def\norm{\@ifstar{\oldnorm}{\oldnorm*}}
\makeatother

\newcommand*{\Value}{\frac{1}{2}x^2}%
\begin{document}
    \[\abs{\Value}  \quad \norm{\Value}  \qquad\text{non-starred}  \]
    \[\abs*{\Value} \quad \norm*{\Value} \qquad\text{starred}\qquad\]
\end{document}
[1] http://www.ctan.org/pkg/mathtools
[2] https://tex.stackexchange.com/questions/23178/swap-definition-of-starred-and-non-starred-command

(6) @egreg: Agreed. But I have not encountered that with \abs yet. BTW, I like your "always is generally wrong" :-) - Peter Grill
Thanks for the help! General point of interest: why do you use \dfrac instead of plain \frac? - jamaicanworm
(3) @jamaicanworm: I used \dfrac in this case to make a larger fraction in inline mode. This is not normally recommended in inline mode as it breaks paragraph spacing. I will update the solution to remove that. - Peter Grill
(5) There's a reason why it's preferable to use the *-version for the automatic resizing macro: always using \left and \right is wrong, in general. - egreg
(5) +1, even though I have to say that this answer is not very skim-reading friendly with the huge "starred"/"non-starred" picture and the remark somewhere in the text "BTW, normal behaviour is the other way around". The question OTOH is one that makes everything in this thread very prone to being skim-read. - Christian
If I could upvote for every time I've referenced this... - jwalk
(6) a definite advantage of using commands like \abs and \norm which no one seems to have mentioned: since the | symbol has special meaning in the makeidx context, trying to enter terminology using a bare | within \index{...} will result in (delayed) errors and a lot of hair pulling for someone not really familiar with that usage. commands like the ones shown here (or even just bare \vert, \Vert or \mid) is a lot safer and worth becoming familiar with. - barbara beeton
@barbarabeeton: Excellent point! - Peter Grill
I do not understand your comment, @egreg about *. In my practice, I almost always need the automatic size changes of these delimiters. The great thing about the mathtools versions is that it's easy to change from one version to another. In many cases, I use starred versions of environments and commands more often than unstarred ones. For example, I usually use equation* or align* in final versions of a paper or book. - Máté Wierdl
It seems that all \abs solutions here do not work with pandoc for now. Why isn't this basic math operator/function be implemented by default? - Minh Nghĩa
1
[+109] [2012-02-01 10:32:48] David Carlisle

Note if you just use | you get mathord spacing, which is different from the spacing you'd get from paired mathopen/mathclose delimiters or from \left/\right even if \left/\right doesn't stretch the symbol. Personally I prefer the left/right spacing from mathinner here (even if @egreg says I'm generally wrong:-)

\documentclass{amsart}

\begin{document}

$  \log|x||y|b $

$  \log\left|x\right|\left|y\right|b $

$  \log\mathopen|x\mathclose|\mathopen|y\mathclose|b $

\end{document}

enter image description here


2
[+89] [2013-12-08 17:21:03] Wildcat

One can also use commath package.

\documentclass{article}

\usepackage{commath}

\begin{document}
\[ \norm{a \vec{u}} = \abs{a} \, \norm{\vec{v}} \]
\end{document}

enter image description here


(8) +1 for this one, as it includes the semantics and has a relatively short syntax. - Martin Thoma
(1) The spacing is somehow bad when used as subscript. The \abs{} of physics is the better choice in my opinion. - fyaa
(1) sorry the commath package is fundamentally broken and should be avoided, see for example tex.stackexchange.com/questions/135944/commath-and-ifinner - David Carlisle
3
[+57] [2016-07-22 17:00:48] NauticalMile

The physics [1] LaTeX package also implements abs and norm:

\documentclass{article}

\usepackage{physics}

\begin{document}
    \[ c = \abs{-c} \]
    \[ \vu{a} = \frac{\vb{a}}{\norm{\vb{a}}} \]
\end{document}

enter image description here

[1] https://www.ctan.org/pkg/physics?lang=en

(1) I think this is the easiest solution! - Tropilio
4
[+34] [2017-08-11 12:04:34] pietrodn

A simple, LaTeX native way of doing this is by using the \| delimiter, with the standard \left and \right modifiers ( source [1]).

For example:

\left\| \sum_{i=1}^{n} x^2 \right\|

gives

enter image description here

[1] http://www.maths.tcd.ie/~dwilkins/LaTeXPrimer/BracketsNorms.html

(9) Also: using the | delimiter (without the preceding backslash) inserts a single vertical bar. - pietrodn
(5) For fun try \|+a\|, as you can see this is wrong, and thus generally users should not just use |...| or \|...\| as they may accidentally come into situations where this is wrong. I'll leave it as an exercise why this happens. Plus in your example the fences does not need to be that tall. - daleif
(1) With \|+a\| the spacing appears to be wrong, but with \left\|+a\right\| it appears to be fine. - pietrodn
(5) Correct because one is left and the other is right. But \left and \right should only be added when actually needed (which is actually not often). My complain to your answer is that it is not good in general, for reasons that are explained in comments to other answers in this thread. - daleif
5
[+10] [2018-02-20 09:21:48] alhelal
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  %begin equation
\lVert w \rVert
%end equation
\]
\end{document}  

enter image description here


6
[+3] [2015-05-28 18:51:46] Mene

For LyX users: maybe I have just overlooked how to do it correctly, but I couldn't find a way of doing this natively.

I thus used a 1x1-Matrix environment and set the kind to determinant. It might just be a hack, but it works fine in my usecase.


At least in the case of Lyx 2.1.4, I find that I can simply type \lvert and \rvert into my display formula and Lyx "does the right thing." In my specific case I want absolute value bars around a fraction, so I used \bigg\lvert on the one side and \bigg\rvert on the other side. - Greg Hill
7
[0] [2020-02-27 22:34:02] James Steward

If you don't want to use any package, use \mid.


(6) \vert doesn't require any package either, and the spacing isn't as awful as with \mid. - Phelype Oleinik
8
[0] [2023-05-07 14:39:18] Nguyen Van Minh Hieu
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{esvect,commath}
\author{Nguyễn Văn Minh Hiếu}
\begin{document}
$\overrightarrow{AB}$\\
$\abs{\vv{AB}}$\\
$\abs[1]{\vv{a}}+\abs[1]{\vv{b}}=\abs[1]{\vv{c}}$\\
\end{document}

9