Skip to content

Commit 556cfc6

Browse files
authored
Merge pull request #11182 from quarto-dev/bugfix/8428
Only forward cross-referenceable labels to tables in cells
2 parents eb72841 + f7a007c commit 556cfc6

4 files changed

Lines changed: 85 additions & 53 deletions

File tree

news/changelog-1.6.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All changes included in 1.6:
1616
## Lua Filters and extensions
1717

1818
- ([#8179](https://github.com/quarto-dev/quarto-cli/issues/8179)): When merging code cells for complex layouts, do not merge cells with different languages.
19+
- ([#8428](https://github.com/quarto-dev/quarto-cli/issues/8428)): only forward cell labels to tables when tables will be cross-referenceable.
1920
- ([#10004](https://github.com/quarto-dev/quarto-cli/issues/10004)): Resolve callout titles, theorem names, and `code-summary` content through `quarto_ast_pipeline()` and `process_shortcodes()`.
2021
- ([#10196](https://github.com/quarto-dev/quarto-cli/issues/10196)): Protect against nil values in `float.caption_long`.
2122
- ([#10328](https://github.com/quarto-dev/quarto-cli/issues/10328)): Interpret subcells as subfloats when subcap count matches subcell count.

src/resources/filters/normalize/flags.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ function compute_flags()
106106

107107
-- FIXME: are we actually triggering this with FloatRefTargets?
108108
-- table captions
109+
local kTblCap = "tbl-cap"
109110
local tblCap = extractTblCapAttrib(node,kTblCap)
110111
if hasTableRef(node) or tblCap then
111112
flags.has_table_captions = true

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

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,64 @@
33

44
local patterns = require("modules/patterns")
55

6-
kTblCap = "tbl-cap"
7-
kTblSubCap = "tbl-subcap"
8-
9-
function table_captions()
10-
return {
6+
function table_captions()
7+
local kTblCap = "tbl-cap"
8+
local kTblSubCap = "tbl-subcap"
9+
return {
1110
Div = function(el)
1211
if tcontains(el.attr.classes, "cell") then
1312
-- extract table attributes
14-
local tblCap = extractTblCapAttrib(el,kTblCap)
13+
local tblCap = extractTblCapAttrib(el, kTblCap)
1514
local tblSubCap = extractTblCapAttrib(el, kTblSubCap, true)
16-
if hasTableRef(el) or tblCap then
17-
local tables = countTables(el)
18-
if tables > 0 then
19-
20-
-- apply captions and labels if we have a tbl-cap or tbl-subcap
21-
if tblCap or tblSubCap then
22-
23-
-- special case: knitr::kable will generate a \begin{tablular} without
24-
-- a \begin{table} wrapper -- put the wrapper in here if need be
25-
if _quarto.format.isLatexOutput() then
26-
el = _quarto.ast.walk(el, {
27-
RawBlock = function(raw)
28-
if _quarto.format.isRawLatex(raw) then
29-
local tabular_match = _quarto.modules.patterns.match_all_in_table(_quarto.patterns.latexTabularPattern)
30-
local table_match = _quarto.modules.patterns.match_all_in_table(_quarto.patterns.latexTablePattern)
31-
if tabular_match(raw.text) and not table_match(raw.text) then
32-
raw.text = raw.text:gsub(
33-
_quarto.modules.patterns.combine_patterns(_quarto.patterns.latexTabularPattern),
34-
"\\begin{table}\n\\centering\n%1%2%3\n\\end{table}\n",
35-
1)
36-
return raw
37-
end
38-
end
39-
end
40-
})
41-
end
42-
43-
-- compute all captions and labels
44-
local label = el.attr.identifier
45-
local mainCaption, tblCaptions, mainLabel, tblLabels = table_captionsAndLabels(
46-
label,
47-
tables,
48-
tblCap,
49-
tblSubCap
50-
)
51-
-- apply captions and label
52-
el.attr.identifier = mainLabel
53-
if mainCaption then
54-
el.content:insert(pandoc.Para(mainCaption))
55-
end
56-
if #tblCaptions > 0 then
57-
el = applyTableCaptions(el, tblCaptions, tblLabels)
15+
if not (tblCap or hasTableRef(el)) then
16+
return
17+
end
18+
if not (tblCap or tblSubCap) then
19+
return
20+
end
21+
local tables = countTables(el)
22+
if tables <= 0 then
23+
return
24+
end
25+
26+
-- special case: knitr::kable will generate a \begin{tabular} without
27+
-- a \begin{table} wrapper -- put the wrapper in here if need be
28+
if _quarto.format.isLatexOutput() then
29+
el = _quarto.ast.walk(el, {
30+
RawBlock = function(raw)
31+
if _quarto.format.isRawLatex(raw) then
32+
local tabular_match = _quarto.modules.patterns.match_all_in_table(_quarto.patterns.latexTabularPattern)
33+
local table_match = _quarto.modules.patterns.match_all_in_table(_quarto.patterns.latexTablePattern)
34+
if tabular_match(raw.text) and not table_match(raw.text) then
35+
raw.text = raw.text:gsub(
36+
_quarto.modules.patterns.combine_patterns(_quarto.patterns.latexTabularPattern),
37+
"\\begin{table}\n\\centering\n%1%2%3\n\\end{table}\n",
38+
1)
39+
return raw
40+
end
5841
end
59-
return el
6042
end
61-
end
43+
})
44+
end
45+
46+
-- compute all captions and labels
47+
local label = el.attr.identifier
48+
local mainCaption, tblCaptions, mainLabel, tblLabels = table_captionsAndLabels(
49+
label,
50+
tables,
51+
tblCap,
52+
tblSubCap
53+
)
54+
-- apply captions and label
55+
el.attr.identifier = mainLabel
56+
if mainCaption then
57+
el.content:insert(pandoc.Para(mainCaption))
58+
end
59+
if #tblCaptions > 0 then
60+
el = applyTableCaptions(el, tblCaptions, tblLabels)
6261
end
62+
return el
6363
end
64-
65-
6664
end
6765
}
6866

@@ -138,7 +136,7 @@ function applyTableCaptions(el, tblCaptions, tblLabels)
138136
cap:extend(tblCaptions[idx])
139137
cap:insert(pandoc.Space())
140138
end
141-
if #tblLabels[idx] > 0 then
139+
if #tblLabels[idx] > 0 and tblLabels[idx]:match("^tbl%-") then
142140
cap:insert(pandoc.Str("{#" .. tblLabels[idx] .. "}"))
143141
end
144142
idx = idx + 1
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: "Test"
3+
format: html
4+
keep-md: true
5+
_quarto:
6+
tests:
7+
html:
8+
ensureFileRegexMatches:
9+
- []
10+
- ['{#test}']
11+
---
12+
13+
14+
::: {#test .cell tbl-cap='Test caption' execution_count=1}
15+
``` {.python .cell-code}
16+
import pandas as pd
17+
from IPython.display import display, Markdown
18+
penguins = pd.read_csv("https://pos.it/palmer-penguins-github-csv")
19+
markdown_result = penguins.groupby("species").size().to_markdown(index=False, tablefmt="pipe", stralign="right")
20+
display(Markdown(markdown_result))
21+
```
22+
23+
::: {.cell-output .cell-output-display .cell-output-markdown}
24+
| 0 |
25+
|----:|
26+
| 152 |
27+
| 68 |
28+
| 124 |
29+
:::
30+
:::
31+
32+

0 commit comments

Comments
 (0)