在Sweave文档中插入手动创建的Markdown表

编程入门 行业动态 更新时间:2024-10-14 06:20:24
本文介绍了在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的内容打印到文档后,myfile和texfile被删除.

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表

本文发布于:2023-11-28 13:35:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642620.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   Sweave   Markdown

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!