0

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.

  • 3
    Possible duplicate of Absolute Value Symbols – CarLaTeX Jul 9 '17 at 6:48
  • @CarLaTeX I want to use amsmath, the answers in the link uses a couple of other packages. – JDoeDoe Jul 9 '17 at 6:54
  • OK, I've retracted my close vote. – CarLaTeX Jul 9 '17 at 6:55
  • Note that mathtools as suggested in Absolute Value Symbols extends amsmath. In fact it loads amsmath itself. So you could argue that a mathtools solution also uses amsmath. – moewe Jul 9 '17 at 9:12
3

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

  • I was looking for something with newcommand (CarLaTeXs post). – JDoeDoe Jul 9 '17 at 11:51
1

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

  • Thanks you! Is it also possible to auto-scale the bars with this command? – JDoeDoe Jul 9 '17 at 11:49
  • @JDoeDoe You're welcome! See my renewed answer. – CarLaTeX Jul 9 '17 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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