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'm currently writing a .tex report with my wife. She's on a Windows machine and I'm running Linux over here; we've decided to encode our report in ISO-8859-1 (i.e., we use \usepackage[latin1]{inputenc} on the preamble).

We're also generating lots of tables in R using xtable() and some other customized functions to output LaTeX. Whenever I sink an R output, I use this command: sink(file("filename.tex", encoding = "ISO-8856-9")). There is, however, this one long table that contains lots of special characters. Whenever I try to sink this table as ISO I get this message:

Warning message:
invalid char string in output conversion  

Even though it's just a warning, it prevents our LaTeX compiler from being able to correctly parse the command \input{filename.tex}.

The workaround we have for this is using encoding = "UTF-8" in R, then opening the file on a text editor and saving it with ISO encoding. Then LaTeX compiles the file correctly with apparently all special characters.

Is there a command by which I can tell LaTeX that a file I'm inputting has a different encoding from the main LaTeX file? In my mind, it would look something like this \input[utf8]{filename.tex} (but unfortunately \input or it's fellow \include don't take options).

share|improve this question

migrated from stackoverflow.com Feb 22 '12 at 20:23

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers 2

up vote 13 down vote accepted

Are you certain that the encoding should be ISO-8856-9? I've never heard of that one. Maybe you mean ISO-8859-6 (Arabic) or ISO-8859-9 (Turkish)?

Let's assume that you meant ISO-8859-9. Then you just write

\usepackage[latin1]{inputenc}

in the beginning of your code,

\inputencoding{latin5}

just before you include the file and

\inputencoding{latin1}

to restore the original encoding.

EDIT:

UTF-8 might cause some problems. I'd solve it by stating that the whole document should be in UTF-8 and then switching back. Here's an example:

The following file is "main-file.tex", and should be saved with Latin 1.

\documentclass{article}
\usepackage[utf8]{inputenc}
\inputencoding{latin1}
\usepackage[russian,brazil]{babel}
\usepackage[T1]{fontenc}
\begin{document}
\section{First section}
Nunca vi socó coçar sem bico, nunca vi sem bico socó coçar. Um socó, dois socós, três socós, quatro socós, quantos socós pra um só socó coçar.

\inputencoding{utf8}
\selectlanguage{russian}
\input{external-file}
\inputencoding{latin1}
\selectlanguage{brazil}

\section{Third section}
A sábia não sabia que o sábio sabia que o sabiá sabia que o sábio não sabia que o sabiá não sabia que a sábia não sabia que o sabiá sabia assobiar.
\end{document}

Also create the file "external-file.tex", and save it with UTF-8:

\section{Second section}
Карл у Клары украл кораллы, а Клара у Карла украла кларнет.

This works for me without any warnings at all. Let me know the specific warnings and errors if you still can't get it to work.

share|improve this answer
    
Thanks for the help, but that doesn't work (we're writing the document in Brazilian Portuguese, BTW, so we're using \usepackage[brazil]{babel} and \usepackage[T1]{fontenc} in the preamble. I've also tried ambracing my \input{} statement between \inputencoding{utf8} and \inputencoding{latin1}, but it didn't work. –  wleoncio Feb 21 '12 at 21:54
    
@wleoncio It strange that it doesn't work, because it's working for me when I'm attempting a configuration that I believe is similar to yours. Could you try the example above and let me know if it works? Thanks. –  aliquis Feb 22 '12 at 4:23
    
That did work, no warnings or errors whatsoever. Thanks for the help! –  Waldir Leoncio Feb 24 '12 at 13:26
add comment

If the only problem is in fact the warning message produced by the call to sink(file()), you could use suppressWarnings() to tell R that you don't want to hear any grumbling from that particular bit of code.

I can't exactly recreate your problem without the files you're working on, so here's an example that uses another warning-producing call. Here's how you could suppress the warning produced by an attempt to convert the character string "A" to a numeric vector:

sink(file("otherEgFile.txt", encoding = "ISO-8856-9"))    
suppressWarnings(as.numeric("A"))
sink()
share|improve this answer
    
Unfortunately, it doesn't. Maybe I'm doing this wrong, but when I use supressWarnings(), what it does is echo what should have been sunk. –  wleoncio Feb 21 '12 at 21:03
    
@wleoncio Just to clarify, did you wrap the call that actually produces the warnings with suppressWarnings()? I realize I gave you the wrong advice, since the call to sink() is not where you are getting your warning. I'll edit my answer above accordingly. –  Josh O'Brien Feb 21 '12 at 21:11
    
Thanks, Josh, your tip helped me solve the problem. What suppressing the warnings for the table inside the sink command actually did was generate a .tex file with an incomplete table. This table was interrupted just before the mischievous character. Since it only happens twice in the table, I can replace it inside the R script, before creating the table. BTW, here's the prick: ’ –  wleoncio Feb 21 '12 at 22:10
    
@wleoncio -- Superb. I'm glad to hear that helped you isolate the problem. Cheers. –  Josh O'Brien Feb 21 '12 at 22:21
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.