I would like to define a command that slightly differs depending on the currently selected language in my document. My code:
\documentclass{article}
\usepackage[english,french,german]{babel}
\newcommand{\mynewcommand}[4]{%
\newcommand{#1}{%
\iflanguage{english}{#2}{%
\iflanguage{french}{#3}{%
\iflanguage{german}{#4}{}%
}}%
}%
}
\mynewcommand{\uno}{one}{une}{eins}
\newcommand{\whatever}{whatever} % if this is commented out the code works as desired
\begin{document}
\selectlanguage{english}
\uno
\selectlanguage{french}
\uno
\end{document}
Why do I get an error thrown at \newcommand{\whatever}{whatever}
and how do I avoid that?
Note:
In an earlier version I oversimplified my MWE using only two languages, in that case no error is thrown
Best Answer
The definition seems to be "almost" correct, in that it gives almost the expected result, apart from spurious spaces:
However this is error prone if one wanted to augment the supported languages; with
xparse
it's easier:If other languages were desired it would be sufficient to add an argument to
\mynewcommand
and the suitable line along the same scheme.An alternative way might be to define different commands:
Unknown languages will result in "No def of
\command
for language"With
\newlanguagecommand{\uno}
one defines the command so that its expansion is\\uno<languagename>
; for example, when the language is French, writing\uno
will use\\unofrench
which is defined by\addtolanguagecommand{\uno}{french}{une}
to expand to "une".This should give no problems also in moving arguments as long as the replacement text doesn't contain fragile commands (text is OK).
A revamped
expl3
implementation. The translations are stored in a property list and used according to the current language.The defined command can receive an optional argument, which should be a valid language name, to force using the corresponding translation independent of the current language.