[Tex/LaTex] Shortcuts for \lvert and \rvert? (with amsmath)

math-modemath-operators

I often use the absolute value symbol and write \lvert x \rvert.

But, can I somehow define something shorter like \abs{x}?

I want to use the amsmath-package.

Best Answer

It's quite strange you refuse loading of other packages: packages good are, says Yoda.

However, if you don't want to use abstract methods and prefer do all the work by hand, you need not even load amsmath.

\documentclass{article}

\makeatletter
\DeclareRobustCommand{\abs}{\@ifstar\star@abs\normal@abs}
\newcommand{\star@abs}[1]{\left|#1\right|}
\newcommand{\normal@abs}[2][]{\mathopen{#1|}#2\mathclose{#1|}}
\makeatother

\begin{document}

$|\sin x|$ (wrong)

$\abs{\sin x}$ (right)

$\abs[\big]{\sin x}$
$\abs[\Big]{\sin x}$
$\abs[\bigg]{\sin x}$
$\abs[\Bigg]{\sin x}$
$\displaystyle\abs*{\frac{\sin x}{x}}$

\bigskip

$\abs[\big]{\sin x}_1^2$
$\abs[\Big]{\sin x}_1^2$
$\displaystyle\abs*{\frac{\sin x}{x}}_1^2$

\end{document}

enter image description here