Dynamic length variable with argument? (like “\setlength{\foo}[1]{0.5+#1*0.5}”)

Is there a way to define some kind of a length function that takes an argument, sort of a mixture between \newcommand and \setlength?

The following example should illustrate what I want. (I'm using a tikz tree because that's where I've encountered this problem, but the way I see it the question is not tikz-specific.)

\documentclass{article}
\usepackage{tikz}
\begin{document}

\newlength{\unit}
\setlength{\unit}{2cm}

\begin{tikzpicture}
\node {hello}
child{
node[yshift=-0.5\unit-5*0.5\unit] {world}
}
;
\end{tikzpicture}

\end{document}


The code I'd like to put in a variable is -0.5\unit-5*0.5\unit. I'd like to replace it by something like \foo{5}, that could be defined as \setlength{\foo}[1]{-0.5\unit-#1*0.5\unit} (that's what it might look like if \setlength could process arguments that same way as \newcommand).

\newcommand doesn't work because it defines a command, not a length, and \setlength doesn't take any arguments.

you want some thing like this?

\documentclass{article}
\usepackage{tikz}
\begin{document}

\newlength{\unit}
\setlength{\unit}{2cm}
\newcommand\foo[1]{-0.5\unit-#1\unit}

\begin{tikzpicture}
\node {hello}
child{
node[yshift=\foo{2.5}] {world}
}
;
\end{tikzpicture}

\end{document}


here 2.5=5*0.5

• Yes! But I could swear this failed when I tried it.. Must've had a typo somewhere. – flotzilla Mar 6 '15 at 8:43