3

I can not understand why the following use of ifthenelse and pgf give me an error :

\documentclass{article}
\usepackage{pgf}
\begin{document}
\pgfmathrandominteger{\a}{2}{7}
\pgfmathrandominteger{\b}{2}{3}
\def\c{\pgfmathparse{ifthenelse(\a!=\b,\a,int(\a+1))} \pgfmathresult}
\def\M{ \pgfmathparse{int(\c)} \pgfmathresult}
$\M$
\end{document}

give me the error

! TeX capacity exceeded, sorry [input stack size=5000].
\@inmathwarn ...latex@warning {Command \protect #1 invalid in math mode}\fi 
l.21 $\M
    $

While the code

\documentclass{article}
\usepackage{pgf}
\begin{document}
\pgfmathrandominteger{\a}{2}{7}
\pgfmathrandominteger{\b}{2}{3}
\def\c{\pgfmathparse{ifthenelse(\a!=\b,\a,int(\a+1))} \pgfmathresult}
\pgfmathparse{int(\c)}
\def\M{\pgfmathresult}
$\M$
\end{document}

give me the error :

! Incomplete \iffalse; all text was ignored after line 20.
<inserted text> 
                \fi 
<*> bugpgf

As long as i don't try to make a computation on \c there is no error; as for example in

\documentclass{article}
    \usepackage{pgf}
    \begin{document}
    \pgfmathrandominteger{\a}{2}{7}
    \pgfmathrandominteger{\b}{2}{3}
 $\a$ $\b$ $\c$
    \def\c{\pgfmathparse{ifthenelse(\a!=\b,\a,int(\a+1))} \pgfmathresult}    \end{document}

Latex seems to tell me i missed the fi part of the command but I did not did I ? the use of

\a!=\b ?\a : int(\a+1)

has similar effects. Any help would be greatly appreciated.

  • Note that you should be a bit careful about the choice of macro names. Both \def and \pgfmathsetmacro will overwrite existing macros without warning, and redefining them could potentially cause problems elsewhere. \a, \b and \c are all defined by default, see tex.stackexchange.com/questions/19111/short-names-for-macros – Torbjørn T. Feb 20 '18 at 20:40
3

When you define \c and then define \M what happens is

int(\pgfmathparse{ifthenelse(\a!=\b,\a,int(\a+1))} \pgfmathresult) + 1

which is not expandable hence it can't take the integer part and add 1 to it. In the second case similar thing happens but at a different step it trips up.

In both cases error messages don't reflect the actual problem but shows the first thing that trips up.

What you need to is to assign the value of the computation to \c either via

 \pgfmathparse{\pgfmathparse{ifthenelse(\a!=\b,\a,int(\a+1))}}
 \let\c=\pgfmathresult

or

 \pgfmathsetmacro\c{ifthenelse(\a!=\b,\a,int(\a+1))}

Then \c would hold the actual value of the computation and not the computation commands.

\documentclass{article}
\usepackage{pgf,pgffor}
\begin{document}
\foreach\x in{0,...,10}{
\pgfmathrandominteger{\a}{2}{7}
\pgfmathrandominteger{\b}{2}{3}
\pgfmathsetmacro\M{int((\a!=\b?\a:int(\a+1))+1)}
$\a, \b \to \M$

}
\end{document}

enter image description here

  • Actually with the first solution "\pgfmathparse{\pgfmathparse{ifthenelse(\a!=\b,\a,int(\a+1))}} \let\c=\pgfmathresult" I got the same error Incomplete \iffalse .... But the second one is working – user42070 Feb 28 '18 at 11:27
  • @user42070 I've modified it that was wrong syntax anyways. – percusse Feb 28 '18 at 11:50
3

Actually, pgf has macros precisely for this situation. The problem is that you do these things in \def, and there are the usual expansion problems (which I do not fully understand). Anyway, this runs without problems.

\documentclass{article}
\usepackage{pgf}
\begin{document}
\pgfmathrandominteger{\a}{2}{7}
\pgfmathrandominteger{\b}{2}{3}
\pgfmathsetmacro{\c}{ifthenelse(\a!=\b,\a,int(\a+1))}
\pgfmathsetmacro{\M}{int(\c)}
$\M$

\pgfmathrandominteger{\a}{2}{7}
\pgfmathrandominteger{\b}{2}{3}
\pgfmathsetmacro{\M}{int(ifthenelse(\a!=\b,\a,int(\a+1)))}
$\M$

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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