在xtable中处理Latex反斜杠 [英] Handling Latex backslashes in xtable

查看:307
本文介绍了在xtable中处理Latex反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下列的表:

I have a table that includes the following column:

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

我想做的是在乳胶标记中输出带有列标题的表,例如$\beta_0$

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

但是,我似乎无法弄清楚如何使用print.xtable输出"$ \ 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)

返回

\eta\_0\$

代替

$\beta_0$

我认为答案是print.xtable的"sanitize.colnames.function"参数,但是对我来说如何使用它并不明显,并且?print.xtable没有提供示例.

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}

推荐答案

这里有两个问题;首先,您需要一个双反斜杠,否则会将其视为控制序列.其次,默认情况下,xtable清除文本,以免破坏LaTeX.使用sanitize.参数之一来控制它.不进行消毒,将身份功能传递给它.

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)

这篇关于在xtable中处理Latex反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆