Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
Adding a \noexpand before the \pgfmathabs solves the problem. –  Marc van Dongen Sep 25 '12 at 9:11

1 Answer 1

up vote 4 down vote accepted

\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}
share|improve this answer
    
Or you can do it inside the node by preventing the expansion \noexpand\pgfmathabs{-2}\noexpand\pgfmathresult –  percusse Sep 25 '12 at 9: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)}}\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? –  Loop Space Sep 25 '12 at 12:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.