Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I wish to keep the language in a command

\newcommand{\testlang}{english}
\begin{otherlanguage}{\testlang}
 ...
\end{otherlangauge}

However, \testlang is not expanded, and i get the error

! Package babel Error: You haven't defined the language testlang yet.

I've seen many tricks related to macro expansions, but I don't understand them well enough to know how to apply them in this scenario.

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

You can expand the argument manually using the following techniques. The easiest thing would be to define your own wrapper environment.

% Expand the argument once:
\newenvironment{Otherlanguage}[1]{%
  \expandafter\otherlanguage\expandafter{#1}%
}{\endotherlanguage}


% Alternative: argument is fully expanded
\newenvironment{Otherlanguage}[1]{%
  \begingroup
  \edef\temp{\endgroup\noexpand\otherlanguage{#1}}%
  \temp
}{\endotherlanguage}

% usage    
\begin{Otherlanguage}{\testlang}
 ...
\end{Otherlangauge}
share|improve this answer
    
Thank you. Is there a reason why otherlanguage shouldn't try to expand this by default? –  Mikael Öhman Apr 5 '11 at 13:40
    
@Mikael Öhman: It's much saver to not expand it inside \edef. If it contains things which can't be expanded but must be executed, like assignments, than it will break inside \edef. This is very similar to the fragile vs. robust issue in sectioning commands. –  Martin Scharrer Apr 5 '11 at 13:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.