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 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?

share|improve this question
    
As a side node: \openin\arqLeSiglas=\jobname.losg% should be better \openin\arqLeSiglas=\jobname.losg. Here the space produced by the end of line works as file name terminator and is then removed. You actually want this in this case. –  Martin Scharrer Jun 1 '12 at 22:59
add comment

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
I just remove \immediate in \write command and \closeout and my problem was solved –  Caio Jun 6 '12 at 20:40
add comment

Since you want essentially to write verbatim I suggest

\immediate\write\stream{\unexpanded{...}}

Here's how I modified your document:

\documentclass{article}

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

%%% Allocate a write stream and close it at end document
\newwrite\arqEscreveSiglas
\AtBeginDocument{
  \immediate\openout\arqEscreveSiglas=\jobname.losg
}
\AtEndDocument{%
  \immediate\closeout\arqEscreveSiglas
}

\newcommand{\sigla}[2]{%
  \immediate\write\arqEscreveSiglas{\unexpanded{#1 #2}}%
  #2 (#1)%
}

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

\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}

Since you want to open the write stream after you've read the former file, it's easier to open it when you're done with the reading.

share|improve this answer
add comment

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.