rendering equations as images for Microsoft output?

equations

#1

I am bumping into situations where the equation editor in Word does not support the equations I am creating in markdown. They render fine in MathJax or LaTeX but Word does not render them properly. Is there an option in R Markdown or knitter to create images from equation blocks and include the images instead of the equation? This seems like it would get around the Microsoft Equation Editor constraint and allow me to put sufficiently complex equations into MS docs.


#2

I am curious about the equation not supported. :blush: Do you have an exemple to use as reprex ? Thanks! :slight_smile:


#3

Sure. I'm not sure all the patterns that are not supported but here are three which I have stumbled on.

The following 3 chunks work fine in MathJax:

## scaled division lines 

$$
  x = a_0 + \cfrac{1}{a_1 
          + \cfrac{1}{a_2 
          + \cfrac{1}{a_3 + \cfrac{1}{a_4} } } }
$$

## scaled text

$$
( \big( \Big( \bigg( \Bigg(
$$
## matrix fonts don't scale

$$
M = \begin{bmatrix}
       \frac{5}{6} & \frac{1}{6} & 0           \\[0.3em]
       \frac{5}{6} & 0           & \frac{1}{6} \\[0.3em]
       0           & \frac{5}{6} & \frac{1}{6}
     \end{bmatrix}
$$

Those equation blocks look good here in MathJax or LaTeX:

scaled division lines

x = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cfrac{1}{a_3 + \cfrac{1}{a_4} } } }

scaled text

( \big( \Big( \bigg( \Bigg(

matrix fonts don't scale

M = \begin{bmatrix} \frac{5}{6} & \frac{1}{6} & 0 \\[0.3em] \frac{5}{6} & 0 & \frac{1}{6} \\[0.3em] 0 & \frac{5}{6} & \frac{1}{6} \end{bmatrix}

But they look like this in MS Word/PowerPoint:

It seems the second two formulas reflect that MS Equations don't support font scaling inside the equation. And both of those need some fonts inside the eq to be different in order to look right. The top chunk may be an equation syntax issues, although I have no idea what it is. It renders fine in MathJax & LaTeX but gives me this when rendering to MSFT products:

[WARNING] Could not convert TeX math '
  x = a_0 + \cfrac{1}{a_1 
          + \cfrac{1}{a_2 
          + \cfrac{1}{a_3 + \cfrac{1}{a_4} } } }
  ', rendering as TeX:
  
                  ^
  unexpected "{"
  expecting "%", "\\label", "\\nonumber" or whitespace

#4

I was just looking at what the --webtex pandoc option does for html & md documents. That's exactly what I want to do, but with MSFT documents. Unfortunately it does not look like Pandoc supports using --webtex with word docs. I could save to HTML then import that into word, but then I lose other features...


#5

Not a native solution, but the thread below seems to have a couple of workarounds that might suit your use-case (essentially using external image renderer to generate and embed, which I guess you'd adapt to just download and include):


Further details/source of solutions came from:


#6

from the top thread, I think this will get me there:

By the way the website I just sent doesn't seem to work with github but from @daattali's site you can define a function like

latexImg = function(latex){

    link = paste0('http://latex.codecogs.com/gif.latex?',
           gsub('\\=','%3D',URLencode(latex)))

    link = gsub("(%..)","\\U\\1",link,perl=TRUE)
    return(paste0('![](',link,')'))
}

in any inline location you can do r latexImg('a = \\frac{b}{c}') to place the image you want. note that this has to be Rmarkdown