Skip to content

Commit d7efc45

Browse files
authored
Merge pull request #930 from quarto-dev/latex-tables-caption
Support more table environment configuration for cross referencing
2 parents 592c453 + 4ed012d commit d7efc45

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,29 @@ kTblSubCap = "tbl-subcap"
66

77
local latexTableWithOptionsPattern = "(\\begin{table}%[%w+%])(.*)(\\end{table})"
88
local latexTablePattern = "(\\begin{table})(.*)(\\end{table})"
9+
local latexLongtablePatternwWithPosAndAlign = "(\\begin{longtable}%[[^%]]+%]{.*})(.*)(\\end{longtable})"
10+
local latexLongtablePatternWithPos = "(\\begin{longtable}%[[^%]]+%])(.*)(\\end{longtable})"
11+
local latexLongtablePatternWithAlign = "(\\begin{longtable}{.*})(.*)(\\end{longtable})"
912
local latexLongtablePattern = "(\\begin{longtable})(.*)(\\end{longtable})"
13+
local latexTabularPatternWithPosAndAlign = "(\\begin{tabular}%[[^%]]+%]{.*})(.*)(\\end{tabular})"
14+
local latexTabularPatternWithPos = "(\\begin{tabular}%[[^%]]+%])(.*)(\\end{tabular})"
15+
local latexTabularPatternWithAlign = "(\\begin{tabular}{.*})(.*)(\\end{tabular})"
1016
local latexTabularPattern = "(\\begin{tabular})(.*)(\\end{tabular})"
1117

1218
local latexTablePatterns = pandoc.List({
1319
latexTableWithOptionsPattern,
1420
latexTablePattern,
21+
latexLongtablePatternwWithPosAndAlign,
22+
latexLongtablePatternWithPos,
23+
latexLongtablePatternWithAlign,
1524
latexLongtablePattern,
25+
latexTabularPatternWithPosAndAlign,
26+
latexTabularPatternWithPos,
27+
latexTabularPatternWithAlign,
1628
latexTabularPattern,
1729
})
1830

19-
local latexCaptionPattern = "(\\caption{)(.-)(}\n)"
31+
local latexCaptionPattern = "(\\caption{)(.-)(}[^\n]*\n)"
2032

2133
function tableCaptions()
2234

@@ -206,7 +218,7 @@ function applyLatexTableCaption(latex, tblCaption, tblLabel, tablePattern)
206218
-- insert caption if there is none
207219
local beginCaption, caption = latex:match(latexCaptionPattern)
208220
if not beginCaption then
209-
latex = latex:gsub(tablePattern, "%1" .. "\n\\caption{ }\n" .. "%2%3", 1)
221+
latex = latex:gsub(tablePattern, "%1" .. "\n\\caption{ }\\tabularnewline\n" .. "%2%3", 1)
210222
end
211223
-- apply table caption and label
212224
local beginCaption, captionText, endCaption = latex:match(latexCaptionPattern)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: "knitr-tables-latex"
3+
format:
4+
pdf:
5+
keep-tex: true
6+
keep-md: true
7+
knitr:
8+
opts_chunk:
9+
echo: false
10+
---
11+
12+
```{r}
13+
#| label: tbl-1
14+
#| tbl-cap: "Markdown table"
15+
knitr::kable(head(iris))
16+
```
17+
18+
```{r}
19+
#| label: tbl-2
20+
#| tbl-cap: "Tabular"
21+
knitr::kable(head(iris), format = "latex")
22+
```
23+
24+
```{r}
25+
#| label: tbl-3
26+
#| tbl-cap: "Longtable"
27+
knitr::kable(
28+
head(mtcars),
29+
format = "latex",
30+
longtable = TRUE
31+
)
32+
```
33+
34+
```{r}
35+
#| label: tbl-4
36+
#| tbl-cap: "Two tables placed side by side."
37+
d1 <- head(cars, 3)
38+
d2 <- head(mtcars[, 1:3], 5)
39+
knitr::kable(
40+
list(d1, d2),
41+
valign = 'c'
42+
)
43+
```
44+
45+
46+
Markdown source for @tbl-1
47+
48+
LaTeX source with tabulat for @tbl-2
49+
50+
LaTeX source with longtable for @tbl-3
51+
52+
LaTeX source with table and tabular for @tbl-4

tests/smoke/crossref/tables.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
*/
77

88
import { ensureFileRegexMatches, ensureHtmlElements } from "../../verify.ts";
9-
import { testRender } from "../render/render.ts";
9+
import { testRender, renderVerifyLatexOutput } from "../render/render.ts";
1010
import { crossref } from "./utils.ts";
11+
import { docs } from "../../utils.ts";
12+
13+
/* HTML */
1114

1215
const tablesQmd = crossref("tables.qmd", "html");
1316
testRender(tablesQmd.input, "html", false, [
@@ -44,3 +47,14 @@ testRender(knitrTablesQmd.input, "html", false, [
4447
/\?@tbl-/,
4548
]),
4649
]);
50+
51+
/* LaTeX */
52+
53+
/* caption is inserted in the right place in table environment*/
54+
renderVerifyLatexOutput(docs("crossrefs/knitr-tables-latex.qmd"), [
55+
/\\begin{longtable}\[.*\]{.*}.*\n\\caption{\\label{tbl-1}.*}\\tabularnewline/,
56+
/\\begin{table}\n\\caption{\\label{tbl-2}.*}.*\n+\\centering\n\\begin{tabular}{.*}/,
57+
/\\begin{longtable}{.*}.*\n\\caption{\\label{tbl-3}.*}\\tabularnewline/,
58+
/\\begin{table}\n\\caption{\\label{tbl-4}.*}.*\n+\\centering\n\\begin{tabular}\[c\]{.*}/,
59+
/\\begin{table}\n\\caption{\\label{tbl-4}.*}.*\n+\\centering\n\\begin{tabular}\[c\]{.*}/,
60+
]);

0 commit comments

Comments
 (0)