Reputation: 329
I'm trying to use R
to create the content of a tex file. The content contains many accented letters and I not able to correctly write them to a tex file.
Here is a short minimal example of what I would like to perform:
I have a file texinput.tex
, which already exists and is encoded as UTF8 without BOM. When I manually write é
in Notepad++ and save this file, it compiles correctly in LaTex
and the output is as expected.
Then I tried to do this in R
:
str.to.write <- "é"
cat(str.to.write, file = "tex_list.tex", append=TRUE)
As a result, the encoded character xe9
appears in the tex file. LaTex
throws this error when trying to compile:
! File ended while scanning use of \UTFviii@three@octets.<inserted text>\par \include{texinput}
I then tried all of the following things before the cat command:
Encoding(str.to.write) <- "latin1"
-> same output error as above
str.to.write <- enc2utf8(str.to.write)
-> same output and error as above
Encoding(str.to.write) <- "UTF-8"
-> this appears in the tex file: \xe9
. LaTex
throws this error: ! Undefined control sequence. \xe
Encoding(str.to.write) <- "bytes"
-> this appears in the tex file: \\xe9
. LaTex
compiles without error and the output is xe9
I know that I could replace é
by \'{e}
, but I would like to have an automatic method, because the real content is very long and contains words from 3 different Latin languages, so it has lots of different accented characters.
However, I would also be happy about a function to automatically sanitize the R output to be used with Latex. I tried using xtable
and sanitize.text.function
, but it appears that it doesn't accept character vectors as input.
Upvotes: 1
Views: 786
Reputation: 143
Use TIPA for processing International Phonetic Alphabet (IPA) symbols in Latex. It has become standard in the linguistics field.
Upvotes: 0
Reputation: 329
After quite a bit of searching and trial-and-error, I found something that worked for me:
# create output function
writeTex <- function(x) {write.table(x, "tex_list.tex",
append = TRUE, row.names = FALSE,
col.names = FALSE, quote = FALSE,
fileEncoding = "UTF-8")}
writeTex("é")
Output is as expected (é
), and it compiles perfectly well in LaTex.
Upvotes: 1