From 8a4d8c0464b75f683698d3c44b57548692464951 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Fri, 29 Apr 2016 17:58:29 -0500 Subject: [PATCH] protect the math environments for epub as suggested in https://github.com/jgm/pandoc/issues/2758 (not an optimal solution, and hoping Pandoc can address the real issue) --- R/ebook.R | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/R/ebook.R b/R/ebook.R index 5b20796b3..f1636cb01 100644 --- a/R/ebook.R +++ b/R/ebook.R @@ -60,6 +60,7 @@ epub_book = function( ) content = restore_part_epub(content) content = restore_appendix_epub(content) + content = protect_math_env(content) writeUTF8(content, input_file) NULL }, @@ -120,6 +121,23 @@ restore_appendix_epub = function(x) { x } +# wrap math environments in $$, otherwise they are discarded by Pandoc +# https://github.com/jgm/pandoc/issues/2758 +protect_math_env = function(x) { + env = 'equation' # may add more LaTeX environments later + s1 = sprintf('\\begin{%s}', env) + s2 = sprintf('\\end{%s}', env) + for (s in s1) { + i = x == s + x[i] = paste0('$$', x[i]) + } + for (s in s2) { + i = x == s + x[i] = paste0(x[i], '$$') + } + x +} + # manually base64 encode images in css: https://github.com/jgm/pandoc/issues/2733 epub_css = function(files, output = tempfile('epub', fileext = '.css')) { css = unlist(lapply(files, function(css) {