I have a tex file with some of my own custom functions, one of which is a switch case function.
Because I use \input to call this file from other documents, I need the output variable to be local.
However, I need a local variable to store the output while I iterate through the rest of the loop.
Either that or abort the loop and go to the end of the function.
I am using tex maker and pdftex
\usepackage{xparse}
\usepackage{fp}
\usepackage{xstring}
\usepackage{ifthen}
\usepackage{printlen}
\usepackage{listofitems}
\usepackage{forloop}
\usepackage{pgffor}
\NewDocumentCommand\SwitchCase{m m}
{
\setsepchar{,/-}
\readlist\Cases{#2}
\FPadd\CaseCount{\Caseslen}{0}
\def\out{}
\foreach \ct in {1,...,\CaseCount}
{
\ifthenelse{\equal{#1}{\Cases[\ct ,1]}}
{
\def\tmp{\Cases[\ct, 2]}
%\tmp
\def\out{\tmp}
}
{}
}
\out
}
%below is a sample of how this code would work
%not exactly how I would use it, but still valid example
\begin{document}
\SwitchCase{hello}{hel-1,he-2,hello-3,Hello-4}
\end{document}
Given the example, the function should return 3, however when I attempt to set the output variable in the loop, it returns either 0 or nothing.
This function has many possible uses in my documents therefore it has to be vague.
p.s. I apologize if any of the wording is awkward, I am not the best with syntax and word choice.
\foreach
starts a group. So the local value of\out
gets "forgotten". Either use\xdef\out{\tmp}
(or\gdef\out{\tmp}
) or use an ordinary\loop
or another loop that does not use groups (you load more than enough packages for that;-). – user194703 Dec 9 '19 at 16:58%
from ends of lines it would be simpler to write it just using expl3, however the main issue is that each iteration of a pgf loop is in a group so your\def
do not survive, you could usegdef
(or perhaps\xdef
depending on what you want the code to do) but netter not to use a loop that groups the items. – David Carlisle Dec 9 '19 at 17:00