quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.94k stars 325 forks source link

Using `%` in `tbl-cap` of `kableExtra` table breaks rendering with error in `applyTableCaptions` #810

Closed selinaZitrone closed 2 years ago

selinaZitrone commented 2 years ago

Rendering of the following doc (both html and pdf) returns an error in quarto-pre.lua:

---
format: html
---

@tbl-iris

```{r}
#| label: tbl-iris
#| tbl-cap: "95%"
kableExtra::kbl(head(iris))


The error is:

![image](https://user-images.githubusercontent.com/38790212/167035195-b812ed1e-dd66-40d0-8add-253a95f8c32e.png)

The document compiles fine if I remove the `%` sign form the caption or print the table with `knitr::kable` instead of `kableExtra`.

I am on Windows 10, Quarto 0.9.363 and use it with RStudio 2022.02.1.

Is there a way to fix this in Quarto or is it more of a `kableExtra` issue?
jjallaire commented 2 years ago

Thanks for reporting this! It turns out we needed to handle some escaping to accommodate the %. Fix is here: https://github.com/quarto-dev/quarto-cli/commit/e357d1c5232082fc0e3f9da9c7d377d5c1563a4d

selinaZitrone commented 2 years ago

Thanks for fixing the bug so quickly. I found that this renders nicely the html document. However, for pdf, there is still an error thrown:

---
format: pdf
---

```{r}
#| label: tbl-rel-diff
#| tbl-cap: "Median and 95% confidence interval"
kableExtra::kbl(head(iris))


This gives me the following error:

![image](https://user-images.githubusercontent.com/38790212/167103405-d6c98d35-4279-42fe-b749-d83e07319e30.png)

Looking into the `.tex` file, I find that the `%` is not escaped an therefore the closing `}` of the caption is turned into a comment:

![image](https://user-images.githubusercontent.com/38790212/167103607-99965517-b73d-4a2a-be6f-84a11ee8f463.png)

I tried to look into the quarto code to fix it but don't understand lua enough to find the mistake.
cderv commented 2 years ago

In LaTeX, the % needs it self to be escaped. So when we are writing the caption code in YAML we need to produce \%.

@jjallaire I think we need to do that in the pre processing only when we are creating a caption

diff --git a/src/resources/filters/quarto-pre/table-captions.lua b/src/resources/filters/quarto-pre/table-captions.lua
index 3c1eb9f9e..e5870aaf0 100644
--- a/src/resources/filters/quarto-pre/table-captions.lua
+++ b/src/resources/filters/quarto-pre/table-captions.lua
@@ -216,7 +216,7 @@ function applyLatexTableCaption(latex, tblCaption, tblLabel, tablePattern)
   if #tblLabel > 0 then
     captionText = captionText .. " {#" .. tblLabel .. "}"
   end
-  latex = latex:gsub(latexCaptionPattern, "%1" .. captionText:gsub("%%", "%%%%") .. "%3", 1)
+  latex = latex:gsub(latexCaptionPattern, "%1" .. captionText:gsub("%%", "\\%%%%") .. "%3", 1)
   return latex
 end

I think we need to add a escaped function to process caption so that we handle any of the 10 special chars if found in caption

{ } % & $ # _ ^ ~ \

I'll submit a PR

jjallaire commented 2 years ago

Thanks @cderv! This should now be fixed here: https://github.com/quarto-dev/quarto-cli/pull/814