2

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.

JDoeDoe
  • 121

2 Answers2

5

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

egreg
  • 1,121,712
2

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}

enter image description here

CarLaTeX
  • 62,716