# TikZ/PGF: compilation hangs when using macro

When compiling the following code using pdflatex,

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fpu}
\begin{document}
\begin{tikzpicture}
\def\dim#1{
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathparse{#1}
\pgfmathresult pt
\pgfkeys{/pgf/fpu=false}
}
\draw ( 0, \dim{10in + 1in} ) circle (1in);
\end{tikzpicture}
\end{document}


the compilation hangs at the following lines:

[...]
) (/usr/share/texmf-dist/tex/generic/oberdiek/pdftexcmds.sty)
(/usr/share/texmf-dist/tex/latex/oberdiek/epstopdf-base.sty
(/usr/share/texmf-dist/tex/latex/oberdiek/grfext.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/kvdefinekeys.sty))
(/usr/share/texmf-dist/tex/latex/oberdiek/kvoptions.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/kvsetkeys.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/etexcmds.sty)))
(/usr/share/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))


Where exactly is the problem? Why does the compilation hang?

Basically, I'd like to have a TeX macro that parses the first argument using PGF's FPU, then returns its result in pts.

EDIT: I'm using texlive 20120701 and PGF 2.10

I noticed that this issue is not related to the macro at all. The following code fails the same way:

\draw ( 0,
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathparse{sqrt(pow(10in, 2) + pow(12in, 2))}
\pgfmathresult pt
\pgfkeys{/pgf/fpu=false}
) circle (1in);

-
\draw ( 0, {(10in + 1in)} ) circle (1in); is enough to parse. –  percusse May 7 '14 at 9:12
Yes it is, but I need to use PGF's FPU. I know, my example isn't ideal, I should've used something like \dim{sqrt(pow(10in, 2) + pow(12in, 2))} to demonstrate the actual problem (use the same expression in TeX and you'll get the Dimension too large error when compiling). –  watain May 7 '14 at 9:15
At a guess, I'd be worrying about expandability here. Are all the macros in \dim expandable? (Also, note that \dim is already defined in the LaTeX kernel so you might want to choose another name.) –  Loop Space May 7 '14 at 9:47
Ah cool haha a simpler example \begin{tikzpicture} \draw (0,\pgfkeys{}) circle (1); \end{tikzpicture}. It's meant to be a hint :P –  percusse May 7 '14 at 11:30
As each part of a coordinate is expanded using \edef an even simpler example is \edef{\pgfkeys{}}. –  Mark Wibrow May 7 '14 at 11:57

You can't just throw any old command into a tikz coordinate as each x, y (o z) component is expanded using \edef. One could try defining a special coordinate system (note in the example below, unit-less dimensions will be interpreted as points).

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{fpu}
\tikzset{fpu cs/.cd, x/.initial=0, y/.initial=0}

\tikzdeclarecoordinatesystem{fpu}{%
\tikzset{fpu cs/.cd,#1}%
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}%
\pgfmathparse{\pgfkeysvalueof{/tikz/fpu cs/x}}%
\let\tmpx=\pgfmathresult%
\pgfmathparse{\pgfkeysvalueof{/tikz/fpu cs/y}}%
\let\tmpy=\pgfmathresult%
\pgfpoint{+\tmpx pt}{+\tmpy pt}%
}

\begin{document}
\begin{tikzpicture}
\draw (fpu cs:x=0, y={sqrt(pow(10in, 2) + pow(12in, 2))}) circle [radius=1in];
\end{tikzpicture}

\end{document}


Not much point in showing the output as it is just a circle. You could of course add the fpu options to the tikzpicture instead.

-
Thanks for the hints. So now I'm just using /pgf/fpu=true,/pgf/fpu/output format=fixed as options for tikzpicture and everything works as expected. No need to use \pgfmathparse/\pgfmathresult anymore, as all calculations are now done using PGF's FPU. –  watain May 9 '14 at 20:23

I don't know if next code is useful but works in this case

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fpu}
\begin{document}
\begin{tikzpicture}
\newcommand\mydim[2]{
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetlengthmacro{#1}{#2}
\pgfkeys{/pgf/fpu=false}
}

\draw ( 0, {(10in + 1in)} ) circle (1in);

\mydim{\test}{sqrt(pow(10in,2)+pow(12in,2))}
\draw ( 0, \test ) circle (1in);

\end{tikzpicture}
\end{document}

-
This seems to work, thank you. But it is a little bit inconvenient. I wonder why it won't work with \pgfmathparse, it seems like a bug to me. –  watain May 7 '14 at 11:09
@watain It's not a bug. TikZ wants to see an expression, not the instructions to produce an expression and that involve assignments (such as \pgfmathparse). –  egreg May 7 '14 at 11:58