1

我在 rmarkdown 文件中使用 Hmisc。当我创建一个表时,这就是我所做的

---
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.default(cstats, title= title....
\begin{table}...
.
.
.
\end{tabular}

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

4

1 回答 1

3

看起来这是硬编码的latex.defaultcat("%", deparse(sys.call()), "%\n", file = file, append = file != "", sep = "")在正文中,没有条件围绕它)。

我认为您最好的猜测是capture.output-dcat输出并自己删除评论。

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

capture.output捕获所有latex(...) cats 的内容,删除[-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='')
于 2015-07-16T00:41:46.880 回答