I have a macro recoding the language:
\def\mylang{English}
and a command to convert the language name into its abbreviation:
\NewDocumentCommand{\StrToABBR}{m}{%
\expandafter\lowercase{\IfStrEqCase{#1}}{%
{english}{EN}%
}%
}
Then there is a macro whose name ends with the abbreviation:
\def\mymacroEN{}
I would like to append some text to the last macro like this: \gappto{\csname mymacro\StrToABBR{\mylang}\endcsname}{...}. Of course some control to the expansion is needed, thus I added a few \expandafter:
\expandafter\gappto\expandafter{\csname mymacro\expandafter\StrToABBR\expandafter{\mylang}\endcsname}{Some English text}
However this causes the error Missing \endcsname inserted.
While writing the current question, I saw this question, and thought that this problem is probably caused by the use of \lowercase. However, even if I remove it and change \StrToABBR into
\NewDocumentCommand{\StrToABBR}{m}{%
\IfStrEqCase{#1}{%
{English}{EN}%
}%
}
the error remains. What's wrong here and how can I fix this problem?
Below is a MWE.
\documentclass{article}
\usepackage{etoolbox,xstring}
\NewDocumentCommand{\StrToABBR}{m}{%
\expandafter\lowercase{\IfStrEqCase{#1}}{%
{english}{EN}%
}%
}
% \NewDocumentCommand{\StrToABBR}{m}{%
% \IfStrEqCase{#1}{%
% {English}{EN}%
% }%
% }
\def\mymacroEN{}
\def\mylang{English}
\begin{document}
% EN
\StrToABBR{English}
% EN
\expandafter\StrToABBR\expandafter{\mylang}
% Works: Some English text
% \expandafter\gappto\expandafter{\csname mymacroEN\endcsname}{Some English text}
% Doesn't work
\expandafter\gappto\expandafter{\csname mymacro\expandafter\StrToABBR\expandafter{\mylang}\endcsname}{Some English text}
\mymacroEN
\end{document}
\StrToABBRis not expandable (definitely because it is defined via\NewDocumentCommandwhich makes robust commands, but it is possible that its definition would also prevent it from being expandable even if you used\NewExpandableDocumentCommand), so it does not expand to a 'string' in between\csname...\endcsname. – moewe Aug 10 '21 at 06:06xstring's\IfStrEqCaseis not expandable, so even if you used\NewExpandableDocumentCommandyour command still would not work. You'd need to find a different way to do this. I'm wondering if there is not a much, much easier way of going about this. What are you trying to do?babelandpolyglossiaoffer several ways to make commands language-aware. – moewe Aug 10 '21 at 06:32\UseLanguagethat calls\selectlanguageinternally, supporting up-casing names likeEnglishor abbreviations likeEN, see https://ctan.org/pkg/projlib. The\StrToABBRwas an attempt to simplify the existed code. – Jinwen Aug 10 '21 at 06:39