rstudio / pagedown

Paginate the HTML Output of R Markdown with CSS for Print
https://pagedown.rbind.io
Other
894 stars 127 forks source link

Problem with rendering flextable when using equations in pagedown #332

Closed sylvaine31 closed 1 month ago

sylvaine31 commented 1 month ago

Hi, I'm not sure wheter it is a flextable or a pagedown issue. When using equations in a pagedown::html_paged Rmd, flextable does not render. my reprex :

---
title: "Blabla"
author: "ploupi"
date: "`r format(Sys.time(), '%d %B %Y')`"
output: pagedown::html_paged
---

# mtcars with flextable does not render

```{r echo=TRUE}

ploup <- mtcars |> 
  head() |> 
  dplyr::select(mpg, cyl)

  flextable::flextable(ploup) |> 
    flextable::autofit()

mtcars with print renders


print(ploup)

the equation

$$ Y = \frac{a_{x}-b} {c} $$


If I replace the equation by a R chunk , the flextable renders without problem : 

equatags::transform_mathjax("Y = \\frac{a_{x}-b} {c}") |> 
  htmltools::HTML()
cderv commented 1 month ago

👋 @sylvaine31

First, advice on formatting: Use one more backtick to wrap you rmd source. See https://yihui.org/issue/#please-format-your-issue-correctly

Then, advice on reprex: With newer R, use |> and not %>% which allows in reprex to avoid having to laod magrittr (which is missing in our example here).

Regarding the issue, it seems there are some changes in flextable that requires some adaptation in pagedown.

Using flextable 0.8.5, it works but then starting from 0.8.6 it fails with table not showing.

So I need to check with @davidgohel what are the changes between these version. Repo has not tagged version so not easy to compare the change, but I'll do a bit of archeology.

cderv commented 1 month ago

Thanks for CRAN snapshot github repo, we can access the difference in the version

Not the commit history, but it could give us some hint still

cderv commented 1 month ago

It could be related to tabwid.js addition which may not run correctly with paged.js applying 🤔

cderv commented 1 month ago

Possibly related to previous problem

where the fix does not work anymore

cderv commented 1 month ago

And this search lead me to this commit

I think that flextable now always output shadow dom, and #216 still applies because the ft.shadow option is not existing anymore in flextable (https://github.com/davidgohel/flextable/commit/439d93c55339b0d1ff7929fbfa80d470adf75308#diff-f0c168982bc3b1afb5f98bd5e0230be9a96126d723db777bfde5b3dfeacf9b00L1012)

@davidgohel I'll see what I can do in pagedown but if there is a way to still not use shadow dom in paged.js context, it would be cool.

Otherwise, it will need to wait for an update in Paged.js inside pagedown but I am not sure that paged.js really support shadow dom

So we may be blocked here. 🤔

davidgohel commented 1 month ago

Hi @cderv

Is there a way to detect with knitr (or any option) that the format is pagedown? Something like knitr::is_html_output()?

cderv commented 1 month ago

Is there a way to detect with knitr (or any option) that the format is pagedown? Something like knitr::is_html_output()?

I don't think there is yet a straightforward way. 🤔

@yihui just to confirm: Do we have a way in knitr to know which rmarkdkown format as triggered the render ?

yihui commented 1 month ago

We don't, but we can set a flag similar to https://github.com/rstudio/pagedown/blob/410def190672fb3f854be7f89ac0200d80b79ec6/R/paged.R#L176-L178

I feel the flag should be set in opts_knit instead of opts_chunk since it's global to the document instead of individual chunks.

With that flag (say, named is.paged.js), flextable can detect knitr::opts_knit$get('is.pagedjs').

davidgohel commented 1 month ago

that would be great, I could read knitr::opts_knit$get('is.pagedjs') in flextable and you could remove that fmt$knitr$opts_chunk[['ft.shadow']] = FALSE in pagedown

cderv commented 1 month ago

@davidgohel you can try out https://github.com/rstudio/pagedown/pull/333

I have kept fmt$knitr$opts_chunk[['ft.shadow']] = FALSE for backward compatibility with older flextable

davidgohel commented 1 month ago

@sylvaine31 the fix has been applied in 'flextable', you should be able to display flextables again with dev version of 'pagedown' and dev version of flextable.