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.



% This allows the user to switch languages using a myriad of synonyms
% The four provided languages are: English, Norsk, Nynorsk and Samin
    % English synonyms
    % Norsk (Bokmål) synonyms
    % Nynorsk synonyms
    % Samin synonyms

% Helper functions to create the switch case above






\languagename % Displays english as it should


\languagename % This should display norsk


\languagename % This should display nynorsk

  • I think you don't want the command to be expandable (since \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. Commented Sep 14, 2019 at 13:24
  • Yeah, the command is not supposed to be expandable. Only to expand the arguments =) Commented Sep 14, 2019 at 13:26

1 Answer 1


With the kind help of expl3, this is expandable (if the code of the cases is, so with the normal definition of \selectlanguage it isn't).

EDIT: as pointed out by @JosephWright, \str_foldcase:e is to be used for this kind of task. As a general rule of thumb: For processing use \str_foldcase:n, for text which should be printed out use \str_lowercase:n.


\NewDocumentCommand \selectLang { m }
    \str_case_e:nn { \str_foldcase:e { #1 } }
        { english } { \selectlanguage{british} }
        { norsk }   { \selectlanguage{norsk} }
        { nynorsk } { \selectlanguage{nynorsk} }
        { samisk }  { \selectlanguage{samisk} }
        { samin }   { \selectlanguage{samin} }
\cs_generate_variant:Nn \str_foldcase:n { e }

\renewcommand\selectlanguage{} % just to show that it works

Chosen language: \selectLang{EnGlIsH}

Chosen language: \selectLang{\foo}
  • Perhaps \NewDocumentCommand or at least \cs_new_protected:Npn, since \selectlanguage doesn't expand. Commented Sep 14, 2019 at 13:35
  • @PhelypeOleinik yes, that would be correct with the normal definition of \selectlanguage (not with the redefinition). I'll change it :)
    – Skillmon
    Commented Sep 14, 2019 at 13:36
  • 2
    Explainer for beginners: \str_foldcase:e doesn't exist by default and needs creating with the \cs_generate_variant statement. This generate_variant must be executed before the \selectLang command is used otherwise you get an error. This is independent of the order in which the code is written. As a beginner myself I found it helpful to put the generate_variant physically before the \NewDocumentCommand that makes use of it.
    – Doc Octal
    Commented Aug 20, 2021 at 8:23
  • 2
    Another two traps for beginners I fell into. First the \selectlanguage command might not work well if executed (as opposed to just written as it is in the answer) from the preamble, that is executed before \begin{document} Second I had problems by executing commands in the preamble that tried to typeset text. I was so enchanted and frustrated by expl3 that I forgot you can't put text into a document that hasn't started yet. These two things seem obvious now but they absolutely weren't at the time.
    – Doc Octal
    Commented Aug 20, 2021 at 8:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .