【问题标题】:Convert markdown italics and boldface to latex将降价斜体和粗体转换为乳胶
【发布时间】:2013-03-21 03:47:46
【问题描述】:

我希望能够即时将 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 &lt;- reports:::wheresPandoc从报告中访问它

编辑我试过的每个 Ben 的 cmets:

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 的 cmets:

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

【问题讨论】:

  • 不知道上下文,但是:使用 pandoc 将 Markdown 渲染为 LaTeX 的任何机会? ` echo '*注意:我也喜欢巧克力牛奶 很多!' | pandoc -t 乳胶`
  • 可能我想过这个(可能使用pander),但在这里或那里转换字符串似乎有点过头了(就像蚊子的火箭筒)。这可能是要走的路。另外,它需要一个外部文件并读回(我认为但可能是错误的)。
  • @TylerRinker 你不需要写入外部文件——你只需要正确使用命令行。
  • out &lt;- paste("echo", shQuote(x), "| pandoc -t latex"); ans &lt;- system(out, intern = T)
  • pander 有一个函数Pandoc.convert——我一辈子都不知道如何设置它不返回完整的乳胶文档。

标签: windows r latex markdown


【解决方案1】:

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}}!"

【讨论】:

  • 效果很好,但我更喜欢system2。感谢您将答案向前推进。
【解决方案2】:

注意我正在使用 Windows 和 ?system

这意味着重定向、管道、DOS内部命令……不能使用

还有来自?system2的备注

注意

system2 是一个比 system 更便携、更灵活的界面, 在 R 2.12.0 中引入。它允许重定向输出而不 需要在 Windows 上调用 shell,这是一种可移植的设置方式 用于执行命令的环境变量,以及更精细的控制 通过 stdout 和 stderr 的重定向。相反,系统(和 Windows 上的 shell)允许调用任意命令行。 使用system2

system2('pandoc', '-t latex', input = '**em**', stdout = TRUE)

【讨论】:

  • 非常感谢。像魅力一样工作。
  • 我在报告包中包含了这方面的工作,并希望将作者身份归属于您。我将使用 stackoverflow.com 的 mnel,除非您给我发电子邮件并告诉我要使用的正确名称。
  • 对不起,我以为你会去找报告包;不想看台:) tyler.rinker@gmail.com
  • @TylerRinker -- 我的意思是在你的收件箱里 -- 我可以找到你的电子邮件 :)
  • 哦 :) Gottcha(在此处插入愚蠢的评论)。
猜你喜欢
  • 2016-12-31
  • 2019-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 2011-07-18
相关资源
最近更新 更多