Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer
    
I suppose I'd better upvote this one for being even less wrong than my answer:-) –  David Carlisle Apr 7 '13 at 10:34
    
Thank you. Both ways work perfectly! –  Max Apr 7 '13 at 11:22

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

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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