[Tex/LaTex] Doing maths with distance values in LaTeX source code

calculationsmacros

I have a sty file with the command

\newcommand{\foo}[1]{\noindent\kern#1}

but I'd actually like to have something like

\newcommand{\foo}[1]{\noindent\kern{#1 *0.5}}

meaning that I'd like to have half of the distance the user puts on command \foo to be passed to \kern. This obviously doesn't work.

Do you have any suggestions on how to do this?

(You may ask "why the hell would you want to do that??". Fair question. I'm using someone else's program which generates tex files and I don't have control over the values that come up on the command \foo. I just know that I'd like to have half of what that program chooses.)

Best Answer

Use the eTeX primitive \dimexpr to allow a factor like before dimension registers:

\newcommand{\foo}[1]{\noindent\kern .5\dimexpr#1\relax}

Note that \dimexpr swallows the \relax and also allows for some arithmetic inside. See the etex_manual for more details.

Related Question