Skip to content

Commit 519ca09

Browse files
authored
Convert parsed table caption to HTML to keep formatting (#2583)
* Convert parsed table caption to HTML to keep formatting * Also apply for PagedHtmlTable * Correctly handles LaTeX tables * Add a test for HTML and LaTeX * Correct regex for testing * Add changelog [skip ci]
1 parent 4df1cae commit 519ca09

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

news/changelog-1.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
- the API for shortcode handlers in lua now accepts a fourth parameter `raw_args` which hold the unparsed arguments in a table ([#3833](https://github.com/quarto-dev/quarto-cli/issues/3833)).
109109
- remove scaffolding div from conditional content in final output ([#3847](https://github.com/quarto-dev/quarto-cli/issues/3847)).
110110
- ensure proof titles are appended to paragraph nodes ([#3772](https://github.com/quarto-dev/quarto-cli/issues/3772)).
111+
- Support parsing markdown in table captions in LaTeX and HTML tables ([#2573](https://github.com/quarto-dev/quarto-cli/issues/2573)).
111112

112113
## Pandoc filter changes
113114

src/resources/filters/quarto-pre/table-captions.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function applyTableCaptions(el, tblCaptions, tblLabels)
167167
-- apply table caption and label
168168
local beginCaption, captionText, endCaption = raw.text:match(captionPattern)
169169
if #tblCaptions[idx] > 0 then
170-
captionText = pandoc.utils.stringify(tblCaptions[idx])
170+
captionText = stringEscape(tblCaptions[idx], "html")
171171
end
172172
if #tblLabels[idx] > 0 then
173173
captionText = captionText .. " {#" .. tblLabels[idx] .. "}"
@@ -182,7 +182,7 @@ function applyTableCaptions(el, tblCaptions, tblLabels)
182182
end
183183
elseif hasPagedHtmlTable(raw) then
184184
if #tblCaptions[idx] > 0 then
185-
local captionText = pandoc.utils.stringify(tblCaptions[idx])
185+
local captionText = stringEscape(tblCaptions[idx], "html")
186186
if #tblLabels[idx] > 0 then
187187
captionText = captionText .. " {#" .. tblLabels[idx] .. "}"
188188
end
@@ -210,10 +210,8 @@ function applyLatexTableCaption(latex, tblCaption, tblLabel, tablePattern)
210210
-- apply table caption and label
211211
local beginCaption, captionText, endCaption = latex:match(latexCaptionPattern)
212212
if #tblCaption > 0 then
213-
captionText = pandoc.utils.stringify(tblCaption)
213+
captionText = stringEscape(tblCaption, "latex")
214214
end
215-
-- escape special characters for LaTeX
216-
captionText = stringEscape(captionText, "latex")
217215
if #tblLabel > 0 then
218216
captionText = captionText .. " {#" .. tblLabel .. "}"
219217
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: markdown in captions
3+
format:
4+
html: default
5+
latex: default
6+
_quarto:
7+
tests:
8+
html:
9+
ensureHtmlElements:
10+
- ["#html caption sup", "#html caption em", "#paged div.table-caption sup", "#paged div.table-caption em", "#md caption sup"]
11+
latex:
12+
ensureFileRegexMatches:
13+
- ["\\\\textsuperscript\\{superscript\\}", "\\\\emph\\{italics\\}"]
14+
execute:
15+
echo: false
16+
---
17+
18+
# HTML table {#html}
19+
```{r}
20+
#| tbl-cap: Using ^superscript^ or _italics_ in table caption
21+
#| eval: !expr knitr::is_html_output()
22+
knitr::kable(head(cars), format = "html")
23+
```
24+
25+
# Paged table {#paged}
26+
27+
```{r}
28+
#| tbl-cap: Using ^superscript^ or _italics_ in table caption
29+
#| eval: !expr knitr::is_html_output()
30+
rmarkdown::paged_table(head(cars))
31+
```
32+
33+
# Mardown Table {#md}
34+
35+
```{r}
36+
#| tbl-cap: Using ^superscript^ in caption
37+
knitr::kable(head(cars))
38+
```
39+
40+
# LaTeX table {#latex}
41+
42+
```{r}
43+
#| tbl-cap: Using _italics_ in caption
44+
#| eval: !expr knitr::is_latex_output()
45+
knitr::kable(head(cars), format = "latex")
46+
```

0 commit comments

Comments
 (0)