-- docs/book/pandoc/table-widths.lua -- -- pandoc's gfm reader leaves every column width unset, so the LaTeX writer -- emits `l` columns and a table with long cells runs off the page. For any -- table wider than WIDE characters, give each column a width proportional -- to its longest cell (clamped to [MIN_COL, MAX_COL] characters); the -- writer then emits wrapping p{} columns that together fill \linewidth. local WIDE = 72 local MIN_COL = 6 local MAX_COL = 60 local stringify = pandoc.utils.stringify local function measure(rows, widths) for _, row in ipairs(rows) do local col = 1 for _, cell in ipairs(row.cells) do local span = cell.col_span or 1 local len = utf8.len(stringify(cell.contents)) or #stringify(cell.contents) local per = len / span for c = col, col + span - 1 do if per > (widths[c] or 0) then widths[c] = per end end col = col + span end end end function Table(tbl) local n = #tbl.colspecs local widths = {} for i = 1, n do widths[i] = 0 end measure(tbl.head.rows, widths) for _, body in ipairs(tbl.bodies) do measure(body.head, widths) measure(body.body, widths) end measure(tbl.foot.rows, widths) local total = 0 for i = 1, n do total = total + widths[i] end if total <= WIDE then return nil end local sum = 0 for i = 1, n do widths[i] = math.max(MIN_COL, math.min(MAX_COL, widths[i])) sum = sum + widths[i] end for i = 1, n do tbl.colspecs[i] = { tbl.colspecs[i][1], widths[i] / sum } end return tbl end