Converting markdowns in italics and bold to latex

I want to be able to convert markdown italics and in bold to latex versions "on the fly" (ie give a text string (s), return a text string (s)). I thought easy. Wrong! (What else could be). Look at the windowsill and the error that I tried below.

What I have (pay attention to the starting asterisk, which was escaped as in the markdown method):

x <- "\\*note: I *like* chocolate **milk** too ***much***!" 

What I would like:

 "*note: I \\emph{like} chocolate \\textbf{milk} too \\textbf{\\emph{much}}!" 

I am not attached to regex, but would prefer a basic solution (although not necessarily).

Stupid business:

 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' 

I have this toy that Ananda Mahto helped me make, if it would be useful. You can access it from reports via wheresPandoc <- reports:::wheresPandoc

EDIT In the comments on Bren, I tried:

 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 In the comments on Dason, I tried:

 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 
+4
source share
2 answers

Noting that I am working on windows and ?system

This means that redirection, pipes, internal DOS commands, ... cannot be used

and note from ?system2

Note

system2 is a more portable and flexible interface than the system introduced in R 2.12.0. It allows you to redirect output without having to invoke a shell on Windows, a portable way to set environment variables to execute a command, and finer control over the redirection of stdout and stderr. Conversely, the system (and shell on Windows) allows you to call arbitrary command lines. Using system2

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

The lack of pipes on Windows made this harder, but you can get around it using input to provide 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}}!" 
+4
source

All Articles