1#' Convert to an RTF document 2#' 3#' Format for converting from R Markdown to an RTF document. 4#' 5#' @inheritParams pdf_document 6#' @inheritParams html_document 7#' @inheritParams word_document 8#' 9#' @return R Markdown output format to pass to \code{\link{render}} 10#' 11#' @details 12#' 13#' See the \href{http://rmarkdown.rstudio.com/rtf_document_format.html}{online 14#' documentation} for additional details on using the \code{rtf_document} format. 15#' 16#' R Markdown documents can have optional metadata that is used to generate a 17#' document header that includes the title, author, and date. For more details 18#' see the documentation on R Markdown \link[=rmd_metadata]{metadata}. 19#' 20#' R Markdown documents also support citations. You can find more information on 21#' the markdown syntax for citations in the 22#' \href{http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html}{Bibliographies 23#' and Citations} article in the online documentation. 24#' 25#' @examples 26#' \dontrun{ 27#' 28#' library(rmarkdown) 29#' 30#' # simple invocation 31#' render("input.Rmd", rtf_document()) 32#' 33#' # specify table of contents option 34#' render("input.Rmd", rtf_document(toc = TRUE)) 35#' } 36#' 37#' @export 38rtf_document <- function(toc = FALSE, 39 toc_depth = 3, 40 fig_width = 5, 41 fig_height = 4, 42 keep_md = FALSE, 43 md_extensions = NULL, 44 pandoc_args = NULL) { 45 46 # knitr options and hooks 47 knitr <- knitr_options( 48 opts_chunk = list(dev = 'png', 49 dpi = 96, 50 fig.width = fig_width, 51 fig.height = fig_height) 52 ) 53 54 # build pandoc args 55 args <- c("--standalone") 56 57 # table of contents 58 args <- c(args, pandoc_toc_args(toc, toc_depth)) 59 60 # pandoc args 61 args <- c(args, pandoc_args) 62 63 preserved_chunks <- character() 64 65 check_knitr_version <- function() { 66 if (packageVersion('knitr') < '1.15') { 67 warning('You need to install a newer version of the knitr package') 68 FALSE 69 } else TRUE 70 } 71 72 pre_processor <- function(metadata, input_file, runtime, knit_meta, 73 files_dir, output_dir) { 74 if (!check_knitr_version()) return() 75 preserved_chunks <<- extract_preserve_chunks(input_file, knitr::extract_raw_output) 76 NULL 77 } 78 79 post_processor <- function(metadata, input_file, output_file, clean, verbose) { 80 if (!check_knitr_version()) return(output_file) 81 output_str <- readLines(output_file, encoding = 'UTF-8') 82 output_res <- knitr::restore_raw_output(output_str, preserved_chunks) 83 if (!identical(output_str, output_res)) 84 writeLines(enc2utf8(output_res), output_file, useBytes = TRUE) 85 output_file 86 } 87 88 # return output format 89 output_format( 90 knitr = knitr, 91 pandoc = pandoc_options(to = "rtf", 92 from = from_rmarkdown(extensions = md_extensions), 93 args = args), 94 keep_md = keep_md, 95 pre_processor = pre_processor, 96 post_processor = post_processor 97 ) 98} 99