在 Sweave 文档中插入手动创建的降价表 [英] Insert manually created markdown table in Sweave document

查看:13
本文介绍了在 Sweave 文档中插入手动创建的降价表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Markdown 中有一堆相当大的表格,是我手动创建的.我在 Rmd 文档中使用它们.由于我需要对 LaTeX 进行更多控制,因此我使用的是 Rnw 文档.如何将我的降价表放入 Sweave 文件中?

I have a bunch of quite big tables in markdown that I created manually. I was using them in an Rmd document. Since I need more control with LaTeX and all, I am using a Rnw document. How can I put my markdown table in the Sweave file?

下面是一个最小的例子(不工作):

Below a minimal example (not working):

documentclass{article}

egin{document}
SweaveOpts{concordance=TRUE}

% my markdown table

col1 | col2 | col3
------|:---:|:---:
row1 | cell1 | cell2 
row2 | cell3 | cell4 
row3 | cell5 | cell6 


end{document}

我尝试转换文档中的表格,只是将表格粘贴到 Sweave 文档中的 markdown 中,然后在 LaTeX 中呈现.我的尝试产生错误,但我更接近:

I've tried to convert the table inside the document, just to paste the table in markdown in the Sweave document, and get it rendered in LaTeX. My try yields errors, but I am closer:

documentclass{article}

egin{document}
SweaveOpts{concordance=TRUE}

<<texifytable, echo=FALSE, results=tex>>=
mytab = sprintf("col1 | col2 | col3
------|:---:|:---:
row1 | cell1 | cell2 
row2 | cell3 | cell4 
row3 | cell5 | cell6")
system2("pandoc", args = c("-f markdown","-t latex"),
        stdout = TRUE, input = mytab)
@

end{document}

推荐答案

可行:

documentclass{article}
usepackage{longtable}
usepackage{booktabs}
egin{document}

<<echo = FALSE, results = "asis", message = FALSE>>=
library(knitr)

markdown2tex <- function(markdownstring) {
  writeLines(text = markdownstring,
             con = myfile <- tempfile())
  texfile <- pandoc(input = myfile, format = "latex", ext = "tex")
  cat(readLines(texfile), sep = "
")
  unlink(c(myfile, texfile))
}

markdowntable <- "
col1 | col2 | col3
-----|:----:|:----:
row1 | cell1 | cell2
row2 | cell3 | cell4
row3 | cell5 | cell6
"

markdown2tex(markdowntable)
@
end{document}

我将代码包装在一个小的辅助函数 markdown2tex 中.这使得代码在与多个降价表一起使用时非常精简.

I wrapped the code in a small helper function markdown2tex. This makes the code quite slim when using it with several markdown tables.

这个想法是简单地复制文档中的markdown表并将其作为字符串分配给一个对象(这里:markdowntable).将 markdowntable 传递给 markdown2tex 会将等效的 LaTeX 表包含到文档中.不要忘记使用块选项 results = "asis"message = FALSE(后者是为了抑制来自 pandoc 的消息).

The idea is to simply copy the markdown table in the document and assign it as character string to an object (here: markdowntable). Passing markdowntable to markdown2tex includes the equivalent LaTeX table into the document. Don't forget to use the chunk options results = "asis" and message = FALSE (the latter in order to suppress messages from pandoc).

markdown2tex 中的主力是 knitr::pandoc.format = "latex", ext = "tex" 它将 input 转换为 TEX 片段并返回 TEX 文件的路径(texfile).由于 pandoc 需要一个文件名作为输入,因此将降价字符串写入临时文件 myfile.将texfile的内容打印到文档后,myfiletexfile被删除.

The workhorse in markdown2tex is knitr::pandoc. With format = "latex", ext = "tex" it converts the input to a TEX fragment and returns the path to the TEX file (texfile). As pandoc needs a filename as input, the markdown string is written to a temporary file myfile. After printing the contents of texfile to the document, myfile and texfile are deleted.

当然,如果markdown表已经保存在文件中,这些步骤可以简化.但就个人而言,我喜欢在 RNW 文件中 in 使用 markdown 字符串的想法.这样,它可以很容易地编辑,内容清晰并且支持再现性.

Of course, if the markdown tables are already saved in files, these steps can be simplified. But personally, I like the idea of having a markdown string in the RNW file. That way, it can be easily edited, the content is clear and it supports reproducibility.

注意:您需要在序言中添加 usepackage{longtable}usepackage{booktabs}.pandoc 生成的 TEX 代码需要这些包.

Note: You need to add usepackage{longtable} and usepackage{booktabs} to the preamble. The TEX code generated by pandoc requires these packages.

上面的例子产生以下输出:

The example above produces the following output:

这篇关于在 Sweave 文档中插入手动创建的降价表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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