Right now, this is the best version of my autotable function:
#let autotable(
caption,
file: none,
precision: 1,
fsize: 9pt,
rot: false,
label-tag: none,
load_txt: false,
max_rows: 30,
) = context {
show table.cell.where(y: 0): strong
show table.cell: set text(size: fsize)
// show table: format-table(..formats)
set table(
fill: (_, y) => if calc.odd(y) { rgb("#a2d0e043") },
stroke: none, //0.6pt + black,
)
let n = counter(figure.where(kind: table)).get().first() + 1
let fname = if file == none {
"table_" + str(n) + ".tsv"
} else {
file
}
let label-name = if label-tag == none {
fname.split("/").last().replace(".tsv", "").replace(".csv", "")
} else {
label-tag
}
// set-round(precision: precision)
// set-num(math: false)
let data = csv(fname, delimiter: "\t")
let ncols = data.at(0).len()
let nrows = data.len() - 1 // assume first row is a header
// let formats = (auto,) * clen
let angle = if rot { -90deg } else { 0deg }
let extra = ""
if load_txt {
let ext = if fname.ends-with(".tsv") { ".tsv" } else { ".csv" }
extra = load-side-text(fname, ext)
}
let cap = if type(caption) == function { caption(nrows, ncols) } else { caption }
if nrows <= max_rows {
[#text(size: text.size / 2, fill: white)[==== Table #n]
#figure(
rotate(
angle,
reflow: true,
table(
table.hline(y: 0),
table.hline(y: 1),
table.vline(x: 1, start: 1),
columns: ncols,
align: (left,) + (right,) * (ncols - 1),
..data.flatten(),
),
),
caption: [#cap #extra],
)#label(label-name)]
} else {
counter(figure.where(kind: table)).update(n)
let header = data.at(0)
let rows = data.slice(1)
let chunks = calc.ceil(nrows / max_rows)
for i in range(chunks) {
let start = i * max_rows
let end = calc.min((i + 1) * max_rows, nrows)
let chunk_data = (header,) + rows.slice(start, end)
[#text(size: text.size / 2, fill: white)[==== Table #n]
#figure(
rotate(
angle,
reflow: true,
table(
table.hline(y: 0),
table.hline(y: 1),
table.vline(x: 1, start: 1),
columns: ncols,
align: (left,) + (right,) * (ncols - 1),
..chunk_data.flatten(),
),
),
caption: [#cap #extra (Part #(i + 1) of #chunks)],
numbering: none,
)#label(label-name + "-" + str(i + 1))]
if i < chunks - 1 {
pagebreak()
}
}
}
}
So, for example, if I have 40 TSV files named table_1.tsv…table_40.tsv, the function will handle them, just by doing:
#autotable([_Caption for table 1._])
#autotable([_Caption for table 2._])
...
But then I modified the function (as shown above) to split over pages if the table has more then max_rows (default 30).
It will work with the only niggle: say table_35.tsv has 49 rows. In TOC I will see:
...
Table 34.......76
Table 35.......77
Table 35.......78
Table 36.......79
...
Links to tables will work fine. For example, if I click on the second Table 35 it goes to page 78 and I can see its caption:
Table 35: Blablabla. (Part 2 of 2)
If I could change anything, I would like to see in Table 35.1 and Table 35.2, both in TOC and captions.
I tried to tweak with numbering but without success.