I have spent the last few hours trying to come up with a good solution for my problem, but alas all solutions falls short. I want to create a switchcase for a function (In reality I need many different kinds, not just one for language, but I added it as an concrete example.)
My ideal switch case function has the following functionality
- It accepts commands as inputs (fully expandable)
- It is case insensitive
My code below is a switch case, but it fails both of the above requirements. Any help figuring out a proper way to do this is more than welcome.
\documentclass{article}
\usepackage[norsk,nynorsk,british,samin]{babel}
\usepackage{pdftexcmds}
% This allows the user to switch languages using a myriad of synonyms
% The four provided languages are: English, Norsk, Nynorsk and Samin
\makeatletter
\newcommand*{\selectLang}[1]{%
\stringcases%
{#1}%
{%
% English synonyms
{english}{\selectlanguage{british}}%
%
% Norsk (Bokmål) synonyms
{norsk}{\selectlanguage{norsk}}%
%
% Nynorsk synonyms
{nynorsk}{\selectlanguage{nynorsk}}%
%
% Samin synonyms
{samisk}{\selectlanguage{samin}}%
{samin}{\selectlanguage{samin}}%
}%
{}%
}
% Helper functions to create the switch case above
\newcommand{\stringcases}[3]{%
\romannumeral
\str@case{#1}#2{#1}{#3}\q@stop
}
\newcommand{\str@case}[3]{%
\ifnum\pdf@strcmp{\unexpanded{#1}}{\unexpanded{#2}}=\z@
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\str@case@end{#3}}
{\str@case{#1}}%
}
\newcommand{\str@case@end}{}
\long\def\str@case@end#1#2\q@stop{\z@#1}
\makeatother
\newcommand{\temptwo}{nynorsk}
\begin{document}
\newcommand{\temp}{\temptwo}
\languagename
\selectLang{english}
\languagename % Displays english as it should
\selectLang{Norsk}
\languagename % This should display norsk
\selectLang{\temp}
\languagename % This should display nynorsk
\end{document}
\selectlanguage
is not). I think you want it to expand it's argument, right? In that case, simply remove the two\unexpanded
in the definition of\str@case
.