我想要能够即时地将Markdown的斜体和粗体转换为LaTeX版本(即输入文本字符串,返回文本字符串)。我以为很容易。错了!(也许还是很容易的)。请参见下面我尝试的愚蠢业务和错误。
我有什么(注意起始星号已被转义为Markdown):
我想要的是:
我不依赖于正则表达式,但更喜欢基础解决方案(虽然不是必需的)。
愚蠢的事情:
我有一件玩具this toy,是Ananda Mahto帮我制作的,如果有帮助的话。你可以通过报告访问它,方法是:
编辑2 根据Dason的评论,我试过了:
我有什么(注意起始星号已被转义为Markdown):
x <- "\\*note: I *like* chocolate **milk** too ***much***!"
我想要的是:
"*note: I \\emph{like} chocolate \\textbf{milk} too \\textbf{\\emph{much}}!"
我不依赖于正则表达式,但更喜欢基础解决方案(虽然不是必需的)。
愚蠢的事情:
helper <- function(ins, outs, x) {
gsub(paste0(ins[1], ".+?", ins[2]), paste0(outs[1], ".+?", outs[2]), x)
}
helper(rep("***", 2), c("\\textbf{\\emph{", "}}"), x)
Error in gsub(paste0(ins[1], ".+?", ins[2]), paste0(outs[1], ".+?", outs[2]), :
invalid regular expression '***.+?***', reason 'Invalid use of repetition operators'
我有一件玩具this toy,是Ananda Mahto帮我制作的,如果有帮助的话。你可以通过报告访问它,方法是:
wheresPandoc <- reports:::wheresPandoc
编辑 根据Ben的意见,我尝试了:
action <- paste0(" echo ", x, " | ", wheresPandoc(), " -t latex ")
system(action)
*note: I *like* chocolate **milk** too ***much***! | C:\PROGRA~2\Pandoc\bin\pandoc.exe -t latex
编辑2 根据Dason的评论,我试过了:
out <- paste("echo", shQuote(x), "|", wheresPandoc(), " -t latex"); system(out)
system(out, intern = T)
> system(out, intern = T)
\*note: I *like* chocolate **milk** too ***much***! | C:\PROGRA~2\Pandoc\bin\pandoc.exe -t latex
echo '\ *note:我也***非常喜欢***巧克力**牛奶**!' | pandoc -t latex
- Ben Bolkerpander
),但似乎对于偶尔转换字符串来说有些大材小用了(就像用火箭筒打蚊子)。也许这是可行的方法。此外,它需要一个外部文件并重新读取(我认为,但可能是错误的)。 - Tyler Rinkerout <- paste("echo", shQuote(x), "| pandoc -t latex"); ans <- system(out, intern = T)
- Dasonpander
有一个名为Pandoc.convert
的函数,就是死活搞不明白如何设置它不返回完整的LaTeX文档。 - mnelout
的输出结果是符合预期的。 - Tyler Rinker