# How to import a text file (with accent, special chars)

I got some text file (logs) that I want to import

I saw this post: How do I import a text file so that it becomes the body text of my LaTeX document? which work, but all the special chars (UTF-8) are dropped and the reserved chars (eg:\, %) are interpreted.

eg.:

ó(%n*\r\ vir\róecu rdOhO {j I iT( J t-y g

The verbatim package solution

  \usepackage{verbatim}
\verbatiminput{a.txt}


work fine with %, but not with UTF-8

any clue what can I do about it?

-

You need to use a LaTeX flavor which supports unicode. Use either lualatex or xelatex instead of (pdf)latex or try to load \usepackage[utf8]{inputenc}.

The special characters like % can be temporary disabled using \catcode\%=12 or easier \@makeother\%. All special characters can be changed at once using \dospecial if \do is changed to use \@makeother.

You need a modern font including the correct encoding so that special characters are correctly shown. Otherwise you will get a " instead of \, etc.

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

\makeatletter
\newcommand*{\inputverbatim}[1]{%
\begingroup
\let\do\@makeother
\dospecials
\obeyspaces% To get normal spaces not a symbol
\input{#1}%
\endgroup
}
\makeatother

\begin{document}

...

\inputverbatim{somefile}

...

\end{document}


This works fine for me with pdflatex and gives:

...
ó(%n*\r\ vir\róecu rdOhO {j I iT( J t-y g
...

-
the inputenc was what I needed. \usepackage{verbatim} \usepackage[T1]{fontenc} \usepackage{lmodern} \usepackage[applemac]{inputenc} \verbatiminput{a.txt}. and my file was in a mac format. not utf-8... using pdflatex. Thanks Martin ! –  Loda Dec 20 '11 at 13:59

If you happen to know which characters they are and want to be a bit more lazy, you can change their category code to normal text using \catcode.

\catcode<symbol>=11


With 11 the code for a normal symbol, mind that symbol needs to be replaced by the LaTeX format of the symbol (# would be \# or é would be \'e). Here is a list for the number codes.

You have to be careful though, changing important characters (like \) can make your whole program fail. You can revert it by typing exactly the same but with the appropriate number code found in the link.

I've only used this with hash (#), which had no problems so far.

-