6

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

6

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.

  • I suppose I'd better upvote this one for being even less wrong than my answer:-) – David Carlisle Apr 7 '13 at 10:34
1

Since this is for a mathematical use, here is a command that can do the calculation with an accuracy of 18 decimal places.

\documentclass{article}
\usepackage{amsmath}
\usepackage{fp}

\begin{document}

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

\utext{test}{1.9809997889999999} \result

\utext{test}{1.9809997889999990} \result
\end{document}

enter image description here

  • 1
    Since I'm only calculating the length of the underscore, I won't need that much precision. This will be handy, when working with mathematical expressions though. Thanks – Max Apr 7 '13 at 11:25

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.