Am 03.12.2017 um 15:01 schrieb Ulrike Fischer:
I don't know which problem you have here. Both variants in the
following document writes utf8:

Yes. But you do use \unexpanded:

\immediate\write\testb{\unexpanded{Grüße}}

If an environment is implemented as in the attached MWE it is impossible to use 
\unexpanded ...
or the solution to the problem is beyond my knowledge of TeX.

Knut
\NeedsTeXFormat{LaTeX2e}
\documentclass[11pt]{letter}

%
% Compile this utf8 encode file with latex, pdflatex, lualatex and xelatex
% and inspect the file mweUtf8Problem.testout after every run.
% You will see that xelatex and lualatex produce valid utf8 encoded output
% in mweUtf8Problem.testout and that latex and pdflatex fail to do so.
%
% If you enable \stopUTFeight and \restartUTFeight (lines 58 and 64) all
% engines mentioned above produce valid output in mweUtf8Problem.testout.
%
% It would be nice to have something like \inputencoding{off} to implement
% a clean way to temporarily disable inputenc.
%

\usepackage{iftex}
\def\isLuaTeX{\ifLuaTeX 1\else 0\fi}
\def\isXeTeX{\ifXeTeX 1\else 0\fi}
\if\isLuaTeX\isXeTeX % latex or pdflatex
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \makeatletter
    \let\savedUTFviii\UTFviii@two@octets
    \def\stopUTFeight{\gdef\UTFviii@two@octets##1##2{##1\string##2}}
    \def\restartUTFeight{\global\let\UTFviii@two@octets\savedUTFviii}
    \makeatother
\else % lualatex or xelatex
    \def\stopUTFeight{}
    \def\restartUTFeight{}
\fi

\newwrite\tmpFileMain
\def\tmpNameFull{mweUtf8Problem.testout}

{
  \catcode`|=0 \catcode`[=1 \catcode`]=2 \catcode`\#=12
  \catcode`\{=12 \catcode`\}=12 \catcode`\\=12
  |gdef|eofrag[\end{fragment}]
  |gdef|hashtag[#]
  |gdef|escape[\]
]

{
  \obeylines
  \gdef\doline#1
  {
    \def\oneline{#1}
    \ifx\oneline\eofrag\def\next{\end{fragment}}
    \else\immediate\write\tmpFile{\oneline}\let\next\doline
    \fi\next%
  }
}

\makeatletter

\newenvironment{fragment}[2]{
  % \stopUTFeight
  \immediate\openout\tmpFileMain=\tmpNameFull
  \gdef\tmpFile{\tmpFileMain}
  \let\do=\@makeother\dospecials\obeylines\doline
}{
  \immediate\closeout\tmpFileMain
  % \restartUTFeight
}

\makeatother

\begin{document}
Deutsche Umlaute und Sonderzeichen: ä,ö,ü,ß,Ä,Ö,Ü

\begin{fragment}{10 cm}{18 cm}
Deutsche Umlaute und Sonderzeichen: ä,ö,ü,ß,Ä,Ö,Ü
\end{fragment}

\end{document}

Reply via email to