10

With the following MWE, I can write the € symbol into the file euro.txt

\documentclass{article}
\newwrite\tempfile
\immediate\openout\tempfile="euro.txt"

\begin{document}
Write Euro symbol into a file
\immediate\write\tempfile{10 €}%
\immediate\closeout\tempfile

\end{document}

My problem is that I use \usepackage[utf8]{inputenc}, and with this encoding, the symbol € produces a bug. What could i do ?

%\usepackage[utf8]{inputenc}
lockstep
  • 250,273
Loic Rosnay
  • 8,167

1 Answers1

11

Writing out Unicode characters is a pain. It mostly depends on what you want that it is written in the file.

If you can, the simplest way out is

\immediate\write\tempfile{10\unexpanded{€}}

but this may not be what you're looking for. So let's try a different strategy.

First of all you need an "immediate" version of \protected@write:

\makeatletter
\newcommand\protected@iwrite[3]{%
  \begingroup
  \let\thepage\relax
  #2%
  \let\protect\@unexpandable@protect
  \edef\reserved@a{\immediate\write #1{#3}}%
  \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi
}
\makeatother

Then this will work:

\documentclass{article}
\usepackage{textcomp}
\usepackage[utf8]{inputenc}

\makeatletter
\newcommand\protected@iwrite[3]{%
  \begingroup
  \let\thepage\relax
  #2%
  \let\protect\@unexpandable@protect
  \edef\reserved@a{\immediate\write #1{#3}}%
  \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi
}
\makeatother

\newwrite\tempfile
\immediate\openout\tempfile="euro.txt"

\begin{document}
Write Euro symbol into a file and in the document: €
\makeatletter
\protected@iwrite\tempfile{}{10€}%
\makeatother
\immediate\closeout\tempfile

\end{document}

This will write

10\IeC {\texteuro }

which still may not be what you need.

If you want a literal € character, you can do in this way

\documentclass{article}
\usepackage{textcomp}
\usepackage[utf8]{inputenc}

\usepackage{newunicodechar}
\makeatletter
\newunicodechar{€}{\ifx\protect\@unexpandable@protect\unexpanded{€}\else\texteuro\fi}

\newcommand\protected@iwrite[3]{%
  \begingroup
  \let\thepage\relax
  #2%
  \let\protect\@unexpandable@protect
  \edef\reserved@a{\immediate\write #1{#3}}%
  \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi
}
\makeatother

\begin{document}
Write Euro symbol into a file and in the document: €
\makeatletter
\protected@iwrite\tempfile{}{10€}%
\makeatother
\immediate\closeout\tempfile

\end{document}

This will write

10€

Notice however that you need \usepackage{textcomp} to give a meaning to .

egreg
  • 1,121,712