15

In the following example I want to use \ifnum to compare two numbers computed by pgfmath, for simplicity I use simply 1 and 2 in the example below. Compiling this code gives me the error:

    ERROR: Missing = inserted for \ifnum.

--- TeX said ---
<to be read again> 
                   .
l.11    \ifnum \one
                   <\two
--- HELP ---
From the .log file...

I was expecting to see `<', `=', or `>'. Didn't.

The example is:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}


\begin{tikzpicture}
   \pgfmathsetmacro{\one}{1};
   \pgfmathsetmacro{\two}{2};
   \ifnum \one<\two
   \draw (0,0) -- (0,1);
   \fi
\end{tikzpicture}

Any idea how to solve this?

1 Answer 1

27

\ifnum compares integers. The results stored by \pgfmathsetmacro always contain a decimal part, even for integer numbers, so you'll have to use \pgfmathtruncatemacro in this case:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
   \pgfmathtruncatemacro\one{1}
   \pgfmathtruncatemacro\two{2}
   \ifnum\one<\two
   \draw (0,0) -- (0,1);
   \fi
\end{tikzpicture}
\end{document}
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .