# environment with local \newunicodechar{ä}{ae} or similar — is it possible?

I'm using xelatex with utf8-input. I'm trying to write a LaTeX environment inside of which every source code ä is printed as ae, every ö as oe etc. I found a number of ways that don't work, but so far none that does. I put the error messages under the respective lines of code:

<!-- language: lang-latex -->
\documentclass{minimal}
\XeTeXinputencoding{utf8}
\usepackage{xltxtra}
\usepackage[ngerman]{babel}
\usepackage{newunicodechar}
%------------------------------------------------------------
\newenvironment{noumlauts}{%
% Neither of these works:
%   \newunicodechar{ä}{ae}
%   ! LaTeX Error: Can be used only in preamble.
%
%   \DeclareUnicodeCharacter{00E4}{ae}
%   ! Undefined control sequence. (\DeclareUnicodeCharacter)
%
%   \catcodeä=\active\def ä{ae}%
%  ! Missing control sequence inserted.
%  <inserted text>
%                  \inaccessible
%
%   \makeatletter\@namedef{u8:\detokenize{ä}}{ae}\makeatother
%     ! You can't use \spacefactor' in vertical mode.
%     \@ ->\spacefactor
%                      \@m {}
}{}
%------------------------------------------------------------
\begin{document}
\begin{noumlauts}
\noindent Bräsige Mägen\\
\emph{should look exactly like}\\
Braesige Maegen
\end{noumlauts}
\end{document}

-

Define a conditional and redefine ä to do something when the condition is true and something else when the condition is false.

\documentclass{article}

\usepackage[ngerman]{babel}
\usepackage{fontspec}

\usepackage{newunicodechar}

\newif\ifnoumlauts
\newenvironment{noumlauts}
{\noumlautstrue}
{}
\newunicodechar{ä}{\ifnoumlauts ae\else ä\fi}

\begin{document}
\noindent Bräsige Mägen (with umlauts)

\bigskip

\begin{noumlauts}
\noindent Bräsige Mägen\\
\emph{should look exactly like}\\
Braesige Maegen
\end{noumlauts}

\end{document}


Note that \XeTeXinputencoding should not be used. Also, don't load xltxtra unless you really need its features (probably not), but rather fontspec.

You can ease defining the characters by an abstraction:

\newcommand{\defnoumlauts}[2]{%
\newunicodechar{#1}{\ifnoumlauts #2\else #1\fi}
}
\defnoumlauts{ä}{ae}
\defnoumlauts{ö}{oe}
\defnoumlauts{ü}{ue}

-
Yes, @egreg, (I called the conditional ifTT), this way I can skip the environment and just have a group {\ifTT ä becomes ae}. And in the same way I can turn en-dashes into single dashes, guillemets into single straight quotes etc. (I'm playing with some reenactments of historic typewriter fonts.) – Blackface May 27 '14 at 14:01
\documentclass{minimal}
\usepackage{fontspec}
\usepackage[ngerman]{babel}

\begingroup
\gdef\changeumlautcatcodes{%
\catcode\ä=13}
\changeumlautcatcodes
\gdef\switchumlauts{%
\defä{ae}}
\endgroup

%------------------------------------------------------------
\newenvironment{noumlauts}{%
\changeumlautcatcodes\switchumlauts}{}
%------------------------------------------------------------
\begin{document}
\begin{noumlauts}
\noindent Bräsige Mägen\\
\emph{should look exactly like}\\
Braesige Maegen
\end{noumlauts}

Bräsige
\end{document}
`
-