diff --git a/src/resources/filters/quarto-pre/table-captions.lua b/src/resources/filters/quarto-pre/table-captions.lua index dbbee6f5ceb..6a1f8246220 100644 --- a/src/resources/filters/quarto-pre/table-captions.lua +++ b/src/resources/filters/quarto-pre/table-captions.lua @@ -6,17 +6,29 @@ kTblSubCap = "tbl-subcap" local latexTableWithOptionsPattern = "(\\begin{table}%[%w+%])(.*)(\\end{table})" local latexTablePattern = "(\\begin{table})(.*)(\\end{table})" +local latexLongtablePatternwWithPosAndAlign = "(\\begin{longtable}%[[^%]]+%]{.*})(.*)(\\end{longtable})" +local latexLongtablePatternWithPos = "(\\begin{longtable}%[[^%]]+%])(.*)(\\end{longtable})" +local latexLongtablePatternWithAlign = "(\\begin{longtable}{.*})(.*)(\\end{longtable})" local latexLongtablePattern = "(\\begin{longtable})(.*)(\\end{longtable})" +local latexTabularPatternWithPosAndAlign = "(\\begin{tabular}%[[^%]]+%]{.*})(.*)(\\end{tabular})" +local latexTabularPatternWithPos = "(\\begin{tabular}%[[^%]]+%])(.*)(\\end{tabular})" +local latexTabularPatternWithAlign = "(\\begin{tabular}{.*})(.*)(\\end{tabular})" local latexTabularPattern = "(\\begin{tabular})(.*)(\\end{tabular})" local latexTablePatterns = pandoc.List({ latexTableWithOptionsPattern, latexTablePattern, + latexLongtablePatternwWithPosAndAlign, + latexLongtablePatternWithPos, + latexLongtablePatternWithAlign, latexLongtablePattern, + latexTabularPatternWithPosAndAlign, + latexTabularPatternWithPos, + latexTabularPatternWithAlign, latexTabularPattern, }) -local latexCaptionPattern = "(\\caption{)(.-)(}\n)" +local latexCaptionPattern = "(\\caption{)(.-)(}[^\n]*\n)" function tableCaptions() @@ -206,7 +218,7 @@ function applyLatexTableCaption(latex, tblCaption, tblLabel, tablePattern) -- insert caption if there is none local beginCaption, caption = latex:match(latexCaptionPattern) if not beginCaption then - latex = latex:gsub(tablePattern, "%1" .. "\n\\caption{ }\n" .. "%2%3", 1) + latex = latex:gsub(tablePattern, "%1" .. "\n\\caption{ }\\tabularnewline\n" .. "%2%3", 1) end -- apply table caption and label local beginCaption, captionText, endCaption = latex:match(latexCaptionPattern) diff --git a/tests/docs/crossrefs/knitr-tables-latex.qmd b/tests/docs/crossrefs/knitr-tables-latex.qmd new file mode 100644 index 00000000000..d23593212ad --- /dev/null +++ b/tests/docs/crossrefs/knitr-tables-latex.qmd @@ -0,0 +1,52 @@ +--- +title: "knitr-tables-latex" +format: + pdf: + keep-tex: true + keep-md: true +knitr: + opts_chunk: + echo: false +--- + +```{r} +#| label: tbl-1 +#| tbl-cap: "Markdown table" +knitr::kable(head(iris)) +``` + +```{r} +#| label: tbl-2 +#| tbl-cap: "Tabular" +knitr::kable(head(iris), format = "latex") +``` + +```{r} +#| label: tbl-3 +#| tbl-cap: "Longtable" +knitr::kable( + head(mtcars), + format = "latex", + longtable = TRUE +) +``` + +```{r} +#| label: tbl-4 +#| tbl-cap: "Two tables placed side by side." +d1 <- head(cars, 3) +d2 <- head(mtcars[, 1:3], 5) +knitr::kable( + list(d1, d2), + valign = 'c' +) +``` + + +Markdown source for @tbl-1 + +LaTeX source with tabulat for @tbl-2 + +LaTeX source with longtable for @tbl-3 + +LaTeX source with table and tabular for @tbl-4 diff --git a/tests/smoke/crossref/tables.test.ts b/tests/smoke/crossref/tables.test.ts index ca247cd7b9e..635dd8a0fdd 100644 --- a/tests/smoke/crossref/tables.test.ts +++ b/tests/smoke/crossref/tables.test.ts @@ -6,8 +6,11 @@ */ import { ensureFileRegexMatches, ensureHtmlElements } from "../../verify.ts"; -import { testRender } from "../render/render.ts"; +import { testRender, renderVerifyLatexOutput } from "../render/render.ts"; import { crossref } from "./utils.ts"; +import { docs } from "../../utils.ts"; + +/* HTML */ const tablesQmd = crossref("tables.qmd", "html"); testRender(tablesQmd.input, "html", false, [ @@ -44,3 +47,14 @@ testRender(knitrTablesQmd.input, "html", false, [ /\?@tbl-/, ]), ]); + +/* LaTeX */ + +/* caption is inserted in the right place in table environment*/ +renderVerifyLatexOutput(docs("crossrefs/knitr-tables-latex.qmd"), [ + /\\begin{longtable}\[.*\]{.*}.*\n\\caption{\\label{tbl-1}.*}\\tabularnewline/, + /\\begin{table}\n\\caption{\\label{tbl-2}.*}.*\n+\\centering\n\\begin{tabular}{.*}/, + /\\begin{longtable}{.*}.*\n\\caption{\\label{tbl-3}.*}\\tabularnewline/, + /\\begin{table}\n\\caption{\\label{tbl-4}.*}.*\n+\\centering\n\\begin{tabular}\[c\]{.*}/, + /\\begin{table}\n\\caption{\\label{tbl-4}.*}.*\n+\\centering\n\\begin{tabular}\[c\]{.*}/, +]);