# pgfmathsetmacro not working

Why is this not working please:

\newcommand\bareme[1]
{\ifthenelse{\equal{#1}{1}}{5}{}
\ifthenelse{\equal{#1}{2}}{8}{}
\ifthenelse{\equal{#1}{3}}{7}{}
\ifthenelse{\equal{#1}{4}}{9}{}
\ifthenelse{\equal{#1}{5}}{1}{}}

\pgfmathsetmacro{\points}{\bareme{1}}


Thanks

• (1) welcome, (2) please always post full minimal examples that others can test as is. Here we have to add a lot of stuff before this even have a chance of compiling. If it is always handling numbers, \ifthenelse is probably overkill. – daleif Oct 12 '17 at 7:57
• If you're only doing integer comparison, could tex.stackexchange.com/a/17678 be useful? – Torbjørn T. Oct 12 '17 at 8:56

The main problem with your code is that you are not using the correct syntax. If you look section 89.3.5 of the current pgf manual (version 3.0.1a), then you will find that pgf provides a \pgfmathifthenelse{x}{y}{z} command that you use inside \pgfmathparse, or \pgfmathsetmacro, as the function ifthenelse(x,y,z). So, your code is using the syntax for the standalone macro \pgfmathifthenelse whereas you should be using the code for the internal ifthenelse pgfmath function. Further, the example given in the manual for ifthenelse is

\pgfmathparse{ifthenelse(5==4,"yes","no")} \pgfmathresult


so your use of \equal{...} is incorrect and unnecessary.

Secondly, to me it seems a bad idea to define a macro that you then give to \pgfmathsetmacro. Instead I would make your macro set \points directly:

\newcommand\bareme[1]{%
\pgfmathsetmacro{\points}{ifthenelse(#1==1,5,
ifthenelse(#1==2, 8,
ifthenelse(#1==3, 7,
ifthenelse(#1==4, 9,
ifthenelse(#1==5, 1,"")))))}%
}


This certainly works. On the other hand, there is a much simpler way to do what you want using the TeX \ifcase command:

\newcommand\Bareme[1]{%
\ifcase #1\or 5% have to skip 0
\or 8%
\or 7%
\or 9%
\or 1%
\else ?%
\fi%
}


For completeness, here is the full code:

\documentclass{article}
\usepackage{pgfmath}

\newcommand\bareme[1]{%
\pgfmathsetmacro{\points}{ifthenelse(#1==1,5,
ifthenelse(#1==2, 8,
ifthenelse(#1==3, 7,
ifthenelse(#1==4, 9,
ifthenelse(#1==5, 1,"")))))}%
}

\newcommand\Bareme[1]{%
\ifcase #1\or 5% have to skip 0
\or 8%
\or 7%
\or 9%
\or 1%
\else ?%
\fi%
}
\begin{document}
\bareme{1} \points

\Bareme{1}
\Bareme{2}
\Bareme{3}

\end{document}

• I think OP is using ifthen package but your answer still holds. – percusse Oct 12 '17 at 17:05
• @percusse As the OP didn't provide a full minimal working example it's not clear but I thought that they were using pgf because they are using \pgfmathsetmacro. On the other hand, perhaps they are using ifthen and the use of \pgfmathsetmacro is the source of confusion... – Andrew Oct 12 '17 at 17:26

It doesn't work because \ifthenelse{\equal{1}{#1}}{A}{B} is a set of instructions for printing either A or B, but doesn't produce the result in a form that \pgfmathsetmacro is happy with (it's not fully expandable).

If the legal input are integers from 1 to 5, you can use an array:

\documentclass{article}
\usepackage{pgfmath}

\newcommand*{\baremeoutput}{{5,8,7,9,1}}
\newcommand*{\bareme}[1]{%
\pgfmathsetmacro{\points}{\baremeoutput[#1-1]}%
}

\begin{document}

\bareme{1}\points

\bareme{2}\points

\bareme{3}\points

\bareme{4}\points

\bareme{5}\points

\end{document}


Since PGF arrays start from zero, one has to shift the index.

A different approach with expl3, that can work also with non consecutive indices.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\bareme}{m}
{
\tl_set:Nf \points
{
\int_case:nn { #1 }
{
{1}{5}
{2}{8}
{3}{7}
{4}{9}
{5}{1}
}
}
}
\ExplSyntaxOff

\begin{document}

\bareme{1}\points

\bareme{2}\points

\bareme{3}\points

\bareme{4}\points

\bareme{5}\points

\end{document}


• I vote always my friends :-). Hi, +1 for you and Andrew, – Sebastiano Oct 12 '17 at 17:30