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.

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?

share|improve this question

1 Answer 1

up vote 14 down vote accepted

\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}
share|improve this answer

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.