2

I'm gratuitously cross posting this from the R Studio community page as this is a bit esoteric.

Is there a way to ask Knitr to render my equations from R Markdown into images and then stick the resulting images into my final document? The use case I have in mind is overcoming some of the shortcomings of MSFT Equation editor when knitting to Word/PowerPoint. If the equation was simply an image, then I could have LaTeX quality equations in my MSFT docs, which would be fabulous!

The closest thing I have found is using latex2exp and putting in an R Code chunk that produces a figure which is actually a rendered LaTeX formula. I kinda like this sort of hack, but latex2exp has some limitations.

JD Long
  • 59,675
  • 58
  • 202
  • 294
  • Possible alternative strategy: create an image an insert it in your Word/Powerpoint document with the [`officer`-package](https://davidgohel.github.io/officer/). – Jaap Sep 07 '18 at 11:59
  • I can't figure out how to script the creation of the equation image, however. Inserting images into MSFT docs is easy, that's handled by R Markdown just fine. No need for `officer` to do that bit. – JD Long Sep 07 '18 at 12:05
  • 1
    Could you include some example rmd-code with an equation that needs to be converted to an image? – Jaap Sep 07 '18 at 12:08
  • @jaap sure: $x$ ... I'd like to make that an image. – JD Long Sep 07 '18 at 12:09
  • 1
    Render to html first using the webtex option and then docx? – James Sep 07 '18 at 13:29
  • 1
    The webtex option is a great find... I was not familiar with that. I wish I could use that switch directly with word output! – JD Long Sep 07 '18 at 16:47

1 Answers1

4

As mentioned in the comments, webtex is an easy solution. Pandoc's --webtex switch has no effect when targeting docx. However, a Lua filter can be used to the same effect.

local mediabag = require 'pandoc.mediabag'
local utils = require 'pandoc.utils'

local function url_encode(str)
  local encode_char = function(c)
    return ("%%%02X"):format(string.byte(c))
  end
  return str
    :gsub("\n", "\r\n")
    :gsub("([^%w%-%_%.%~])", encode_char)
end

local function webtex_url(formula)
  return 'https://latex.codecogs.com/png.latex?' .. url_encode(formula)
end

function Math(el)
  local filename = utils.sha1(el.text) .. '.png'
  local mime, contents = mediabag.fetch(webtex_url(el.text), '.')
  mediabag.insert(filename, mt, contents)
  local img = pandoc.Image({}, filename)
  return el.mathtype == 'DisplayMath'
    and {pandoc.LineBreak(), img, pandoc.LineBreak()}
    or img
end

Save this to a file and pass the file to pandoc via the --lua-filter option. It will convert all equations into png images via webtex.

tarleb
  • 19,863
  • 4
  • 51
  • 80
  • I'd love to see that added to pandoc! – JD Long Sep 08 '18 at 14:21
  • @tarleb where are the png files saved (if at all)? – Farid Cheraghi Mar 28 '21 at 13:23
  • @FaridCheragi those are kept in memory, but you can save them by passing `--extract-media` to pandoc. – tarleb Mar 29 '21 at 06:12
  • @tarleb you have misspelled my name so I was not notified of your comment. It works now. Thank you. – Farid Cheraghi Mar 30 '21 at 11:26
  • @FaridCheraghi sorry about that, I was on mobile when I wrote that. Glad to hear that it works. – tarleb Mar 30 '21 at 13:27
  • @tarleb , no problem. Out of curiosity, is it possible to generate these equation images with my local Latex binaries instead of `--webtex` switch, which is a bit slow? I am looking at [this Q/A](https://tex.stackexchange.com/questions/34054/tex-to-image-over-command-line/34058#34058) but would rather to do it through Pandoc. – Farid Cheraghi Mar 30 '21 at 13:33
  • 1
    Found [this](https://hackage.haskell.org/package/latex-formulae-pandoc) – Farid Cheraghi Apr 14 '21 at 20:34