Skip to content

Commit 5203ffc

Browse files
committed
normalize all table widths
1 parent 71ba03a commit 5203ffc

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

filter/tabularx.lua

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@ function Length(element)
99
return n
1010
end
1111

12+
function NormalizeColumns(colspecs)
13+
local total_width = 0
14+
for i, colspec in ipairs(colspecs) do
15+
total_width = total_width + (colspec[2] or 1.0)
16+
end
17+
for i, colspec in ipairs(colspecs) do
18+
colspecs[i][2] = (colspec[2] or 1.0) / total_width
19+
end
20+
for i , colspec in ipairs(colspecs) do
21+
print(string.format("%d: %s (%f)", i, colspec[1], colspec[2]))
22+
end
23+
return colspecs
24+
end
25+
1226
-- The adjustment value for any column width to account for column separators.
1327
local ColumnAdjustmentValue = '-2\\tabcolsep-\\arrayrulewidth'
1428

15-
function ColumnWidth(colspec, numcols)
16-
local width = 1.0/numcols
17-
if colspec[2] then
18-
width = colspec[2]
19-
end
20-
return string.format('%f\\linewidth', width)
29+
function ColumnWidth(colspec)
30+
return string.format('%f\\linewidth', colspec[2])
2131
end
2232

2333
-- This function converts a Pandoc ColSpec object into a colspec for the xltabular environment.
@@ -30,7 +40,7 @@ function TabularColspec(colspec, plain, numcols)
3040
['AlignRight'] = '>{\\RaggedLeft}',
3141
}
3242

33-
local result = string.format('%sp{%s%s}', column_pre[colspec[1]], ColumnWidth(colspec, numcols), ColumnAdjustmentValue)
43+
local result = string.format('%sp{%s%s}', column_pre[colspec[1]], ColumnWidth(colspec), ColumnAdjustmentValue)
3444
if not plain then
3545
result = '|' .. result
3646
end
@@ -145,9 +155,9 @@ function TabularRows(rows, header, no_first_hline, plain, colspecs)
145155
if cell.col_span > 1 then
146156
-- Get the total width of all the columns we're spanning.
147157
-- This allows us to place block elements inside multicolumn cells.
148-
local total_column_width = ColumnWidth(colspecs[j], Length(colspecs))
158+
local total_column_width = ColumnWidth(colspecs[j])
149159
for z = j+1,j+cell.col_span-1 do
150-
total_column_width = total_column_width .. '+' .. ColumnWidth(colspecs[z], Length(colspecs))
160+
total_column_width = total_column_width .. '+' .. ColumnWidth(colspecs[z])
151161
end
152162
total_column_width = total_column_width .. ColumnAdjustmentValue
153163
cell_code = string.format('\\multicolumn{%d}{%sp{%s}%s}{%s}', cell.col_span, left_line, total_column_width, right_line, cell_code)
@@ -186,6 +196,7 @@ end
186196
-- which gives us the option to draw the full grid of the table.
187197
function Table(tbl)
188198
if FORMAT =='latex' then
199+
tbl.colspecs = NormalizeColumns(tbl.colspecs)
189200
local latex_code = ''
190201

191202
local plain = false

0 commit comments

Comments
 (0)