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.

I try to use the results of pgfmath calculations with cnttest from xifthen, but it fails:

\documentclass{article}
\usepackage{pgf,xifthen}

\begin{document}
  \pgfmathparse{42+77}%
  \def\val{\pgfmathprintnumber{\pgfmathresult}}
  \ifthenelse{\cnttest{\val}{>=}{50}}{%
    Large
  }{%
    Small
  }%
\end{document}

pdflatex (TexLive 2012) produces in the log:

./test.tex:7: Missing number, treated as zero.
<to be read again> 
                   \protect 
l.7   \ifthenelse{\cnttest{\val}{>=}{50}}
                                         {%

And in the document (yes, the message leaves the page):

enter image description here

I suspect it's an expansion problem -- something I seem to be unable to wrap my head around. I know that using \pgfmathprintnumber{\pgfmathresult} directly (instead of val does not help. Can I fix this somehow?

share|improve this question
1  
You can simply use \pgfmathtruncatemacro\val{42+77}% to directly save the integer part of the operation result into your \val macro. Then everything works as expected. –  Jake Nov 14 '12 at 22:41
    
@Jake Sweet, thanks. Can you make that an answer? –  Raphael Nov 14 '12 at 22:48
    
Is really \pgfmathparse necessary? If you're dealing with integers, \numexpr is way handier. –  egreg Nov 14 '12 at 22:58
    
@egreg I'm not too familar with the capabilities of either one, but keep in mind that I provided an MWE. Real use cases may not be restricted to integers. –  Raphael Nov 14 '12 at 23:16

1 Answer 1

up vote 3 down vote accepted

You are already using PGF so why not use also the conditionals of PGF ?

\documentclass{article}
\usepackage{pgf}
\begin{document}
  \pgfmathparse{42+77<50? "Small" : "Large"}\pgfmathresult%
\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.