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 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.

share|improve this question

migrated from stackoverflow.com Apr 23 '13 at 17:44

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

1  
Worth a read eelvex.net/latex/greek-in-latex –  mnel Apr 23 '13 at 3:21

1 Answer 1

up vote 8 down vote accepted

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
share|improve this answer
    
Son of a pup beat me by 23 seconds +1 –  Tyler Rinker Apr 23 '13 at 3:20

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.