Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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...

share|improve this question
21  
\usepackage{amsmath}...\(\lvert x\rvert\); \mid denotes a relation symbol and is wrong for the absolute value. –  egreg Jan 31 '12 at 23:45
    
Should I do $\usepackage{mathtools}...\DeclarePairedDelimiter{\vert}{\lvert}{\rvert} as per your answer to a previous question? tex.stackexchange.com/a/42274/9757 –  jamaicanworm Jan 31 '12 at 23:47
2  
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 Jan 31 '12 at 23:50
    
Thanks! Why do people not just use the keyboard | symbol? –  jamaicanworm Feb 1 '12 at 0:16
10  
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 Feb 1 '12 at 0:22

3 Answers 3

up vote 45 down vote accepted

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

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 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}
share|improve this answer
2  
@egreg: Agreed. But I have not encountered that with \abs yet. BTW, I like your "always is generally wrong" :-) –  Peter Grill Jan 31 '12 at 23:57
    
Thanks for the help! General point of interest: why do you use \dfrac instead of plain \frac? –  jamaicanworm Feb 1 '12 at 0:08
2  
@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 Feb 1 '12 at 0:16
1  
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 Oct 15 '12 at 17:24
2  
+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 Feb 27 '13 at 18:10

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.