6

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).

2
  • 1
    A better approach would be writing your own /.styles instead of nesting macros to avoid such problems. Commented Dec 30, 2012 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. Commented Dec 30, 2012 at 16:17

4 Answers 4

5

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}
2
  • Hm! How nice, what about the other keys? Is there a list which ones are parsed by pgfmath? Commented Dec 30, 2012 at 16:01
  • 1
    @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. Commented Dec 30, 2012 at 16:31
5

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}
5

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.)

3

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}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.