[Tex/LaTex] Accent characters incorrectly written external file

unicodewrite file

I tried to write some strings in an external file for use in a special type of index, but using utf8 accented characters appear wrong.

In my external file, those characters appear in the same way as I wrote in my document and thess characters can't be read later.

My test code is:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[portuges]{babel}
\usepackage[T1]{fontenc}

\newif\iffirstSiglaUse\firstSiglaUsetrue

\newcommand{\sigla}[2]{%
    \iffirstSiglaUse
        \newwrite\arqEscreveSiglas%
        \immediate\openout\arqEscreveSiglas=\jobname.losg%
        \firstSiglaUsefalse
    \fi
    \immediate\write\arqEscreveSiglas{#1 #2}%
    #2 (#1)%
}

\AtEndDocument{%
    \iffirstSiglaUse\else\immediate\closeout\arqEscreveSiglas\fi
}

\makeatletter
\newcommand{\leArquivo}{%
    \def\dados{}
    \newread\arqLeSiglas%
    \openin\arqLeSiglas=\jobname.losg%
    \loop\unless\ifeof\arqLeSiglas%
        \read\arqLeSiglas to \dados%
        \dados\par%
    \repeat%
    \closein\arqLeSiglas%
}

\begin{document}

\leArquivo

A \sigla{EPUSP}{Escola Politécnica da Universidade de Sao Paulo} é uma instituição de ensino ligada à \sigla{USP}{Universidade de Sao Paulo} e visa o ensino em engenharia. A entidade representativa dos engenheiros formado na EPUSP é responsabilidade da \sigla{AEP}{Associacao dos Engenheiros Politecnicos} que promove a integração entre ex-alunos.

\end{document}

The answer is "! Package inputenc Error: Unicode char \u8:(something) not set up for use with LaTeX"

I read the inputenc documentation and I saw some aux files generated using utf8 characters and I realized that the correct representation of accented characters is "\IeC{…}", but my output file is not so.

My output file is:

EPUSP Escola Politécnica da Universidade de Sao Paulo
USP Universidade de Sao Paulo
AEP Associacao dos Engenheiros Politecnicos

Does anyone know what I did wrong?

Best Answer

Characters defined via inputenc are made robust via LaTeX's \protect mechanism. But they can not be used in a "moving argument" such as \write unless \protect is locally given an appropriate definition. Use \protected@write not \immediate\write (If you need \immediate then you'd need to set \protect "by hand" but usually for an index you would not want immediate writes.

Related Question