0

How does one save a table with Greek letters in R, so that LaTeX recognizes and outputs the Greek letters when the table is called in LaTeX ?

I am using R to output a table of results, save the results table as a *.tex file using R's print() command, and I input this table of results in my longer *.tex report.

As you can see below in the below R code, I want to include Greek letters as row labels, but I cannot figure it out.

R code is:

table <- rbind(1:5, 6:10, 11:15, 16:20)

rownames(table) <- c("hat(beta)",
                     "hat(tau)",
                     "hat(sigma)_C",
                     "gamma")

colnames(table) <- c("Var1",
                     "Var2",
                     "Var3",
                     "Var4",
                     "Var5")

print(xtable(table), only.contents=TRUE, include.rownames=T, 
      include.colnames=T, floating=F,
      file = 'table.tex')

Tex code:

  \begin{tabular}{  l cc cc c   }
          \hline
     \input{table.tex} 
          \hline
        \end{tabular} 
  • My first impression is that this is not a latex problem per se, unless there are specific tools for using R along with latex. It seems that all you need is a script that prepends a backlash to all the words that correspond to greek letters (alpha,beta,gamma, ..) Are you using linux by any chance? – geo909 Mar 14 '17 at 18:40
  • @geo909 I'm using mac osx, not linux. I considered posting this at stackoverflow, but I opted for tex.stack since this is ultimately about using tex, although there are probably work-arounds in both R and Tex. I'll try that prepend idea now – Dr. Beeblebrox Mar 14 '17 at 18:44
  • @geo909 I found the answer on stackoverflow and posted an answer below – Dr. Beeblebrox Mar 14 '17 at 18:53
3

I solved this on the R end rather than the tex end. But for future reference the solution is below.

I found the necessary help here: https://stackoverflow.com/questions/8732712/handling-latex-backslashes-in-xtable

The necessary steps in R are using double backslashes and sanitize.colnames.function = identity or sanitize.rownames.function = identity.

colnames(table) <- "$\\beta_0$"
print(xtable(table), sanitize.colnames.function = identity)

The corrected code from my example is below:

table <- rbind(1:5, 6:10, 11:15, 16:20)

rownames(table) <- c("$\\hat{\\beta}$",
                     "$\\hat{\\tau}$",
                     "$\\hat{\\sigma}_C$",
                     "$\\gamma$")

colnames(table) <- c("Var1",
                     "Var2",
                     "Var3",
                     "Var4",
                     "Var5")

print(xtable(table), only.contents=TRUE, include.rownames=T, 
      include.colnames=T, floating=F, sanitize.rownames.function = identity
      file = 'table.tex')

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.