rmarkdown icon indicating copy to clipboard operation
rmarkdown copied to clipboard

Problem with escaping equation in kable() when rendering to HTML

Open cderv opened this issue 3 years ago • 3 comments

---
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)
```

image

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 > &#124;t&#124;)$|
|:---------|----------:|----------:|---------:|----------------------:|
|$\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 🤔

cderv avatar Jan 12 '22 16:01 cderv

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 &gt; \&amp;#124;t\&amp;#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 \&#124; which gets transformed to \&amp;#124; - not HTML code for Mathjax to deal with.

Not found the cause yet

cderv avatar Jan 12 '22 17:01 cderv

This is an issue outside of table too if HTML escaped character is used in equation \&#124;

---
title: test
output: 
  html_document:
    keep_md: true
---

Usual: `$P(T > |t|)$`

$P(T > |t|)$

Produced by `knitr::kable_mark`: `$P(T > \&#124;t\&#124;)$`

$P(T > \&#124;t\&#124;)$

But it seems that Pandoc has always escaped those to &amp 🤔 Curious why it seems it was working in the past... Tested with Pandoc 2.0.3 and I get the same.

cderv avatar Jan 12 '22 17:01 cderv

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.

zenolee22 avatar May 11 '22 03:05 zenolee22