jimhester / knitrBootstrap

A framework to create bootstrap styled HTML reports from knitr Rmarkdown.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Math markup

TanyaMurphy opened this issue · comments

I noticed certain texmath entries in .Rmd docs are being marked up differently in knitrBootstrap than in RStudio's HTML parser (correct term?):

knitrBootstrap: .... (lots of these)
Rstudio's HTML: (.....) (the usual mathjax notation)

I'm getting better results with the second syntax; a table filled with equations renders properly with RStudio's knit button, but not with knitrBootstrap (specified with YAML block). Would it be possible to use the same markup in knitrBootstrap as in RStudio's HTML method for math?

Could you post a minimally reproducible example of an Rmd that does not work as expected. Both knitrBootstrap and normal Rmarkdown use Mathjax to render mathematical symbols, so I am not sure why you are seeing a difference without an example.

Thanks!


output:
knitrBootstrap::bootstrap_document:
theme: united
highlight: github

theme.chooser: TRUE

Calibration and inversion regression

The most commonly recommended method for calibration is least squares (non-)linear regression (using a data set consisting of calibrators of known concentration and their signal readings) followed by calculation of the unknown sample concentrations from the inverse of the estimated function. This is sometimes called inverse regression, but note that the inverse function is not being directly estimated.

Common calibration curve models. See below for parameter definitions:

Name Function Inverse
Linear $y = int + b * x$ $x = \frac{y-int}{b}$
Michaelis-Menten $y = \frac{Vm*x}{k+x}$ $x = \frac{k*y}{Vm-y}$
4P Logistic $y = d + \frac{a-d}{1 + (\frac{x}{c})^b}$ $x = c \Big(\frac{a-d}{y-d} -1 \Big)^{1/b}$
5P Logistic $y = d + \frac{(a-d)}{\Big(1 + (\frac{x}{c})^b\Big)^g}$ $x = c \Big((\frac{a-d}{y-d})^{1/g} -1 \Big)^{1/b}$

Note: In our R scripts, we will use more descriptive labels for the parameters of the logistic models than used by Dunn and Wild or Findlay. These will serve as a memory aid, but also single letter labels are at high risk of being confused with or replaced by a data set or temporary variable.

  • $b$ (beta or hill): Rate of change, slope, "slope factor" or "Hill slope". In the case of linear regression, the units of $b$ are x-units per y-unit.

Thanks!

From http://docs.mathjax.org/en/latest/start.html#tex-and-latex-input it states that the default escape for Tex math in MathJax is $$ rather than $. So you just need to add an extra $ around your math expressions. I verified this fixes the issue. RStudio must either change the default delimiter to a single $ or add the extra $ for you. I also verified that double dollars renders properly for regular rmarkdown as well, so there should be no problem using them for everything.

Thanks, that solves the problem in some parts. (BTW, thank you for the package. The source toggle is brilliant!)

render() does accept inline equations using $ ... $, however. It produces a more compact table. I tried it using the 'Knit' button with and without your YAML block to reduce any possible variability from my side.

Here is the .Rmd:

---
output:
  knitrBootstrap::bootstrap_document:
    title: "Test file"
    theme: united
    highlight: github
    theme.chooser: TRUE
    highlight.chooser: TRUE

---


| Name               |      Function                                           |       Inverse             |
|:-------------------|:--------------------------------------------------------|:--------------------------|
| *Linear*           | $y = int + b * x$               |   $x = \frac{y-int}{b}$     |
| *Michaelis-Menten* |   $y = \frac{Vm*x}{k+x}$           |    $x = \frac{k*y}{Vm-y}$    |

The reciprocal of the variance function is used to weight the data; that is, if 

$$ Var(y) = a * Mean(y)^{theta} $$

then 

$$ weight(obs) = standardized[1/EstimatedMean(y.group)^{theta}] $$

Here is the relevant html portion using knitrBootstrap (i.e. 'Knit' with the YAML block):


<table>
<thead>
<tr class="header">
<th align="left">Name</th>
<th align="left">Function</th>
<th align="left">Inverse</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left"><em>Linear</em></td>
<td align="left"><span class="math"><em>y</em> = <em>i</em><em>n</em><em>t</em> + <em>b</em> * <em>x</em></span></td>
<td align="left"><span class="math">$x = \frac{y-int}{b}$</span></td>
</tr>
<tr class="even">
<td align="left"><em>Michaelis-Menten</em></td>
<td align="left"><span class="math">$y = \frac{Vm*x}{k+x}$</span></td>
<td align="left"><span class="math">$x = \frac{k*y}{Vm-y}$</span></td>
</tr>
</tbody>
</table>

<p>The reciprocal of the variance function is used to weight the data; that is, if</p>
<p><br /><span class="math"><em>V</em><em>a</em><em>r</em>(<em>y</em>) = <em>a</em> * <em>M</em><em>e</em><em>a</em><em>n</em>(<em>y</em>)<sup><em>t</em><em>h</em><em>e</em><em>t</em><em>a</em></sup></span><br /></p>
<p>then</p>
<p><br /><span class="math"><em>w</em><em>e</em><em>i</em><em>g</em><em>h</em><em>t</em>(<em>o</em><em>b</em><em>s</em>) = <em>s</em><em>t</em><em>a</em><em>n</em><em>d</em><em>a</em><em>r</em><em>d</em><em>i</em><em>z</em><em>e</em><em>d</em>[1/<em>E</em><em>s</em><em>t</em><em>i</em><em>m</em><em>a</em><em>t</em><em>e</em><em>d</em><em>M</em><em>e</em><em>a</em><em>n</em>(<em>y</em>.<em>g</em><em>r</em><em>o</em><em>u</em><em>p</em>)<sup><em>t</em><em>h</em><em>e</em><em>t</em><em>a</em></sup>]</span><br /></p>

Here is the html using the default settings (i.e. 'Knit' without the YAML block):


<table>
<thead>
<tr class="header">
<th align="left">Name</th>
<th align="left">Function</th>
<th align="left">Inverse</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left"><em>Linear</em></td>
<td align="left"><span class="math">\(y = int + b * x\)</span></td>
<td align="left"><span class="math">\(x = \frac{y-int}{b}\)</span></td>
</tr>
<tr class="even">
<td align="left"><em>Michaelis-Menten</em></td>
<td align="left"><span class="math">\(y = \frac{Vm*x}{k+x}\)</span></td>
<td align="left"><span class="math">\(x = \frac{k*y}{Vm-y}\)</span></td>
</tr>
</tbody>
</table>

<p>The reciprocal of the variance function is used to weight the data; that is, if</p>
<p><span class="math">\[ Var(y) = a * Mean(y)^{theta} \]</span></p>
<p>then</p>
<p><span class="math">\[ weight(obs) = standardized[1/EstimatedMean(y.group)^{theta}] \]</span></p>

In the table, for some reason the first cell is parsed differently than the other cells.
I've tried variations such as using the html (<span class="math">\(...) directly in .Rmd file, but no luck so far.

In the paragraph, the equation blocks end up as left-justified regular italics instead of centered in MathJax style.

The inline $'s are an extra MathJax option that it looks like rmarkdown adds with the reveal.js plugin. See

https://github.com/rstudio/rmarkdown/blob/fef2f3addb9b613b8c9acbafe4043c10da5a8a9d/inst/rmd/revealjs/reveal.js-2.6.1/plugin/math/math.js#L15-L17

I should probably set that option as well so that the syntax is compatible, thanks for the report!

There are some other gotchas with inline math using MathJax. Since a single '$' often appears in not-math-typesetting contexts, some versions of MathJax don't acknowledge it as a signal to start typesetting an inline equation. Usually one of \( a=w-x \) or \\( a=w-x \\) will work. I think the spaces after the '(' and before the ')' must be there, but try it for yourself.

If I use the regular style of $\beta$ together with knitr bootstrap, it produces the warning message:

[WARNING] Could not convert TeX math '\frac{p}{1-p}', rendering as TeX

And indeed the equations are not printed. If I use two $ as suggested above, then the equation become centered and I cannot use them with text in between or inline anymore. Is there a way for me to fix this? Because other than that, knitr bootstrap look very nice. the only other things that does not work is bibtex citations but for my website this is not (yet so important) but I cannot use it if latex equations are not rendered correctly. If you know how I could fix this @jimhester , please give me a hint. Some equations seems to work for reasons I do not understand, e.g. this

$Pr(y=1) = p_i$ and $logit(p_i) = X_i\beta$

works even though it is embedded similar to the other latex equations.

Thanks jimhester for the package!