9

I have a tex file with:

\usepackage[utf8]{inputenc}

The file calls a macro that saves the content between and \begin{something} and \end{something} to a file. But the encoding of the file is not utf8. Is there a way to set encoding of the output file (I can rewrite the macro if necessary).

The macro is:

\def\@something[#1]{%
    \gdef\@somethinginclude{#1}
    \immediate\openout\@out=\@outname.tmp
    \newlinechar='15
    \begingroup \catcode`\^^M=12 %
    \let\do\@makeother\dospecials\obeyspaces%
    \@xsomething
}

\def\endsomething{%
    \endgroup
    \immediate\closeout\@out
}

Edit

I have simplified the macro:

\RequirePackage{fancyvrb}

\newenvironment{something}
{
    \VerbatimOut{\@outname.tmp}}
{
    \endVerbatimOut
}

This makes the code much easier, but the encoding is still not right. It seems that the internal representation is used.

  • tex.stackexchange.com/q/58178/3734 – F'x Nov 16 '13 at 16:44
  • Is there something like \outputencoding{latin9 or whatever} from outputenc package while writing to external files ? – texenthusiast Nov 16 '13 at 16:47
  • How are \something and \@xsomething defined? – egreg Nov 16 '13 at 17:13
  • all standard latex file writing should write just using LICR ie ascii TeX notation so be compatible with utf-8, – David Carlisle Nov 16 '13 at 17:44
9

With \usepackage[utf8]{inputenc} characters with the high bit set are made active and verbatim environments don't change this. You can do it yourself:

I called this file hernan.tex, so the output file will be hernan.out:

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

\usepackage{fancyvrb}

\newenvironment{writefile}[1]
 {\deactivateeightbit\VerbatimOut{#1.out}}
 {\endVerbatimOut}
\makeatletter
\newcommand{\deactivateeightbit}{%
  \count@=127
  \loop
    \catcode\count@=12
    \ifnum\count@<255
    \advance\count@\@ne
  \repeat
}
\makeatother

\begin{document}
Questo è il mio testo.

\begin{writefile}{\jobname}
àèì
\end{writefile}

\end{document}

The contents of hernan.out is

àèì

If I look at it with LESSCHARSET=ascii less hernan.out I get this

<C3><A0><C3><A8><C3><AC>

which shows correct UTF-8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.