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.
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.
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}
Why don't simply define a new command?
To auto-scale the bars, just use \left and \right, here some examples:
\documentclass{article}
\usepackage{amsmath}
\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}
\begin{document}
\[
\abs{x} \abs{\frac{y}{z}} \abs{w_{i}^{k}} \abs{\sum_{j=1}^{N}{X_{j}}}
\]
\end{document}
mathtoolsas suggested in Absolute Value Symbols extendsamsmath. In fact it loadsamsmathitself. So you could argue that amathtoolssolution also usesamsmath. – moewe Jul 09 '17 at 09:12