We can use calc library inside tikzmath to do calculations with coordinates.
Everything works fine if we want to calculate something like \p3=(0,0)!.5!(2,0); (inside and outside functions). But if we want a rotation like this \p3=(0,0)!.5!90:(2,0); the following happens :
- If we use this kind of calculation in a function and we compile with PDFLaTeX, or LuaLaTeX, all the coordinates are misplaced after that.
- If we do the same calculations outside of function or if we compile with XeLaTex, everything is ok.
Here is a test code:
\documentclass[tikz,border=7pt,convert={density=3500}]{standalone}
\usetikzlibrary{calc,math}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\tikzmath{
% function supposed to draw (0,0) -- (0,1) in red
function test(\n){ % useless \n parameter
coordinate \p;
\p1 = (0,0); \p2 = (2,0);
\p3 = (\p1)!.5!90:(\p2);
{\draw[red,thick] (\p1) -- (\p3);};
};
% same code outside function : draw (0,0) -- (0,1) in green
coordinate \p;
\p1 = (0,0); \p2 = (2,0);
\p3 = (\p1)!.5!90:(\p2);
{\draw[green,ultra thick] (\p1) -- (\p3);};
test(1);
}
\end{tikzpicture}
\end{document}
Here is the (bad) result compiled with PDFLaTeX (same as LuaLaTeX):

Here is the (good) result compiled with XeLaTeX:

The question is : Can we work around this bug?
Some remarks :
- The behaviour is very similar to what we can observe in this question. And following the Mark Wibrow's answer, we can suspect a "non
nullfontproblem". That can happens for example if:is active character producing some output. - If we call
test(1)just after its definition, both lines are misplaced. - I had to put in
testfunction one useless parameter\nbecause we can't have functions without parameter intikzmath. This is, for me, a strange behaviour.
<blank space>and\nullfontstrings in the log file (with\tracingcommands=1) and it's exactly the same either withpdflatexorxelatex. – egreg Jan 16 '15 at 21:21calcinsidetikzmathand the syntax is validcalccalculation. And it works as expected in some cases. – Kpym Jan 16 '15 at 23:25functionshould bereturn. Is that mistaken? – cfr Jan 17 '15 at 01:18returnis not mandatory. If you use it, it must be the last executed statement. One reason is thatreturndont stop the function execution (like in "usual" languages). All statements after the return will be executed too. – Kpym Jan 17 '15 at 07:48