# How can I calculate within style values?

Is it possible to calculate (addition etc.) in style values? I wanted to do the following:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,chains}

\newcommand*{\mypath}[3][0.0]{% looseness adjustment (optional),1st node, 2nd node
\draw (#2.south east) to [in=90, out=90, looseness=0.85+#1] (#3.south west);
}

\begin{document}
\begin{tikzpicture}[start chain]
\node[on chain] (k11) {};
\node[on chain] (k12) {};
\mypath{k11}{k12}
\end{tikzpicture}
\end{document}


The problem is looseness=0.85+#1 which fails with Missing number, treated as zero and Illegal unit of measure (pt inserted).

-
A better approach would be writing your own /.styles instead of nesting macros to avoid such problems. –  percusse Dec 30 '12 at 16:05
@percusse I don't like it either, but I don't know yet how I should transform my path operations into a style, I'll have to read the manual a bit more. –  neo Dec 30 '12 at 16:17

It is possible to parse the number into a macro and use the macro as the argument to the key.

But, the reason this has to be done is that the looseness keys do not appear to be parsed using pgfmath. So, for fans of hacking:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,chains}

\makeatletter
\def\tikz@to@set@in@looseness#1{%
\pgfmathparse{#1}\let\tikz@to@in@looseness=\pgfmathresult%
\let\tikz@to@end@compute=\tikz@to@end@compute@looseness%
\tikz@to@switch@on%
}
\def\tikz@to@set@out@looseness#1{%
\pgfmathparse{#1}\let\tikz@to@out@looseness=\pgfmathresult%
\let\tikz@to@start@compute=\tikz@to@start@compute@looseness%
\tikz@to@switch@on%
}

\newcommand*{\mypath}[3][0.0]{% looseness adjustment (optional),1st node, 2nd node
\draw (#2.south east) to [in=90, out=90, looseness=0.85+#1] (#3.south west);
}

\begin{document}
\begin{tikzpicture}[start chain]
\node[on chain] (k11) {};
\node[on chain] (k12) {};
\mypath[-0.5]{k11}{k12}
\end{tikzpicture}
\end{document}

-
Hm! How nice, what about the other keys? Is there a list which ones are parsed by pgfmath? –  neo Dec 30 '12 at 16:01
@neo All keys that requires real or integer values should be parsed as mathematical expression. It is not the case of looseness. I think this is a bug. –  Paul Gaborit Dec 30 '12 at 16:31

Yet another way is to fill a macro with the expression. (So there is no need to make the option math parseable)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,chains}

\newcommand{\mypath}[3][0.0]{% looseness adjustment (optional),1st node, 2nd node
\pgfmathsetmacro{\mytemp}{.85+ #1}
\draw (#2.south east) to [looseness=\mytemp,in=90, out=90, ] (#3.south west);
}

\begin{document}
\begin{tikzpicture}[start chain]
\node[on chain] (k11) {};
\node[on chain] (k12) {};
\mypath[1]{k11}{k12}
\mypath[0]{k11}{k12}
\end{tikzpicture}

\end{document}

-

Another approach to the same fundemental issue (the need to do floating-point maths on the value), this time using LaTeX3's FPU

\documentclass{article}
\usepackage{tikz}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
\usetikzlibrary{positioning,fit,chains}

\newcommand*{\mypath}[3][0.0]{% looseness adjustment (optional),1st node, 2nd node
\draw (#2.south east) to [in=90, out=90, looseness=\fpeval{0.85+#1}] (#3.south west);
}

\begin{document}
\begin{tikzpicture}[start chain]
\node[on chain] (k11) {};
\node[on chain] (k12) {};
\mypath{k11}{k12}
\end{tikzpicture}
\end{document}


(The LaTeX3 code is expandable, so can simply be dropped in to a place where the underlying TikZ code needs some form of number.)

-

Using the fp package works, as explained in another question.

\documentclass{article}
\usepackage[nomessages]{fp}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,chains}

\newcommand*{\mypath}[3][0.0]{% looseness adjustment (optional),1st node, 2nd node
\FPeval\loose{0.85+(#1)}
\draw   (#2.south east) to [in=90, out=90, looseness=\loose] (#3.south west);
}

\begin{document}

\begin{tikzpicture}[start chain]

\node[on chain] (k11) {};
\node[on chain] (k12) {};

\mypath[4.0]{k11}{k12}

\end{tikzpicture}

\end{document}

-