I imagine this has to be asked somewhere but I've been searching for a while with no luck, so if there is an answer feel free to just direct me there.
I have a series of functions that all do basically the same thing, but with a variable number of inputs. I would like to call one function with one comma separated list of inputs, have the function find the length of the input and call the appropriate function with the appropriate inputs. Unfortunately I don't really understand the comma separated list syntax and can't find a good primer for it. An example of what I want (pseudocode):
\newcommand{\RandQ}[1]{
%Set \length to be the length of the input to \RandQ list
\renewcommand{\length}{length{#1}}
%Now call the appropriate function with the appropriate inputs
ifthenelse{\length = 5}{\RandFiveQ{#1}{#2}{#3}{#4}{#5}}{}
%Where #1, #2, #3, etc are the 1st, 2nd, 3rd, etc entries of the list from the input above
ifthenelse{\length = 4}{\RandFourQ{#1}{#2}{#3}{#4}}{}
ifthenelse{\length = 5}{\RandThreeQ{#1}{#2}{#3}}{}
ifthenelse{\length = 5}{\RandTwoQ{#1}{#2}}{}
}
Where \RandFiveQ
, \RandFourQ
, \RandThreeQ
, \RandTwoQ
are already defined functions that take 5, 4, 3, and 2 inputs respectively.
Example usage would be something like
\RandQ{1, 2, 3, 4}
Which would return
\RandFourQ{1}{2}{3}{4}
which would then get parsed as the actual command \RandFourQ.
As per request I am including the code for \RandXQ commands. I will add that this project was in an effort to circumvent some bad code in a required cls file, as well as learn how to code in latex itself... so there might be some Rube-Goldberging in here... you have been warned :)
Essentually, I have pre-generated a randomized list of counters A1-AX (RandFourQ generated A1 through A4, and randomly assigned the numbers 1 through 4 to them... e.g. A1 = 3, A2 = 2, A3 = 1, A4 = 4), then for each RandXQ the first input is the answer order, then the following are the answer choices. If the first one is 3, it outputs the 3rd answer choice, and so I can randomize answer orders (for a multiple choice test for example). Worth noting there are much easier ways to do this, but the required cls file killed off most of those choices unfortunately.
%Syntax: For each "choice" You want to type The following:
%\RandMe{n}{A}{}
%\(n)choices
%\Rand(n)Q{\arabic{A1}}{Answer1}{Answer2}...{Answer n}
%\Rand(n)Q{\arabic{A2}}{Answer1}{Answer2}...{Answer n}
%.
%.
%.
%\Rand(#ofchoices)Q{An}{Answer1}{Answer2}...{Answer n}
%
%Where n is the number of questions choices there are Note that the number of choices "n" in the \Rand(n)Q must be spelled out with the first letter capitalized. Thus for 5 choices you would type "\RandFiveQ".
%So for example with a question that has 4 choices of "2", "4", "17" and "No Answer" you would type:
%
%\RandMe{4}
%\fourchoices
%{\RandFourQ{\arabic{A1}}{2}{4}{17}{No Answer}}
%{\RandFourQ{\arabic{A2}}{2}{4}{17}{No Answer}}
%{\RandFourQ{\arabic{A3}}{2}{4}{17}{No Answer}}
%{\RandFourQ{\arabic{A4}}{2}{4}{17}{No Answer}}
%
%Currently supported are: \RandFiveQ, \RandFourQ, \RandThreeQ, \RandTwoQ.
%Also required is \usepackage{calc}, \usepackage{lcg}
%
\newcommand{\RandFiveQ}[6]
{
\ifthenelse{#1 = 1}{#2}{}
\ifthenelse{#1 = 2}{#3}{}
\ifthenelse{#1 = 3}{#4}{}
\ifthenelse{#1 = 4}{#5}{}
\ifthenelse{#1 = 5}{#6}{}
}
\newcommand{\RandFourQ}[5]
{
\ifthenelse{#1 = 1}{#2}{}
\ifthenelse{#1 = 2}{#3}{}
\ifthenelse{#1 = 3}{#4}{}
\ifthenelse{#1 = 4}{#5}{}
}
\newcommand{\RandThreeQ}[4]
{
\ifthenelse{#1 = 1}{#2}{}
\ifthenelse{#1 = 2}{#3}{}
\ifthenelse{#1 = 3}{#4}{}
}
\newcommand{\RandTwoQ}[3]
{
\ifthenelse{#1 = 1}{#2}{}
\ifthenelse{#1 = 2}{#3}{}
}
\RandXQ
actually does. I think, this could be done with\clist
features and\clist_map_function
fromexpl3
\RandFiveQ
actually takes 6 arguments,\RandFourQ
takes 5 arguments, etc? Please confirm.