Skip to content

Commit

Permalink
handle longtable in new crossref system
Browse files Browse the repository at this point in the history
  • Loading branch information
cscheid committed Nov 16, 2023
1 parent 5df7906 commit cd94c58
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 21 deletions.
33 changes: 33 additions & 0 deletions src/resources/filters/customnodes/floatreftarget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,39 @@ end, function(float)
arg = pandoc.Span(quarto.utils.as_inlines(latex_caption or {}) or {}) -- unnecessary to do the "or {}" bit but the Lua analyzer doesn't know that
})

-- special case for singleton longtable floats
if float_type == "tbl" then
local raw
_quarto.ast.walk(float.content, {
RawBlock = function(el)
if _quarto.format.isRawLatex(el) and el.text:match(_quarto.patterns.latexLongtablePattern) then
raw = el
end
end
})
if raw then
local longtable_content = raw.text:gsub(_quarto.patterns.latexLongtablePattern, "%2", 1)
-- split the content into params and actual content
-- params are everything in the first line of longtable_content
-- actual content is everything else
local params, content = longtable_content:match("^(.-)\n(.*)$")
local result = pandoc.Blocks({latex_caption, pandoc.RawInline("latex", "\\\\")})
local cap_loc = cap_location(float)
-- if cap_loc is top, insert content on bottom
if cap_loc == "top" then
result:insert(pandoc.RawBlock("latex", content))
else
result:insert(1, pandoc.RawBlock("latex", content))
end

result:insert(1, pandoc.RawBlock("latex", "\\begin{longtable}" .. params))
result:insert(pandoc.RawBlock("latex", "\\end{longtable}"))
return result
end
end



if float.parent_id then
-- need to fixup subtables because nested longtables appear to give latex fits
local vAlign = validatedVAlign(float.attributes[kLayoutVAlign])
Expand Down
7 changes: 2 additions & 5 deletions src/resources/filters/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,7 @@ local quarto_pre_filters = {
{ name = "pre-table-captions",
filter = table_captions(),
flags = { "has_table_captions" } },

{ name = "pre-longtable-no-caption-fixup",
filter = longtable_no_caption_fixup(),
flags = { "has_longtable_no_caption_fixup" } },


{ name = "pre-code-annotations",
filter = code_annotations(),
flags = { "has_code_annotations" } },
Expand Down Expand Up @@ -346,6 +342,7 @@ local quarto_post_filters = {
{ name = "layout-meta-inject-latex-packages", filter = layout_meta_inject_latex_packages() },

-- format fixups post rendering
{ name = "post-render-latex-fixups", filter = render_latex_fixups() },
{ name = "post-render-html-fixups", filter = render_html_fixups() },
{ name = "post-render-ipynb-fixups", filter = render_ipynb_fixups() },
{ name = "post-render-typst-fixups", filter = render_typst_fixups() },
Expand Down
22 changes: 21 additions & 1 deletion src/resources/filters/quarto-post/latex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,24 @@ function render_latex()
return pandoc.Div(calloutContents)
end
}
end
end


function render_latex_fixups()
if not _quarto.format.isLatexOutput() then
return {}
end

return {
RawBlock = function(raw)
if _quarto.format.isRawLatex(raw) then
if (raw.text:match(_quarto.patterns.latexLongtablePattern) and
not raw.text:match(_quarto.patterns.latexCaptionPattern)) then
raw.text = raw.text:gsub(
_quarto.patterns.latexLongtablePattern, "\\begin{longtable*}%2\\end{longtable*}", 1)
return raw
end
end
end
}
end
15 changes: 0 additions & 15 deletions src/resources/filters/quarto-pre/table-captions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@ local patterns = require("modules/patterns")
kTblCap = "tbl-cap"
kTblSubCap = "tbl-subcap"

function longtable_no_caption_fixup()
return {
RawBlock = function(raw)
if _quarto.format.isRawLatex(raw) then
if (raw.text:match(_quarto.patterns.latexLongtablePattern) and
not raw.text:match(_quarto.patterns.latexCaptionPattern)) then
raw.text = raw.text:gsub(
_quarto.patterns.latexLongtablePattern, "\\begin{longtable*}%2\\end{longtable*}", 1)
return raw
end
end
end
}
end

function table_captions()
return {
Div = function(el)
Expand Down
29 changes: 29 additions & 0 deletions tests/docs/smoke-all/2023/11/16/7604.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: "7604"
format: pdf
---

```{r}
library(kableExtra)
```

```{r}
#| label: tbl-sad-broken
#| tbl-cap: "This table is cross-referenceable but no longer breaks across pages because it's inside `\\centering{}`"
rbind(mtcars, mtcars) |>
kbl(longtable = TRUE, booktabs = TRUE) |>
kable_styling()
```

::: {#tbl-also-sad}
```{r}
rbind(mtcars, mtcars) |>
kbl(longtable = TRUE, booktabs = TRUE) |>
kable_styling()
```

This table is also cross-referenceable but also doesn't break across pages because of the same `\centering{}` problem
:::


see @tbl-sad-broken and @tbl-also-sad.

0 comments on commit cd94c58

Please sign in to comment.