2

I have defined a command for absolute value using providecommand. Initially, I simply used

\providecommand{\abs}[1]{\lvert#1\rvert}

That was working fine until I needed to take the absolute value of an integral, and the lines did not scale at all. To make the lines scale, I tried

\providecommand{\abs}[1]{\left\lvert#1\right\rvert}

This works in the document, but my editor (overleaf) does not like it at all, and has highlighted all subsequent text in red - an error message intermittently displays saying command may only be used in math mode. Is my use OK (and should I therefore ignore the error message - can I override it?) or is there a better way of writing it?

Thanks

6
  • Welcome to TeX.SE! Probably the easiest is just to indeed use this command in math mode, so either as part of an equation or in text as This is some text $\abs{25}$ and more text (note the $ signs that open and close math mode). – Marijn Aug 10 '20 at 16:05
  • Overleaf's code check is distinct from the compilation process - it tries to highlight common errors as you type. It does have its limitations, as it is not a full compilation of your project, and can get fouled up by custom commands. You can selectively turn code check off for sections of your .tex file, or turn it off globally: overleaf.com/learn/how-to/Code_Check – Dan MacKinnon Aug 10 '20 at 17:01
  • @Marijn - thanks, I am only using the command in math mode anyway; I just wondered if there was anything syntactically wrong with it – Jack Aug 10 '20 at 18:50
  • @DanMacKinnon great thanks, exactly what I needed - would have driven me mad if I had to deal with pretty much the entirety of my document being highlighted red for the rest of the time I'm writing it. – Jack Aug 10 '20 at 18:59
  • 3
    Hardcoding autoscalling is a disaster waiting to happen (try it on \sum_n), I'd rather recommend using \DeclarePairedDelimiter to define a better abs command (disclaimer I wrote it) – daleif Aug 11 '20 at 5:07
2

You can do this with \DeclarePairedDelimiter from mathtools. This gives you the options to scale automatically, or specify size as an option to the command.

One of the examples in the manual is

\DeclarePairedDelimiter\abs{\lvert}{\rvert}
1

I not know if Overleaf has the physics package into his distribuition...but you could to use it. It is a package that have some limits (correct spaces). I add some examples with \qty, \abs, \abs\Big, \abs*:

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath,amssymb}

\usepackage{physics}

\begin{document}
\[\qty|a|, \quad \text{qty is a short name of quantity: typical absolute value}\]
\[\abs{a}, \quad \text{the absolute value equivalent to the previous code} \]
\[\abs\Big{a}\]
\[\abs*{b}, \quad \text{star for no resize} \]
\end{document}

enter image description here

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