4

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?

student
  • 29,003

1 Answers1

4

\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}
Jake
  • 232,450
  • Or you can do it inside the node by preventing the expansion \noexpand\pgfmathabs{-2}\noexpand\pgfmathresult – percusse Sep 25 '12 at 09:13
  • 1
    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)}}\ExplSyntaxOff. – Joseph Wright Sep 25 '12 at 09:18
  • @JosephWright: Thanks! I've incorporated the suggestion into my answer – Jake Sep 25 '12 at 09: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
  • @AndrewStacey Just installed the latest version now it works! – student Sep 25 '12 at 12:30
  • Actually, we also have \fp_abs:n {#1} which is equivalent to \fp-eval:n { abs(#1) }. – Bruno Le Floch Oct 06 '12 at 22:13