Skip to content

Instantly share code, notes, and snippets.

@jeroen
Last active August 3, 2022 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroen/ce9cc548d17c97513bc2361f3ccdcb13 to your computer and use it in GitHub Desktop.
Save jeroen/ce9cc548d17c97513bc2361f3ccdcb13 to your computer and use it in GitHub Desktop.
HTML with math svg from KaTeX
<!DOCTYPE html><html><head><title>R: Display math in R documentation</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<link href="/doc/html/prism.css" rel="stylesheet" />
<link rel="stylesheet" href="/doc/html/katex/katex.css">
<script type="text/javascript" src="/doc/html/katex-config.js"></script>
<script defer src="/doc/html/katex/katex.js"
onload="processMathHTML();"></script>
<link rel="stylesheet" type="text/css" href="/doc/html/R.css" />
</head><body><div class="container">
<table style="width: 100%;"><tr><td>math_to_rd {katex}</td><td style="text-align: right;">R Documentation</td></tr></table>
<h2>Display math in R documentation</h2>
<h3>Description</h3>
<p>Helper function to insert tex math expressions into R documentation (<code>.rd</code>) files.
Uses Katex rendering for documentation in html format, and the appropriate latex
macros for documentation rendered in pdf or plain-text.
</p>
<h3>Usage</h3>
<pre><code class='language-R'>math_to_rd(tex, ascii = tex, displayMode = TRUE, ..., include_css = TRUE)
</code></pre>
<h3>Arguments</h3>
<table>
<tr style="vertical-align: top;"><td><code>tex</code></td>
<td>
<p>input string with tex math expression.</p>
</td></tr>
<tr style="vertical-align: top;"><td><code>ascii</code></td>
<td>
<p>alternate text-only representation of the input math to show in
documentation rendered to plain text format.</p>
</td></tr>
<tr style="vertical-align: top;"><td><code>displayMode</code></td>
<td>
<p>render math in centered 2D layout, similar to <code style="white-space: pre;">&#8288;$$&#8288;</code> in tex.
Set to <code>FALSE</code> to render (non-centered) inline layout for use in text.
For pdf output, this corresponds to the <code style="white-space: pre;">&#8288;\deqn{}&#8288;</code> and <code style="white-space: pre;">&#8288;\eqn{}&#8288;</code> macros, see
<a href="https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Mathematics">WRE 2.6: Mathematics</a></p>
</td></tr>
<tr style="vertical-align: top;"><td><code>...</code></td>
<td>
<p>additional html rendering options passed to
<a href="https://katex.org/docs/options.html">katex.render</a></p>
</td></tr>
<tr style="vertical-align: top;"><td><code>include_css</code></td>
<td>
<p>adds the katex css file to the output.
This is only required once per html webpage. Set to <code>FALSE</code> if you include css
files into the your html head some other way.</p>
</td></tr>
</table>
<h3>Details</h3>
<p>Use <code>math_to_rd()</code> inside <code style="white-space: pre;">&#8288;\Sexpr&#8288;</code> to embed math in your R package documentation
pages. For example the code below can be inserted in your <code>rd</code> (or roxygen)
source code:</p>
<pre>\Sexpr[results=rd, stage=build]{
katex::math_to_rd(katex::example_math())
}
</pre>
<p>Which results in the following output:
</p>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" data-external="1">
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>f</mi><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo><mfrac><mn>1</mn><mrow><mi>σ</mi><msqrt><mrow><mn>2</mn><mi>π</mi></mrow></msqrt></mrow></mfrac><msup><mi>e</mi><mrow><mo>−</mo><mfrac><mn>1</mn><mn>2</mn></mfrac><mo stretchy="false">(</mo><mfrac><mrow><mi>x</mi><mo>−</mo><mi>μ</mi></mrow><mi>σ</mi></mfrac><msup><mo stretchy="false">)</mo><mn>2</mn></msup></mrow></msup></mrow><annotation encoding="application/x-tex">f(x)= {\frac{1}{\sigma\sqrt{2\pi}}}e^{- {\frac {1}{2}} (\frac {x-\mu}{\sigma})^2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mclose">)</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:2.2514em;vertical-align:-0.93em;"></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.3214em;"><span style="top:-2.2028em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">σ</span><span class="mord sqrt"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.9072em;"><span class="svg-align" style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord" style="padding-left:0.833em;"><span class="mord">2</span><span class="mord mathnormal" style="margin-right:0.03588em;">π</span></span></span><span style="top:-2.8672em;"><span class="pstrut" style="height:3em;"></span><span class="hide-tail" 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.1328em;"><span></span></span></span></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.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">1</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.93em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:1.0369em;"><span style="top:-3.413em;margin-right:0.05em;"><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 mtight"><span class="mord mtight"><span class="mopen nulldelimiter sizing reset-size3 size6"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8443em;"><span style="top:-2.656em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span><span style="top:-3.2255em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line mtight" style="border-bottom-width:0.049em;"></span></span><span style="top:-3.384em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.344em;"><span></span></span></span></span></span><span class="mclose nulldelimiter sizing reset-size3 size6"></span></span></span><span class="mopen mtight">(</span><span class="mord mtight"><span class="mopen nulldelimiter sizing reset-size3 size6"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.879em;"><span style="top:-2.656em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em;">σ</span></span></span></span><span style="top:-3.2255em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line mtight" style="border-bottom-width:0.049em;"></span></span><span style="top:-3.4624em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size3 size1 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">x</span><span class="mbin mtight">−</span><span class="mord mathnormal mtight">μ</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.344em;"><span></span></span></span></span></span><span class="mclose nulldelimiter sizing reset-size3 size6"></span></span><span class="mclose mtight"><span class="mclose mtight">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913em;"><span style="top:-2.931em;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></span></span></span></span></span></span></span></span></span></span></span>
<p>Optionally you can specify an alternate ascii representation that will be shown in
the plain-text format rendering of the documentation:</p>
<pre>\Sexpr[results=rd, stage=build]{
katex::math_to_rd('E=MC^2', 'E=mc²')
}
</pre>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" data-external="1">
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>E</mi><mo>=</mo><mi>M</mi><msup><mi>C</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">E=MC^2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">E</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:0.8641em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.07153em;">C</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8641em;"><span style="top:-3.113em;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></span></span></span>
<p>If no ascii representation is given, the input tex in displayed verbatim into the
plain-text documentation.
</p>
<h3>Value</h3>
<p>a string with an rd fragment to be included in R documentation
</p>
<h3>Note for Windows</h3>
<p>On Windows, R versions before 4.1.2 had a <a href="https://bugs.r-project.org/show_bug.cgi?id=18152">bug</a>
which could lead to incorrect HTML encoding for <code style="white-space: pre;">&#8288;\Sexpr{}&#8288;</code> output.
As a workaround, we automatically escape non-ascii html characters
on these versions of R. Linux and MacOS are unaffected.
</p>
<h3>See Also</h3>
<p>Other katex:
<code><a href="../../katex/help/katex">katex</a></code>,
<code><a href="../../katex/help/pandoc">pandoc</a></code>
</p>
<hr /><div style="text-align: center;">[Package <em>katex</em> version 1.4.0 <a href="00Index.html">Index</a>]</div>
</div>
<script src="/doc/html/prism.js"></script>
</body></html>
tidy --drop-empty-elements no math_to_rd.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment