Skip to content

Commit

Permalink
try to keep headers and footers together
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisfenner committed Feb 8, 2024
1 parent ed490d0 commit 17e18d6
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions filter/tabularx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function TabularRows(rows, header, no_first_hline, plain, colspecs)
local latex_code = ''
-- Keep a 2d array of bools for which cells we know we need to skip.
local skips = {}
local rows_with_rowspans = {}

-- For each row in the list of rows,
for i, row in ipairs(rows) do
Expand Down Expand Up @@ -138,6 +139,9 @@ function TabularRows(rows, header, no_first_hline, plain, colspecs)
-- the cells that get covered up empty.
if cell.row_span > 1 or cell.col_span > 1 then
if cell.row_span > 1 then
for skipi=i,i+cell.row_span-1 do
rows_with_rowspans[skipi] = true
end
cell_code = string.format('\\multirow{%d}{=}{%s}', cell.row_span, cell_code)
end
local left_line = ''
Expand Down Expand Up @@ -180,8 +184,17 @@ function TabularRows(rows, header, no_first_hline, plain, colspecs)
break
end
end

local linebreak = '\\\\'
if header or i == height or rows_with_rowspans[i] then
-- Use the \\* break which keeps rows together even when there's a page break.
-- Use this on header/footer lines, the last row in the body, and on
-- any rows where there was a rowspan.
linebreak = linebreak .. '*'
end

-- The entire row is all the cells joined by '&' with a '\\' at the end.
latex_code = latex_code .. clines_code .. ' ' .. table.concat(row_code, ' & ') .. ' \\\\\n'
latex_code = latex_code .. clines_code .. ' ' .. table.concat(row_code, ' & ') .. string.format(' %s\n', linebreak)
end

latex_code = latex_code .. '\n'
Expand Down Expand Up @@ -278,10 +291,12 @@ function Table(tbl)
latex_code = latex_code .. '\\endfirsthead\n'

--
-- Create the not-first header. This is the same as the first header, except there's no caption.
-- Create the not-first header. This is the same as the first header, except there's no caption
-- and there's a continuation note instead.
--

-- Write out all the header rows.
latex_code = latex_code .. string.format('\\multicolumn{%s}{c}\n{\\Centering\\textit{\\textcolor{gray}{\\Centering %s (continued from previous page)}}}\\\\\n', Length(tbl.colspecs), escaped_caption)
if Length(tbl.head.rows) > 0 then
latex_code = latex_code .. TabularRows(tbl.head.rows, true, false, plain, tbl.colspecs)
end
Expand All @@ -299,8 +314,9 @@ function Table(tbl)
if not plain then
latex_code = latex_code .. '\\hline\n'
end
latex_code = latex_code .. '\\endfoot\n'
end
latex_code = latex_code .. string.format('\\multicolumn{%s}{c}\n{\\Centering\\textit{\\textcolor{gray}{\\Centering(continued on next page)}}}\\\\\n', Length(tbl.colspecs))
latex_code = latex_code .. '\\endfoot\n'

-- Write out all the footer rows again for the last footer.
latex_code = latex_code .. TabularRows(tbl.foot.rows, true, false, plain, tbl.colspecs)
Expand Down

0 comments on commit 17e18d6

Please sign in to comment.