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

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

问题描述

我在Markdown中有一堆很大的表,这些表是我手动创建的.我在Rmd文档中使用了它们.由于我需要对LaTeX和所有其他控件进行更多控制,因此我在使用Rnw文档.如何将我的markdown表放在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}

\begin{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}

\begin{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}
\begin{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 = "\n")
  unlink(c(myfile, texfile))
}

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

markdown2tex(markdowntable)
@
\end{document}

我将代码包装在一个小的辅助函数markdown2tex中.当与几个markdown表一起使用时,这会使代码变得很苗条.

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.

当然,如果降价表已经保存在文件中,则可以简化这些步骤.但就我个人而言,我喜欢在RNW文件中包含减价字符串 的想法.这样,就可以轻松地对其进行编辑,使内容清晰并支持可重复性.

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文档中插入手动创建的Markdown表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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