Last active
May 4, 2019 23:30
-
-
Save baptiste/c49bb5de5a1a1b19d2ccb8b04f6a4d7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rmarkdown) | |
pre_knit <- function(input, ...){ | |
rd <- readLines(input) | |
id <- grep('◊', rd) | |
rd[id] <- stringr::str_replace_all(rd[id], | |
"`◊([:alnum:]+)\\((.*?)\\)`", | |
replacement = '`r \\1("\\2")`') | |
file.copy(input, paste0(input, 'original.Rmd')) | |
writeLines(text = rd, con = input) | |
} | |
custom_format <- rmarkdown::output_format(knitr = rmarkdown::knitr_options(opts_chunk = list(dev = 'png')), | |
pandoc = rmarkdown::pandoc_options(to = "markdown"), | |
pre_knit = pre_knit) | |
pdf_format <- rmarkdown::output_format(knitr = rmarkdown::knitr_options(opts_chunk = list(dev = 'png')), | |
pandoc = rmarkdown::pandoc_options(to = "latex")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
knit: (function(inputFile, encoding) {source('_pre_knit.R'); render(inputFile, output_format = custom_format, run_pandoc = FALSE); original = paste0(inputFile,"_tmp.Rmd"); rmarkdown::render(inputFile, encoding = encoding); file.copy(original, inputFile); unlink(original)}) | |
output: | |
pdf_document | |
--- | |
```{r setup, include=FALSE} | |
knitr::opts_chunk$set(echo = FALSE) | |
enable_caps <- FALSE | |
emph = function(x){ | |
if(knitr::is_latex_output()) { | |
if(enable_caps) sprintf("\\textsc{%s}", x) else sprintf("\\textbf{%s}", x) | |
} else x | |
} | |
``` | |
## Intro | |
This is an R Markdown document `◊emph(with custom formatting)`. | |
```{r cars} | |
summary(cars) | |
``` | |
## Wait a second | |
`r enable_caps = TRUE` | |
In this essay, `◊emph(I will)` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment