Skip to contents

Get 'HTML' or 'MathML' XML codes corresponding to the rendering of 'MathJax' equations.

Usage

transform_mathjax(
  x,
  to = "html",
  display = FALSE,
  strict = "ignore",
  rpr = NULL
)

Arguments

x

MathJax equations

to

output format, one of 'html' or 'mml'

display

should the equation be in display (TRUE) or inline mode (FALSE, by default)

strict

KaTeX option.If "ignore" (default), allow features that make writing LaTeX convenient. Other options are "warn" or "error". This happens E;G;, when you use accented characters in your equations.

rpr

run properties to apply to the 'Office Open XML Math' equations, used only when to = "mml": a <w:rPr> element for 'Microsoft Word' or an <a:rPr> element for 'Microsoft PowerPoint', as a string; the namespace declaration can be omitted. It sets for example the size or the color of the equations, that otherwise get the default values of the document. One value for all equations or one value per equation, NA for none. With NULL (default), no run properties are written.

Value

a character vector that contains 'html' or 'mml' codes corresponding to the equations.

Examples

x <- c("(ax^2 + bx + c = 0)",
       "x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.")
z <- transform_mathjax(x = x, to = "html")
cat(z, sep = "\n\n")
#> <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" data-external="1">
#> <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>a</mi><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mi>b</mi><mi>x</mi><mo>+</mo><mi>c</mi><mo>=</mo><mn>0</mn><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(ax^2 + bx + c = 0)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.0641em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">b</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">0</span><span class="mclose">)</span></span></span></span>
#> 
#> <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" data-external="1">
#> <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo>=</mo><mfrac><mrow><mo>−</mo><mi>b</mi><mo>±</mo><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac><mi mathvariant="normal">.</mi></mrow><annotation encoding="application/x-tex">x = {-b \pm \sqrt{b^2-4ac} \over 2a}.</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.3845em;vertical-align:-0.345em;"></span><span class="mord"><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.0395em;"><span style="top:-2.655em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span><span class="mord mathnormal mtight">a</span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.394em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">−</span><span class="mord mathnormal mtight">b</span><span class="mbin mtight">±</span><span class="mord sqrt mtight"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.9221em;"><span class="svg-align" style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord mtight" style="padding-left:0.833em;"><span class="mord mtight"><span class="mord mathnormal mtight">b</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7463em;"><span style="top:-2.786em;margin-right:0.0714em;"><span class="pstrut" style="height:2.5em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mbin mtight">−</span><span class="mord mtight">4</span><span class="mord mathnormal mtight">a</span><span class="mord mathnormal mtight">c</span></span></span><span style="top:-2.8821em;"><span class="pstrut" style="height:3em;"></span><span class="hide-tail mtight" style="min-width:0.853em;height:1.08em;"><svg xmlns="http://www.w3.org/2000/svg" width='400em' height='1.08em' viewBox='0 0 400000 1080' preserveAspectRatio='xMinYMin slice'><path d='M95,702
#> c-2.7,0,-7.17,-2.7,-13.5,-8c-5.8,-5.3,-9.5,-10,-9.5,-14
#> c0,-2,0.3,-3.3,1,-4c1.3,-2.7,23.83,-20.7,67.5,-54
#> c44.2,-33.3,65.8,-50.3,66.5,-51c1.3,-1.3,3,-2,5,-2c4.7,0,8.7,3.3,12,10
#> s173,378,173,378c0.7,0,35.3,-71,104,-213c68.7,-142,137.5,-285,206.5,-429
#> c69,-144,104.5,-217.7,106.5,-221
#> l0 -0
#> c5.3,-9.3,12,-14,20,-14
#> H400000v40H845.2724
#> s-225.272,467,-225.272,467s-235,486,-235,486c-2.7,4.7,-9,7,-19,7
#> c-6,0,-10,-1,-12,-3s-194,-422,-194,-422s-65,47,-65,47z
#> M834 80h400000v40h-400000z'/></svg></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1179em;"><span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.345em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span><span class="mord">.</span></span></span></span>
z <- transform_mathjax(x = x, to = "mml")
cat(z, sep = "\n\n")
#> 
#> <m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
#>   <m:r>
#>     <m:t>(a</m:t>
#>   </m:r>
#>   <m:sSup>
#>     <m:e>
#>       <m:r>
#>         <m:t>x</m:t>
#>       </m:r>
#>     </m:e>
#>     <m:sup>
#>       <m:r>
#>         <m:t>2</m:t>
#>       </m:r>
#>     </m:sup>
#>   </m:sSup>
#>   <m:r>
#>     <m:t>+bx+c=0)</m:t>
#>   </m:r>
#> </m:oMath>
#> 
#> 
#> 
#> <m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
#>   <m:r>
#>     <m:t>x=</m:t>
#>   </m:r>
#>   <m:f>
#>     <m:fPr>
#>       <m:type m:val="bar"/>
#>     </m:fPr>
#>     <m:num>
#>       <m:r>
#>         <m:t>−b±</m:t>
#>       </m:r>
#>       <m:rad>
#>         <m:radPr>
#>           <m:degHide m:val="on"/>
#>         </m:radPr>
#>         <m:deg/>
#>         <m:e>
#>           <m:sSup>
#>             <m:e>
#>               <m:r>
#>                 <m:t>b</m:t>
#>               </m:r>
#>             </m:e>
#>             <m:sup>
#>               <m:r>
#>                 <m:t>2</m:t>
#>               </m:r>
#>             </m:sup>
#>           </m:sSup>
#>           <m:r>
#>             <m:t>−4ac</m:t>
#>           </m:r>
#>         </m:e>
#>       </m:rad>
#>     </m:num>
#>     <m:den>
#>       <m:r>
#>         <m:t>2a</m:t>
#>       </m:r>
#>     </m:den>
#>   </m:f>
#>   <m:r>
#>     <m:rPr>
#>       <m:sty m:val="p"/>
#>     </m:rPr>
#>     <m:t>.</m:t>
#>   </m:r>
#> </m:oMath>
#> 

# equations at 16 points in a Word document
z <- transform_mathjax(
  x = x, to = "mml",
  rpr = "<w:rPr><w:sz w:val=\"32\"/><w:szCs w:val=\"32\"/></w:rPr>"
)
cat(z, sep = "\n\n")
#> 
#> <m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
#>   <m:r>
#>     <w:rPr>
#>       <w:sz w:val="32"/>
#>       <w:szCs w:val="32"/>
#>     </w:rPr>
#>     <m:t>(a</m:t>
#>   </m:r>
#>   <m:sSup>
#>     <m:sSupPr>
#>       <m:ctrlPr>
#>         <w:rPr>
#>           <w:sz w:val="32"/>
#>           <w:szCs w:val="32"/>
#>         </w:rPr>
#>       </m:ctrlPr>
#>     </m:sSupPr>
#>     <m:e>
#>       <m:r>
#>         <w:rPr>
#>           <w:sz w:val="32"/>
#>           <w:szCs w:val="32"/>
#>         </w:rPr>
#>         <m:t>x</m:t>
#>       </m:r>
#>     </m:e>
#>     <m:sup>
#>       <m:r>
#>         <w:rPr>
#>           <w:sz w:val="32"/>
#>           <w:szCs w:val="32"/>
#>         </w:rPr>
#>         <m:t>2</m:t>
#>       </m:r>
#>     </m:sup>
#>   </m:sSup>
#>   <m:r>
#>     <w:rPr>
#>       <w:sz w:val="32"/>
#>       <w:szCs w:val="32"/>
#>     </w:rPr>
#>     <m:t>+bx+c=0)</m:t>
#>   </m:r>
#> </m:oMath>
#> 
#> 
#> 
#> <m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
#>   <m:r>
#>     <w:rPr>
#>       <w:sz w:val="32"/>
#>       <w:szCs w:val="32"/>
#>     </w:rPr>
#>     <m:t>x=</m:t>
#>   </m:r>
#>   <m:f>
#>     <m:fPr>
#>       <m:type m:val="bar"/>
#>       <m:ctrlPr>
#>         <w:rPr>
#>           <w:sz w:val="32"/>
#>           <w:szCs w:val="32"/>
#>         </w:rPr>
#>       </m:ctrlPr>
#>     </m:fPr>
#>     <m:num>
#>       <m:r>
#>         <w:rPr>
#>           <w:sz w:val="32"/>
#>           <w:szCs w:val="32"/>
#>         </w:rPr>
#>         <m:t>−b±</m:t>
#>       </m:r>
#>       <m:rad>
#>         <m:radPr>
#>           <m:degHide m:val="on"/>
#>           <m:ctrlPr>
#>             <w:rPr>
#>               <w:sz w:val="32"/>
#>               <w:szCs w:val="32"/>
#>             </w:rPr>
#>           </m:ctrlPr>
#>         </m:radPr>
#>         <m:deg/>
#>         <m:e>
#>           <m:sSup>
#>             <m:sSupPr>
#>               <m:ctrlPr>
#>                 <w:rPr>
#>                   <w:sz w:val="32"/>
#>                   <w:szCs w:val="32"/>
#>                 </w:rPr>
#>               </m:ctrlPr>
#>             </m:sSupPr>
#>             <m:e>
#>               <m:r>
#>                 <w:rPr>
#>                   <w:sz w:val="32"/>
#>                   <w:szCs w:val="32"/>
#>                 </w:rPr>
#>                 <m:t>b</m:t>
#>               </m:r>
#>             </m:e>
#>             <m:sup>
#>               <m:r>
#>                 <w:rPr>
#>                   <w:sz w:val="32"/>
#>                   <w:szCs w:val="32"/>
#>                 </w:rPr>
#>                 <m:t>2</m:t>
#>               </m:r>
#>             </m:sup>
#>           </m:sSup>
#>           <m:r>
#>             <w:rPr>
#>               <w:sz w:val="32"/>
#>               <w:szCs w:val="32"/>
#>             </w:rPr>
#>             <m:t>−4ac</m:t>
#>           </m:r>
#>         </m:e>
#>       </m:rad>
#>     </m:num>
#>     <m:den>
#>       <m:r>
#>         <w:rPr>
#>           <w:sz w:val="32"/>
#>           <w:szCs w:val="32"/>
#>         </w:rPr>
#>         <m:t>2a</m:t>
#>       </m:r>
#>     </m:den>
#>   </m:f>
#>   <m:r>
#>     <m:rPr>
#>       <m:sty m:val="p"/>
#>     </m:rPr>
#>     <w:rPr>
#>       <w:sz w:val="32"/>
#>       <w:szCs w:val="32"/>
#>     </w:rPr>
#>     <m:t>.</m:t>
#>   </m:r>
#> </m:oMath>
#>