hmisc latex function需要去掉第一行

Hmisc latex fuction need to remove the first line

我在 rmarkdown 文件中使用 Hmisc。当我创建 table 这就是我所做的

---
output: pdf_document
---

```{r Arrests Stats, results ='asis', message = FALSE, warning = FALSE, echo = FALSE}

# render the table

options(digits=1)
library(Hmisc)
latex(head(mtcars), file="")

```

latex 输出的第一行如下所示

%latex.default(cstats, title= title....
\begin{table}...
.
.
.
\end{tabular}

注意“%”,我需要弄清楚如何删除第一行,因为它在编织时显示在 PDF 文档上

看起来它被硬编码到 latex.default 中(cat("%", deparse(sys.call()), "%\n", file = file, append = file != "", sep = "") 在正文中,周围没有条件)。

我认为你最好的猜测是 capture.output cat-d 输出并自己删除注释。

cat(capture.output(latex(head(mtcars), file=''))[-1], sep='\n')

capture.output 捕获 latex(...) cat 的所有内容,[-1] 删除第一行(即 '%latex.default') ,cat 打印出所有其他内容,并带有换行符。

您可以定义自己的 mylatex 来执行此操作,并且更聪明一点(例如,不是盲目地剥离输出的第一行,您只能剥离以 '%' 开头的行).

mylatex <- function (...) {
    o <- capture.output(latex(...))
    # this will strip /all/ line-only comments; or if you're only
    #  interested in stripping the first such comment you could
    #  adjust accordingly
    o <- grep('^%', o, inv=T, value=T)
    cat(o, sep='\n')
}
mylatex(head(mtcars), file='')