Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/resources/filters/quarto-pre/table-captions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
52 changes: 52 additions & 0 deletions tests/docs/crossrefs/knitr-tables-latex.qmd
Original file line number Diff line number Diff line change
@@ -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
16 changes: 15 additions & 1 deletion tests/smoke/crossref/tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down Expand Up @@ -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\]{.*}/,
]);