0

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}
4
  • \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.
    – moewe
    Aug 10, 2021 at 6:06
  • 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 and polyglossia offer several ways to make commands language-aware.
    – moewe
    Aug 10, 2021 at 6:32
  • @moewe I have built a command \UseLanguage that calls \selectlanguage internally, supporting up-casing names like English or abbreviations like EN, see ctan.org/pkg/projlib. The \StrToABBR was an attempt to simplify the existed code.
    – Jinwen
    Aug 10, 2021 at 6:39
  • you are mixing levels. You are defining a "Document Command" and then you are trying to use it for programming tasks. That is normally wrong and adding randomly expandafter isn't the solution here. Use the right tools from the start, e.g. from etoolbox or expl3. Aug 10, 2021 at 6:46

0

You must log in to answer this question.

Browse other questions tagged .