Handling Latex backslashes in xtable

我的未来我决定 提交于 2019-11-27 14:26:01

问题


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. $\beta_0$

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

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

returns a column header of

\eta\_0\$

instead of

$\beta_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:

\begin{table}[ht]
 \begin{center}
  \begin{tabular}{r}
    \hline
    $\beta_0$ \\ 
    \hline
    1.00 \\ 
    2.00 \\ 
    3.00 \\ 
    \hline
  \end{tabular}
 \end{center}
\end{table}

回答1:


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)


来源:https://stackoverflow.com/questions/8732712/handling-latex-backslashes-in-xtable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!