Edit
Thanks to @PhelypeOleinik for pointing out the problem wasn't the \str_foldcase itself but the switch statement bodies trying to typeset text before the document has begun. The cognitive overload from trying to learn expl3 was so high that I lacked capacity to spot such a basic error.
New MWE
This works but doesn't print the value of the selected language, nor does it actually select a language. I'm not entirely sure the naming of the variable \l__myclass_lang_tl is "best practice" as this is my first time. Advice, corrections, and improvements on this would be welcome.
As I understand it so far, though I'm likely wrong or incomplete in multiple places:
`\l` means declare a local variable
`_` (the first one) is just a separator
`_` (the second one) is currently a mystery to me
`myclass` is referencing the name space of the class I'm creating
`_` is just a separator for readability because we're using snake_case
`lang` is the meaningful part of variable name
`_` another separator
`tl` I don't fully understand yet: I know it stands for "token list"
but I'm not sure why this has to be a token list.
I was expecting "str" for "string"
\documentclass[]{article}
\usepackage{xparse}
\ExplSyntaxOn
\tl_new:N \l__myclass_lang_tl
\NewDocumentCommand \selectLang { m }
{
\str_case_e:nnF { \str_foldcase:e { #1 } }
{
{ english } { \tl_set:Nn \l__myclass_lang_tl {english} }
{ norsk } { \tl_set:Nn \l__myclass_lang_tl {norsk} }
{ nynorsk } { \tl_set:Nn \l__myclass_lang_tl {nynorsk} }
{ samisk } { \tl_set:Nn \l__myclass_lang_tl {samisk} }
{ samin } { \tl_set:Nn \l__myclass_lang_tl {samin} }
}
{\tl_set:Nn \l__myclass_lang_tl {error}}
}
\cs_generate_variant:Nn \str_foldcase:n { e }
\ExplSyntaxOff
\selectLang{Samin}
\renewcommand\selectlanguage{} % just to show that it works
\begin{document}
Chosen language: \selectLang{EnGlIsH}
\def\foo{NOrsK}
Chosen language: \selectLang{\foo}
\end{document}
Original (for posterity)
I believe I'm having trouble with \str_foldcase needing an active char but I'm not certain.
I'm writing my first class file and want to utilise expl3. I've only just started with expl3 and I'm still at the 'confused beginner' stage. I'm feeling very frustrated that I haven't been able to work this out for myself.
For class options which can take parameters, eg \documentclass[fruit=banana]{fruity} (with other valid options being apple and cherry) I want to process these in a case-insensitive way and found this promising-looking example which uses a case-insensitive switch statement to create a \selectLang command.
However when I try to call the \selectLang command it creates while still in the preamble (line 18) this causes a no begin document error.
I've done what I can to research this and it seems to be something about active char which I don't currently understand. I have seen this but I don't understand the answer and since it dates from 2015 and talks about a new mechanism, it seems out of date. This seems more modern but I still don't understand.
I have looked at expl3.pdf xparse.pdf and interface3.pdf but I'm too new to expl3 to really understand them. And as far I can tell none have any examples I can find showing how to use any variant of \str_foldcase so that it will work in the preamble.
MWE
This is my modified version of the original question. I know it's not a class I'm just trying to make case-insensitive string comparisons work in the preamble. The only two changes I've made to the original example are first to remove the \selectlanguage macro call because that generates an understandable error because you can't select a language at that stage, and second to add line 18 which calls \selectLang while still in the preamble.
I get the same error whether the \selectLang call in the preamble is before or after \ExplSyntaxOff
Removing \str_foldcase:e and corresponding braces means there is no begin document error which is what makes me think the problem is with \str_foldcase being run inappropriately. It works fine when run from the document contents.
\documentclass[]{article}
\usepackage{xparse}
\ExplSyntaxOn
\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 }
\ExplSyntaxOff
\selectLang{Samin} % Causes an error about a missing begin document
\renewcommand\selectlanguage{} % just to show that it works
\begin{document}
Chosen language: \selectLang{EnGlIsH}
\def\foo{NOrsK}
Chosen language: \selectLang{\foo}
\end{document}
generates
! LaTeX Error: Missing \begin{document}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.18 \selectLang{Samin}
You're in trouble here. Try typing <return> to proceed.
\str_case_e:nntries to typesetselectlanguage <name>, so you can't use it in a class file (which is before\begin{document}). – Phelype Oleinik Aug 18 '21 at 22:34expl3that I failed to recognize that the problem is not\str_case_eper se, but the code in the switch statement that's trying to typeset text before the document proper has begun. I've modified the code so that the switch statement sets a variable contents and it now works when I call it from the preamble. – Doc Octal Aug 18 '21 at 23:36expl3! – Phelype Oleinik Aug 19 '21 at 01:45