4

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.

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

--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

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.