# How to build a macro that return the absolute value of an argument of another macro?

How can I build a macro that return the absolute value of an argument of another macro? I would like that the \abs macro return the absolute value of the its argument.

\RequirePackage{tikz}
\newcommand{\abs}[1]{<implementation>}
\newcommand{\command}[2]{\raisebox{\numexpr\abs{#1}*#1\relax\baselineskip}[0pt][0pt]{}{}}


So whether I use a positive or negative number as argument, the expression \abs{#1} will always be positive.

• \newcommand{\abs}[1]{\ifnum#1<0 -\fi#1} – egreg Jun 14 '14 at 22:42

--1 is legal in \numexpr, equivalent to 1. So

\newcommand{\absval}[1]{\ifnum#1<0 -\fi#1}


is what you're looking for. Then

\newcommand{\command}[2]{%
\raisebox{\numexpr\absval{#1}\relax\baselineskip}[0pt][0pt]{}{}}


will do.

• Shouldn't it be \abs instead of \absval in the first snippet to be compatible with the second one? – Henri Menke Jun 15 '14 at 0:30
• @HenriMenke Yes, it should. ;-) – egreg Jun 15 '14 at 8:32