【问题标题】:Hmisc latex fuction need to remove the first lineHmisc乳胶功能需要删除第一行
【发布时间】:2015-07-16 00:20:51
【问题描述】:

我在 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 输出的第一行如下所示

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

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

【问题讨论】:

  • 我也用stargazer 做了一个测试,也有一行代码用%。非常奇怪的是 % 是对 LaTeX 的注释,因此对于 asis 块选项,它应该被忽略。也许这是一个错误。你在 Rstudio 中使用 knitr 吗?
  • 反正有herextable的解决方案。

标签: r latex hmisc


【解决方案1】:

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

我认为你最好的猜测是capture.outputcat-d 输出并自己删除评论。

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='')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    相关资源
    最近更新 更多