I'm having an hard time understanding how to use the various options etoolbox
has for comparing strings. Let's consider this MWE:
\documentclass[11pt, a4paper]{article}
\usepackage{etoolbox}
\newcounter{ContNationality}
\setcounter{ContNationality}{2}
\newcommand{\Nationality}{%
\ifcase\value{ContNationality}\or%
{ita}\or{eng}\or{fra}%
\fi%
}%
\begin{document}
\expandafter\ifdefstring{\Nationality}{ita}{You are italian}{}%
\expandafter\ifdefstring{\Nationality}{eng}{You are english}{}%
\expandafter\ifdefstring{\Nationality}{fra}{You are french}{}%
\end{document}
I put that \expandafter
because \ifdefstrings
won't expand the content of the command, according to the package guide. Yet, if I execute such code, with or without the \expandafter
, the resulting pdf will only include a paragraph with a "b". Quite of an astonishing outcome, I have to say.
So, how can I manage this situation and have the conditionals there to work as I want?
EDIT:
As you pointed out, I wrote \ifvalue
instead of \ifcase\value
. My mistake.
EDIT 2:
I didn't state this because I thought it wasn't important: the value of \Nationality
needs to be dynamic, because actually in the main document I'm working on it is called 100 times, always with a different value of the ContNationality
counter.
The fact that there is such counter is more or less fixed by the fact that the value of \Nationality
needs to be random, and that I'm using the lcg
package which assigns random values to counters. So, basically, I give a random value to that counter and then I use it to determine the random value of \Nationality
.
A larger MWE would be the following. I implemented Steven B. Segletes's solution – which works perfectly, I see – which looked like the most user-friendly option to me. I saw egreg's one, and I'm sure it works, but if I copy-paste it in my document I receive a very long list of errors, probably given by the fact that I don't have the very-last version of LaTeX, and I don't understand how to download it. Moreover, I would like to avoid LaTeX3-based solutions because I really don't know anything of that language and I'd prefer to use answers that I understand how they work, so that of course I can work with them in case I need to change my code's behaviour.
\documentclass{article}
\usepackage{etoolbox}
\usepackage[last=2147483647]{lcg}
\usepackage{xifthen}
\newcounter{ContNationality}
\setcounter{ContNationality}{2}
\newcounter{Cycles}
\newcounter{SEED}
\newcommand{\Random}[3]{%
\providecommand{\NewSeed}{%
\reinitrand[last=2147483647, seed=\value{SEED}]%
\rand%
\setcounter{SEED}{\therand}%
\typeout{Il seed attualmente è \theSEED}%
}%
\reinitrand[first=#1,last=#2,seed=\value{SEED}]%
\rand%
\setcounter{#3}{\value{rand}}%
\NewSeed%
}
\newcommand{\Nationality}{%
\edef\theNationality{%
\ifcase\value{ContNationality}%
\or ita%
\or eng%
\or fra%
\fi%
}%
}
\begin{document}%
%
\rand%
\setcounter{SEED}{\value{rand}}%
\setcounter{Cycles}{0}%
\whiledo{\value{Cycles}<10}{%
\addtocounter{Cycles}{1}%
\Random{1}{3}{ContNationality}%
\Nationality%
\ifdefstring{\theNationality}{ita}{You are Italian}{}%
\ifdefstring{\theNationality}{eng}{You are English}{}%
\ifdefstring{\theNationality}{fra}{You are French}{}%
\\
}%
\end{document}
Best Answer
David already explained why the
\epandafter
is useless. But the second one which would expand\Nationality
wouldn't help either, since the definition of\Natioanlity
never is the same as the strings you are testing it with. At some point you must expand it completely before the comparison. The easiest way is to\edef
\Nationality
(unless you don't want it to be fixed but be able to change it mid-document). BTW: there is no\ifvalue
that I know of. I guess you want\ifcase\value{...}
:PS: just seeing that egreg already explained the issues. I'll leave the answer anyway as it doesn't use expl3.