Converting withMathJax equations in R shiny to word doc

40 views Asked by At

I have a shiny app in R that includes lots of equations with the withMathJax package. For example:

 tags$div(HTML("<script type='text/x-mathjax-config' >
 MathJax.Hub.Config({
 tex2jax: {inlineMath: [['$','$']]}
 });
 </script >")),

 p(withMathJax("$$n_{\\sf egg_{\\it i}} \\sim {\\sf negative\\,binomial}(\\lambda_{\\sf fecund}, k_{\\sf fecund})$$"), style="text-align: center; font-size:20px;"),
           

returns

enter image description here

I need to reproduce all of the equations in a word document. I could manually re-create all of these, but it would be so much simpler to do this programmatically (especially as the equations change).

I am hoping to use pandoc to convert MathJax to LaTeX (something like this: https://stackoverflow.com/a/20501901/9096420), because word appears to have a LaTeX option, but I cannot figure out how to get it to work. Does anyone has a better idea?


Here is what I have tried:

I have installed "pandoc-3.1.11.1-windows-x86_64.zip" from here https://github.com/jgm/pandoc/releases/tag/3.1.11.1 and unzipped it in a directory of my choice. Following this answer to another question (https://stackoverflow.com/a/29109007/9096420), I set the path in PowerShell:

$PATH = [Environment]::GetEnvironmentVariable("PATH")
$pandoc_path = "C:\Users\UserName\Documents\Pandoc\pandoc-3.1.11.1\"
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$pandoc_path")

PowerShell recognizes pandoc, but is having trouble with the formatting. I've created "MathJaxEquations.txt" that contains a single line:

n_{\\sf egg_{\\it i}} \\sim {\\sf negative\\,binomial}(\\lambda_{\\sf fecund}, k_{\\sf fecund})

or wrapped in $. When I follow the above answer and try

pandoc -f MathJaxEquations.txt+tex_math_dollars+tex_math_single_backslash -t latex

I get

Unknown input format MathJaxEquations.txt

I can use regexpr in R to convert my code to whatever it needs to be for pandoc (I've never worked in anything other than R), but I am not really sure how to figure out what format pandoc is looking for (or if there is an easier way). Another idea I had was trying to put these withMathJax() lines in Rmd and knitting to word, but not sure how I can do that either. Any help pointing me in the right direction is much appreciated.

1

There are 1 answers

0
mweylandt On

RMarkdown can definitely render math formulas into Word. The issue is that it wants LateX style maths, and MathJax formulas are different. Your formula above would be written:

$$n_{ egg_{i}} \sim {\text{negative binomial}}(\lambda_{fecund}, k_{ fecund})$$

If you put that into an RMarkdown document it will render without an issue. The problem is that it seems you want to use the same formula in the shiny app and your word doc, and doing the mathjax-LaTeX conversion would add at least a different pandoc step to the pipeline.

Luckily the withMathJax function does allow you providing it with LaTeX formulas. So you can be consistent by using only LaTeX formulas.

A final hiccup -- the formula needs extra \ to work as a character in an R code chunk. you can print it with cat (whether directly, or as shown below as part of the Shiny output), and tell the chunk to output asis, and you can render to word and html. screenshot of the word doc

---
title: "Untitled"
author: "author"
date: "2024-02-03"
output:
  md_document: default
  html_document: default
  word_document: default
keep_md: yes
---

This works in RMarkdown -- but `withMathJax` can't handle the single \ because it escapes the string

$$n_{ egg_{i}} \sim {\text{negative binomial}}(\lambda_{fecund}, k_{ fecund})$$

Here is rendered to Word: 

```{r echo =FALSE, results = 'asis'}

t <- shiny::withMathJax("$$n_{ egg_{i}} \\sim {\\text{negative binomial}}(\\lambda_{fecund}, k_{ fecund})$$
")

cat(t[[2]]) # for now, printing from list created above