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}
\StrToABBR
is not expandable (definitely because it is defined via\NewDocumentCommand
which 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
.xstring
's\IfStrEqCase
is not expandable, so even if you used\NewExpandableDocumentCommand
your 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?babel
andpolyglossia
offer several ways to make commands language-aware.\UseLanguage
that calls\selectlanguage
internally, supporting up-casing names likeEnglish
or abbreviations likeEN
, see ctan.org/pkg/projlib. The\StrToABBR
was an attempt to simplify the existed code.