From 30f6d06b7ab580037abbb254798be3e00829626d Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Thu, 19 May 2022 11:51:13 +0200 Subject: [PATCH 1/7] Match table environment with tabular options also so that caption is placed correctly --- src/resources/filters/quarto-pre/table-captions.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/resources/filters/quarto-pre/table-captions.lua b/src/resources/filters/quarto-pre/table-captions.lua index dbbee6f5ceb..7ebb626d08b 100644 --- a/src/resources/filters/quarto-pre/table-captions.lua +++ b/src/resources/filters/quarto-pre/table-captions.lua @@ -6,13 +6,25 @@ 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, }) From 6bf16b883978a17d59ed8feeabfc160555267acc Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Thu, 19 May 2022 12:15:38 +0200 Subject: [PATCH 2/7] Newline needs to be added after caption to avoid noalign issue --- src/resources/filters/quarto-pre/table-captions.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resources/filters/quarto-pre/table-captions.lua b/src/resources/filters/quarto-pre/table-captions.lua index 7ebb626d08b..7e137d8fafc 100644 --- a/src/resources/filters/quarto-pre/table-captions.lua +++ b/src/resources/filters/quarto-pre/table-captions.lua @@ -28,7 +28,7 @@ local latexTablePatterns = pandoc.List({ latexTabularPattern, }) -local latexCaptionPattern = "(\\caption{)(.-)(}\n)" +local latexCaptionPattern = "(\\caption{)(.-)(}\\?\\?\n)" function tableCaptions() @@ -218,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{ }\\\\\n" .. "%2%3", 1) end -- apply table caption and label local beginCaption, captionText, endCaption = latex:match(latexCaptionPattern) From 76b38ab9ab938d51d96d339226ea3138b12c6014 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Thu, 19 May 2022 12:26:10 +0200 Subject: [PATCH 3/7] Use \tabularnewline instead of just \\ --- src/resources/filters/quarto-pre/table-captions.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resources/filters/quarto-pre/table-captions.lua b/src/resources/filters/quarto-pre/table-captions.lua index 7e137d8fafc..1196d7de029 100644 --- a/src/resources/filters/quarto-pre/table-captions.lua +++ b/src/resources/filters/quarto-pre/table-captions.lua @@ -28,7 +28,7 @@ local latexTablePatterns = pandoc.List({ latexTabularPattern, }) -local latexCaptionPattern = "(\\caption{)(.-)(}\\?\\?\n)" +local latexCaptionPattern = "(\\caption{)(.-)(}[^\n]*\n)" function tableCaptions() @@ -218,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) From addc5758f7a2278c3f4fb1af19580b37ff5abc0f Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 20 May 2022 22:55:20 +0200 Subject: [PATCH 4/7] Add working tests for caption insertion for raw latex tables --- .../docs/latex-output/latex-tables-knitr.qmd | 52 +++++++++++++++++++ .../smoke/render/render-latex-output.test.ts | 9 ++++ 2 files changed, 61 insertions(+) create mode 100644 tests/docs/latex-output/latex-tables-knitr.qmd diff --git a/tests/docs/latex-output/latex-tables-knitr.qmd b/tests/docs/latex-output/latex-tables-knitr.qmd new file mode 100644 index 00000000000..d23593212ad --- /dev/null +++ b/tests/docs/latex-output/latex-tables-knitr.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/render/render-latex-output.test.ts b/tests/smoke/render/render-latex-output.test.ts index d75ec6d59b6..b8dfca884e7 100644 --- a/tests/smoke/render/render-latex-output.test.ts +++ b/tests/smoke/render/render-latex-output.test.ts @@ -11,8 +11,17 @@ import { renderVerifyLatexOutput } from "./render.ts"; renderVerifyLatexOutput(docs("latex-output/captionless-margin-image.qmd"), [ /\\begin{marginfigure}/, ]); + renderVerifyLatexOutput(docs("latex-output/figure-div.qmd"), [ /{\\centering/, /\\caption{\\label{fig-foo}This is the figure}/, /See Figure~\\ref{fig-foo} for more\./, ]); + +renderVerifyLatexOutput(docs("latex-output/latex-tables-knitr.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\]{.*}/, +]); + From 86b1a565e4cbc03459dfb570501ce8b33c489739 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 20 May 2022 23:13:31 +0200 Subject: [PATCH 5/7] Move the tests with other tests about tables crossref --- .../knitr-tables-latex.qmd} | 0 tests/smoke/crossref/tables.test.ts | 15 ++++++++++++++- tests/smoke/render/render-latex-output.test.ts | 9 --------- 3 files changed, 14 insertions(+), 10 deletions(-) rename tests/docs/{latex-output/latex-tables-knitr.qmd => crossrefs/knitr-tables-latex.qmd} (100%) diff --git a/tests/docs/latex-output/latex-tables-knitr.qmd b/tests/docs/crossrefs/knitr-tables-latex.qmd similarity index 100% rename from tests/docs/latex-output/latex-tables-knitr.qmd rename to tests/docs/crossrefs/knitr-tables-latex.qmd diff --git a/tests/smoke/crossref/tables.test.ts b/tests/smoke/crossref/tables.test.ts index ca247cd7b9e..9c13f005d35 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,13 @@ testRender(knitrTablesQmd.input, "html", false, [ /\?@tbl-/, ]), ]); + +/* PDF */ + +/* 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\]{.*}/, +]); diff --git a/tests/smoke/render/render-latex-output.test.ts b/tests/smoke/render/render-latex-output.test.ts index b8dfca884e7..d75ec6d59b6 100644 --- a/tests/smoke/render/render-latex-output.test.ts +++ b/tests/smoke/render/render-latex-output.test.ts @@ -11,17 +11,8 @@ import { renderVerifyLatexOutput } from "./render.ts"; renderVerifyLatexOutput(docs("latex-output/captionless-margin-image.qmd"), [ /\\begin{marginfigure}/, ]); - renderVerifyLatexOutput(docs("latex-output/figure-div.qmd"), [ /{\\centering/, /\\caption{\\label{fig-foo}This is the figure}/, /See Figure~\\ref{fig-foo} for more\./, ]); - -renderVerifyLatexOutput(docs("latex-output/latex-tables-knitr.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\]{.*}/, -]); - From a4b016416196ebcd6d73ab51bf351d4bbe6c2823 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 20 May 2022 23:50:26 +0200 Subject: [PATCH 6/7] Latex's table environment column options can contains brackets Change regex to support e.g `\begin{longtable}{|p{.8\textwidth}|}` --- src/resources/filters/quarto-pre/table-captions.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/resources/filters/quarto-pre/table-captions.lua b/src/resources/filters/quarto-pre/table-captions.lua index 1196d7de029..6a1f8246220 100644 --- a/src/resources/filters/quarto-pre/table-captions.lua +++ b/src/resources/filters/quarto-pre/table-captions.lua @@ -6,13 +6,13 @@ kTblSubCap = "tbl-subcap" local latexTableWithOptionsPattern = "(\\begin{table}%[%w+%])(.*)(\\end{table})" local latexTablePattern = "(\\begin{table})(.*)(\\end{table})" -local latexLongtablePatternwWithPosAndAlign = "(\\begin{longtable}%[[^%]]+%]{[^}]+})(.*)(\\end{longtable})" +local latexLongtablePatternwWithPosAndAlign = "(\\begin{longtable}%[[^%]]+%]{.*})(.*)(\\end{longtable})" local latexLongtablePatternWithPos = "(\\begin{longtable}%[[^%]]+%])(.*)(\\end{longtable})" -local latexLongtablePatternWithAlign = "(\\begin{longtable}{[^}]+})(.*)(\\end{longtable})" +local latexLongtablePatternWithAlign = "(\\begin{longtable}{.*})(.*)(\\end{longtable})" local latexLongtablePattern = "(\\begin{longtable})(.*)(\\end{longtable})" -local latexTabularPatternWithPosAndAlign = "(\\begin{tabular}%[[^%]]+%]{[^}]+})(.*)(\\end{tabular})" +local latexTabularPatternWithPosAndAlign = "(\\begin{tabular}%[[^%]]+%]{.*})(.*)(\\end{tabular})" local latexTabularPatternWithPos = "(\\begin{tabular}%[[^%]]+%])(.*)(\\end{tabular})" -local latexTabularPatternWithAlign = "(\\begin{tabular}{[^}]+})(.*)(\\end{tabular})" +local latexTabularPatternWithAlign = "(\\begin{tabular}{.*})(.*)(\\end{tabular})" local latexTabularPattern = "(\\begin{tabular})(.*)(\\end{tabular})" local latexTablePatterns = pandoc.List({ From 4ed012d66f139c91feb1957738f22827f88ab1f2 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Sat, 21 May 2022 00:06:00 +0200 Subject: [PATCH 7/7] Correct format in comment --- tests/smoke/crossref/tables.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/smoke/crossref/tables.test.ts b/tests/smoke/crossref/tables.test.ts index 9c13f005d35..635dd8a0fdd 100644 --- a/tests/smoke/crossref/tables.test.ts +++ b/tests/smoke/crossref/tables.test.ts @@ -48,7 +48,7 @@ testRender(knitrTablesQmd.input, "html", false, [ ]), ]); -/* PDF */ +/* LaTeX */ /* caption is inserted in the right place in table environment*/ renderVerifyLatexOutput(docs("crossrefs/knitr-tables-latex.qmd"), [ @@ -56,4 +56,5 @@ renderVerifyLatexOutput(docs("crossrefs/knitr-tables-latex.qmd"), [ /\\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\]{.*}/, ]);