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'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}
share|improve this question

2 Answers 2

up vote 9 down vote accepted

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}

enter image description here

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}
share|improve this answer
    
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 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}
share|improve this answer

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.