rmarkdown
rmarkdown copied to clipboard
Problem with escaping equation in kable() when rendering to HTML
---
title: test
output: html_document
---
```{r}
m <- lm(dist ~ speed, data = cars)
d <- coef(summary(m))
rownames(d) <- c("$\\beta_0$", "$\\beta_1$")
colnames(d)[4] <- "$P(T > |t|)$"
knitr::kable(d, escape = FALSE)
```
The escaping will happen in https://github.com/yihui/knitr/blob/master/R/table.R#L390-L392
Intermediate markdown passed to Pandoc will have this table
| | Estimate| Std. Error| t value| $P(T > |t|)$|
|:---------|----------:|----------:|---------:|----------------------:|
|$\beta_0$ | -17.579095| 6.7584402| -2.601058| 0.0123188|
|$\beta_1$ | 3.932409| 0.4155128| 9.463990| 0.0000000|
It does not seem to be a Pandoc issue as I tested with different Pandoc version without problem...
I don't really know what is wrong in the mathjax processing 🤔
The HTML code generated is this one:
<table>
<thead>
<tr class="header">
<th align="left"></th>
<th align="right">Estimate</th>
<th align="right">Std. Error</th>
<th align="right">t value</th>
<th align="right"><span class="math inline">\(P(T > \&#124;t\&#124;)\)</span></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left"><span class="math inline">\(\beta_0\)</span></td>
<td align="right">-17.579095</td>
<td align="right">6.7584402</td>
<td align="right">-2.601058</td>
<td align="right">0.0123188</td>
</tr>
<tr class="even">
<td align="left"><span class="math inline">\(\beta_1\)</span></td>
<td align="right">3.932409</td>
<td align="right">0.4155128</td>
<td align="right">9.463990</td>
<td align="right">0.0000000</td>
</tr>
</tbody>
</table>
So there is indeed an issue possibly with Pandoc because knitr will modify |
to \|
which gets transformed to \&#124;
- not HTML code for Mathjax to deal with.
Not found the cause yet
This is an issue outside of table too if HTML escaped character is used in equation \|
---
title: test
output:
html_document:
keep_md: true
---
Usual: `$P(T > |t|)$`
$P(T > |t|)$
Produced by `knitr::kable_mark`: `$P(T > \|t\|)$`
$P(T > \|t\|)$
But it seems that Pandoc has always escaped those to &
🤔 Curious why it seems it was working in the past...
Tested with Pandoc 2.0.3 and I get the same.
I've been having the same issue where I can't have both math equations and a formatted table via kable with an escape=F
and have them both render correctly. I've resorted to taking a screenshot of the equation and embedding it as an image instead, but would love to find a solution to this.