我希望能够即时将 Markdown 斜体和粗体转换为 latex 版本(即,提供一个文本字符串返回一个文本字符串)。我以为很简单。错误的! (它仍然可能是)。请参阅我在底部尝试的阈值业务和错误。
我有什么(注意在 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
从报告中访问它
编辑 我试过 Per 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
EDIT2 我试过 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
最佳答案
Windows 上缺少管道使这变得棘手,但您可以使用 input
来提供 stdin
来绕过它:
> x = system("pandoc -t latex", intern=TRUE, input="\\*note: I *like* chocolate **milk** too ***much***!")
> x
[1] "*note: I \\emph{like} chocolate \\textbf{milk} too \\textbf{\\emph{much}}!"
关于windows - 将 Markdown 斜体和粗体转换为 latex ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15539121/