Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

r - Handling Latex backslashes in xtable

I have a table that includes the following column:

 mytable <- data.frame(beta_0 = c(1,2,3)

What I want to do is output a table with a column header in latex markup, e.g. $eta_0$

However, I can not seem to figure out how to output the "$eta_0$" using print.xtable:

colnames(mytable) <- "$eta_0$"
library(xtable)
print(xtable(mytable), include.rownames = F)

returns a column header of

eta\_0$

instead of

$eta_0$

I presume that the answer is the "sanitize.colnames.function" argument to print.xtable, but it is not obvious to me how to use this, and ?print.xtable provides no examples.

Specifically, I would like to output a latex table like:

egin{table}[ht]
 egin{center}
  egin{tabular}{r}
    hline
    $eta_0$ \ 
    hline
    1.00 \ 
    2.00 \ 
    3.00 \ 
    hline
  end{tabular}
 end{center}
end{table}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two issues here; first, you need a double backslash as otherwise it treats it as a control sequence. Second, by default, xtable sanitizes text so that it won't break LaTeX. Use one of the sanitize. parameters to control this; to do no sanitizing, pass it the identity function.

colnames(mytable) <- "$\beta_0$"
print(xtable(mytable), include.rownames = F, sanitize.colnames.function = identity)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...