Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

r - 如何使用R将字符从Markdown转换为LaTeX(How to use R to convert character from markdown to LaTeX)

I have a variable, x , which is a character formatted using markdown:

(我有一个变量x ,这是一个使用markdown格式化的字符:)

x <- "Here is some _great_ text in a **variable** and some blank spaces ____."

I want to convert it to Tex so that it looks like this

(我想将其转换为Tex,使其看起来像这样)

y <- some_library::md2tex(x)
y
[1] "Here is some extit{great} text in a extbf{variable} and some blank spaces \_\_\_\_."

Is there an R function that achieves this?

(是否有R函数可以实现这一目标?)

The backslashes themselves might need to be escaped, but you get the idea.

(反斜杠本身可能需要转义,但是您明白了。)

I'm certain this exists as it's easy to convert an from .Rmd to a .pdf, but I'd prefer not to create and write an intermediate .tex file since this will need to be repeated a lot.

(我确信这是存在的,因为很容易将.Rmd转换为.pdf,但是我不希望创建和编写中间.tex文件,因为这需要重复很多次。)

I've dug around the vignettes, documentation, and source code for both knitr and RMarkdown but I can't find what I'm looking for.

(我已经研究了knitrRMarkdown的小插曲,文档和源代码,但找不到所需的内容。)

EDIT

(编辑)

So I found knitr::pandoc which is almost there but requires input and output files.

(所以我找到了knitr::pandoc ,它几乎在那里,但是需要输入和输出文件。)

  ask by haff translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just write your string to a temp file, and convert that.

(只需将字符串写入临时文件,然后进行转换即可。)

I recommend using rmarkdown::render instead of knitr::pandoc ;

(我建议使用rmarkdown::render而不是knitr::pandoc ;)

they both call pandoc , but the former sets all the options for you:

(它们都称为pandoc ,但是前者为您设置了所有选项:)

x <- "Here is some _great_ text in a **variable** and some blank spaces ____."
infile <- tempfile(fileext=".md")
writeLines(x, infile)
outfile <- rmarkdown::render(infile, rmarkdown::latex_fragment(), quiet = TRUE)
readLines(outfile)

This produces the following output:

(这将产生以下输出:)

[1] "Here is some \emph{great} text in a \textbf{variable} and some blank"
[2] "spaces \_\_\_\_."  

For neatness, you could remove the two temp files at the end:

(为了简洁起见,您可以在最后删除两个临时文件:)

unlink(c(infile, outfile))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...