5

I have the following Rnw document which I intend should produce a small table with "a b c" and "α β γ". At the moment, the encoding seems to be causing problems. The funny thing is that the output in the pdf is different to that in the R console. The following example doesn't throw any errors, but doesn't have the Greek letters I want:

\documentclass{article}

\begin{document}
<<makegreek>>=
greek <- c("\U03B1", "\U03B2", "\U03B3")
greek
@

<<r grecoroman>>=
gr <- data.frame(roman=letters[1:3], greek=greek)
gr
@

<<gr-table, results='asis'>>=
library(xtable)
print(xtable(gr))
@

\end{document}

All Greek to me

I also tried adding the line: \usepackage[utf8]{inputenc} to my preamble, but that simply throws an error: Package inputenc Error: Unicode char \u8:? not set up for use with LaTeX.

1
9

The following is my first thought, but I feel fairly certain that a better way exists:

\documentclass{article}

\begin{document}
<<makegreek>>=
greek <- c("$\\alpha$", "$\\beta$", "$\\gamma$")
greek
@

<<r grecoroman>>=
gr <- data.frame(roman=letters[1:3], greek=greek)
gr
@

<<gr-table, results='asis'>>=
library(xtable)
print(xtable(gr),sanitize.text.function = function(x) x)
@

\end{document}

Following @mnel's suggestion above, this also seems to work (leaving the rest of my soltuion unchanged):

\documentclass{article}
\usepackage[greek,english]{babel}

\begin{document}
<<makegreek>>=
greek <- c("\\greektext a", "\\greektext b", "\\greektext c")
greek
@

As this was migrated from StackOverflow, I thought I would explain a few things that might be confusing to people familiar with LaTeX but not with R.

The R code here is generating LaTeX and inserting it into the document. Backslashes are special characters in R, so we need to escape them (e.g. \\alpha). Next, we have to tell the xtable function to not try to process the table entries, since it will typically assume that you want it to convert certain special characters to literals (like \backslash). So I'm simply passing an identity function that does nothing in

sanitize.text.function = function(x) x
1
  • Son of a pup beat me by 23 seconds +1 Apr 23 '13 at 3:20

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.