# Problem with tikz-pgf and \pgfmathabs

This is a follow up question to Problem with pgfmathsetmacro in foreach loop with pgfplots.

In short I also want to use \pgfmathabs to get the absolute value of a number in that example (with \i as input). But I get the following error:

ERROR: Incomplete \iffalse; all text was ignored after line 8.

--- TeX said ---
! Incomplete \iffalse; all text was ignored after line 8.
--- HELP ---
No help available


I tried to compose a MWE to isolate this problem:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\edef\test{\noexpand \node at (0,0) {\pgfmathabs{-2}};}
\test
\end{tikzpicture}

\end{document}


Any idea how to fix this?

-
Adding a \noexpand before the \pgfmathabs solves the problem. –  Marc van Dongen Sep 25 '12 at 9:11

\pgfmathabs (and the other pgfmath macros, including the general \pgfmathparse) are not expandable, so you'll have to do the calculation step before the \edef definition:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\pgfmathabs{-2}
\edef\test{\noexpand \node at (0,0) {\pgfmathresult};}
\test
\end{tikzpicture}

\end{document}


As Joseph suggests in the comment and in the linked answer, you can use the LaTeX3 FPU as an alternative:

\documentclass{article}

\usepackage{tikz}
\usepackage{expl3}

\ExplSyntaxOn
\newcommand{\fpabs}[1]{\fp_eval:n{abs(#1)}}
\ExplSyntaxOff

\begin{document}

\begin{tikzpicture}
\edef\test{\noexpand \node at (0,0) {\fpabs{-2}};}
\test
\end{tikzpicture}

\end{document}

-
Or you can do it inside the node by preventing the expansion \noexpand\pgfmathabs{-2}\noexpand\pgfmathresult –  percusse Sep 25 '12 at 9:13
An alternative would be to use the LaTeX3 FPU as in the linked question, with a definition such as \usepackage{expl3}\ExplSyntaxOn\newcommand{\fpabs}[1]{\fp_eval:n{abs(#1)}}\Expl‌​SyntaxOff. –  Joseph Wright Sep 25 '12 at 9:18
@JosephWright: Thanks! I've incorporated the suggestion into my answer –  Jake Sep 25 '12 at 9:28
I tried the LaTeX3 FPU version, but get the following error: ERROR: Undefined control sequence. --- TeX said --- \fpabs #1->\fp_eval:n {abs(#1)} l.13 ...\test{\noexpand \node at (0,0) {\fpabs{-2} };} –  student Sep 25 '12 at 11:54
@student Do you have the most recent version of the LaTeX3 FPU code? –  Andrew Stacey Sep 25 '12 at 12:21
show 2 more comments