I have a macro \DefineStrEqCaseMacro which defines a macro that selects a value based on a parameter. With this, I am trying to replace the manual method:
\newcommand*{\SelectBasedOnValue}{beta}% Default Selector
\newcommand{\GreekSelector}[1][\SelectBasedOnValue]{%
\IfStrEqCase{#1}{%
{alpha}{\alpha}%
{beta}{\beta}%
{gamma}{\gamma}%
}[\chi]%
}%
%% Define this so we can step through the entire list of possible options
\csdef{GreekSelector List}{alpha, beta, gamma, <default>}%
with:
\DefineStrEqCaseMacro{GreekSelector}%
[\chi]% <-- Default value in case there is no match
{%
{alpha}{\alpha}%
{beta}{\beta}%
{gamma}{\gamma}%
}%
At the end I would like to be able to print ALL the possible values along with the default value (if any). The code below produces the desired result:
However, you notice that in \DefineStrEqCaseMacro, I have hard coded the list of possible values:
\IfValueTF{#2}{%
%% <> added to list ONLY if a default was specified}
\csdef{#1 List}{alpha, beta, gamma, <default>}%
}{%
\csdef{#1 List}{alpha, beta, gamma}%
}%
Question:
How do I automatically define this \csdef{} based on #3 provided to this macro. This list should contain all the first options to each pair that \IfStrEqCase expects.
Summary:
Given a parameter of the form:
{alpha}{\alpha}%
{beta}{\beta}%
{gamma}{\gamma}%
with an arbitrary number of elements, how can I define a comma separated list so that I can step through each of the possible matches based on the first parameter in these pairs.
Code:
\documentclass{article}
\usepackage{xstring}
\usepackage{xparse}
\usepackage{etoolbox}
\usepackage{tikz}% for \foreach
\newcommand*{\SelectBasedOnValue}{beta}
\NewDocumentCommand\DefineStrEqCaseMacro{% Simplified from actual use case
m% #1 = macro name to define
o% #2 = default value in case of no match
m% #3 = IfStrEq case parameters
}{%
%% https://tex.stackexchange.com/q/226987/4301
\expandafter\NewDocumentCommand\csname#1\endcsname{%
s% #1 = NOT used here
O{\SelectBasedOnValue}% #2 = selector
}{%
\IfValueTF{#2}{%
\IfStrEqCase{##2}{%
#3%
}[#2]%
}{%
\IfStrEqCase{##2}{%
#3%
}[\PackageError{\jobname}{Unknown selector "##2"}]%
}%
}%
\IfValueTF{#2}{% ----- ??? How create this list defined here???
%% <default> added to list ONLY if a default was specified}
\csdef{#1 List}{alpha, beta, gamma, <default>}%
}{%
\csdef{#1 List}{alpha, beta, gamma}%
}%
}%
%%% ---------------------------------------------------------- Manual Method
%%% This is the manual way of doing things, but is error prone
%\newcommand{\GreekSelector}[1][\SelectBasedOnValue]{%
% \IfStrEqCase{#1}{%
% {alpha}{\alpha}%
% {beta}{\beta}%
% {gamma}{\gamma}%
% }[\chi]%
%}%
Define this so we can step through the entire list of possible options
%\csdef{GreekSelector List}{alpha, beta, gamma, <default>}%
%%% ----------------------------------------------------------
%% ---------------------------------------------------------- Automated Method
\DefineStrEqCaseMacro{GreekSelector}%
[\chi]% <-- Default value in case there is no match
{%
{alpha}{\alpha}%
{beta}{\beta}%
{gamma}{\gamma}%
}%
%% ----------------------------------------------------------
\newcommand*{\TestGreekSelector}{%
Testing:
$\GreekSelector$, % <--- Select based on value of \SelectBasedOnValue
$\GreekSelector[alpha]$,
$\GreekSelector[Unknown]$.% <-- Select default case
}%
\newcommand*{\ShowAllPossibleValues}[1]{%
\par
List of ALL options of \texttt{\string#1}:\par
\edef\MyList{\csuse{#1 List}}%
\foreach \x in \MyList {
{\ttfamily\bfseries \x}: $\csuse{#1}[\x]$\par
}
}%
\begin{document}
%\TestGreekSelector
\ShowAllPossibleValues{GreekSelector}%
\end{document}



