Skip to content

Commit

Permalink
regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
cscheid committed Oct 16, 2023
1 parent dc2a574 commit cfebb28
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 10 deletions.
21 changes: 13 additions & 8 deletions src/resources/filters/customnodes/floatreftarget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,13 @@ end, function(float)
end

local latex_caption
if float.caption_long and type(float.caption_long) ~= "table" then
latex_caption = quarto.utils.as_inlines(float.caption_long)
else
latex_caption = float.caption_long
end

if float.caption_long and #float.caption_long.content == 0 then
if #latex_caption == 0 then
local caption_setup = quarto.LatexInlineCommand({
name = "captionsetup",
arg = "labelsep=none"
Expand All @@ -260,11 +265,11 @@ end, function(float)
name = "label",
arg = pandoc.Str(float.identifier)
})
float.caption_long.content:insert(1, label_cmd)
latex_caption:insert(1, label_cmd)
latex_caption = quarto.LatexInlineCommand({
name = caption_cmd_name,
opt_arg = fig_scap,
arg = pandoc.Span(quarto.utils.as_inlines(float.caption_long or {}) or {}) -- unnecessary to do the "or {}" bit but the Lua analyzer doesn't know that
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
})

if float.parent_id then
Expand Down Expand Up @@ -315,11 +320,11 @@ end, function(float)

if latex_caption then
if caption_cmd_name == kSideCaptionEnv then
if #figure_content > 1 then
figure_content:insert(2, latex_caption) -- FIXME why is this 2 and not 1?
else
figure_content:insert(latex_caption)
end
if #figure_content > 1 then
figure_content:insert(2, latex_caption) -- FIXME why is this 2 and not 1?
else
figure_content:insert(latex_caption)
end
elseif capLoc == "top" then
figure_content:insert(1, latex_caption)
else
Expand Down
9 changes: 8 additions & 1 deletion src/resources/filters/modules/patterns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ local engine_escape = "{({+([^}]+)}+)}"
local shortcode = "{{+<[^>]+>}+}"
local latex_label = "(\\label{([^}]+)})"
local latex_caption = "(\\caption{([^}]+)})"
local attr_identifier = "({#([^}]+)})"

-- this will catch two longtables in a single rawblock
-- but I'm willing to call that a bug in the source document
local latex_long_table = "(\\begin{longtable}.*\\end{longtable})"
local latex_tabular = "(\\begin{tabular}.*\\end{tabular})"

-- note the capture pattern here is different because we need to capture the
-- body of the float environment as well as the environment itself
local latex_table = "(\\begin{table})(.*)(\\end{table})"

return {
attr_identifier = attr_identifier,
engine_escape = engine_escape,
html_gt_table = html_gt_table,
html_paged_table = html_paged_table,
Expand All @@ -35,5 +41,6 @@ return {
latex_label = latex_label,
latex_caption = latex_caption,
latex_long_table = latex_long_table,
latex_tabular = latex_tabular
latex_tabular = latex_tabular,
latex_table = latex_table,
}
70 changes: 70 additions & 0 deletions src/resources/filters/quarto-pre/parsefiguredivs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,76 @@ function parse_reftargets()
content = { content },
caption_long = caption_inlines,
}), false
end,

RawBlock = function(raw)
if not (_quarto.format.isLatexOutput() and
_quarto.format.isRawLatex(raw)) then
return nil
end

-- first we check if all of the expected bits are present

-- check for {#...} or \label{...}
if raw.text:find(patterns.latex_label) == nil and
raw.text:find(patterns.attr_identifier) == nil then
return nil
end

-- check for \caption{...}
if raw.text:find(patterns.latex_caption) == nil then
return nil
end

-- check for tabular or longtable
if raw.text:find(patterns.latex_long_table) == nil and
raw.text:find(patterns.latex_tabular) == nil then
return nil
end

-- if we're here, then we're going to parse this as a FloatRefTarget
-- and we need to remove the label and caption from the raw block
local identifier = ""
local b, e, match1, label_identifier = raw.text:find(patterns.latex_label)
if b ~= nil then
raw.text = raw.text:sub(1, b - 1) .. raw.text:sub(e + 1)
identifier = label_identifier
else
local b, e, match2, attr_identifier = raw.text:find(patterns.attr_identifier)
if b ~= nil then
raw.text = raw.text:sub(1, b - 1) .. raw.text:sub(e + 1)
identifier = attr_identifier
else
internal_error()
return nil
end
end

local caption
local b, e, match3, caption_content = raw.text:find(patterns.latex_caption)
if b ~= nil then
raw.text = raw.text:sub(1, b - 1) .. raw.text:sub(e + 1)
caption = pandoc.RawBlock("latex", caption_content)
else
internal_error()
return nil
end

-- finally, if the user passed a \\begin{table} float environment
-- we just remove it because we'll re-emit later ourselves

local b, e, begin_table, table_body, end_table = raw.text:find(patterns.latex_table)
if b ~= nil then
raw.text = table_body
end

return quarto.FloatRefTarget({
attr = pandoc.Attr(identifier, {}, {}),
type = "Table",
content = { raw },
caption_long = quarto.utils.as_blocks(caption)
}), false
end

}
end
4 changes: 3 additions & 1 deletion src/resources/filters/quarto-pre/table-classes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ function table_classes()
return nil
end

if float.caption_long == nil or (#float.caption_long.content and #float.caption_long.content < 1) then
if (float.caption_long == nil or
float.caption_long.content == nil or
#float.caption_long.content < 1) then
return nil
end

Expand Down
45 changes: 45 additions & 0 deletions tests/docs/smoke-all/2023/10/16/7236.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: "Raw TeX cross referencing"
format:
pdf: default
---

See @tbl-tex, @tbl-2, @tbl-3.

```{=latex}
\begin{tabular}{@{}lll@{}}
\label{tbl-tex}
\caption{Some letters with LaTeX}
\toprule
Column 1 & Column 2 & Column 3 \\ \midrule
A & B & C \\
D & E & F \\
G & H & I \\ \bottomrule
\end{tabular}
```

```{=latex}
\begin{tabular}{@{}lll@{}}
\caption{Some letters with LaTeX {#tbl-2}}
\toprule
Column 1 & Column 2 & Column 3 \\ \midrule
A & B & C \\
D & E & F \\
G & H & I \\ \bottomrule
\end{tabular}
```

As best effort, we try to remove `\begin{table}` as well:

```{=latex}
\begin{table}
\begin{tabular}{@{}lll@{}}
\caption{Some letters with LaTeX {#tbl-3}}
\toprule
Column 1 & Column 2 & Column 3 \\ \midrule
A & B & C \\
D & E & F \\
G & H & I \\ \bottomrule
\end{tabular}
\end{table}
```

0 comments on commit cfebb28

Please sign in to comment.