[Tex/LaTex] Floating Point multiplication in custom commands

calculationsmacros

I'd like to create a custom command which creates an underscore of a given length. However, this length should be the argument of the command multiplied be a floating point constant.

How can I multiply the argument of a command within the command's definition?

The code without the multiplication looks like this:

\newcommand{\utext}[2]{$\underset{\mbox{\tiny #1}}{\underline{\hspace{#2cm}}}$}

Instead of the \hspace of #2cm it should be something like (0.75 * #2)cm

Best Answer

There are two ways for solving the problem.

First (and recommended):

\newlength{\threequarters}
\setlength{\threequarters}{0.75cm}

\newcommand{\utext}[2]{%
  $\underset{\mbox{\tiny #1}}{\underline{\hspace{#2\threequarters}}}$%
}

In this way, #2 can be any decimal number.

Second, trickier:

\newcommand{\utext}[2]{%
  $\underset{\mbox{\tiny #1}}{\underline{\hspace{0.75\dimexpr#2cm\relax}}}$%
}

Why do I recommend the first way? You can use \threequarters wherever you want and change its width by acting just in one place, instead of looking for 0.75 across your definitions.

However, also the second solution is "parametrisable", by doing

\newcommand{\threequarterfactor}{0.75}

and using \hspace{\threequarterfactor\dimexpr#2cm\relax} in the definition. I still prefer reserving a length.

Related Question