I am new to this, so I’m not sure if this is possible, but I would like to create a variable that defines the formatting of a table, and then I would like to be able to choose which format to use rather than pasting it ahead of each table. As an example, I have two table formats:
A: #set table( fill: (x, y) => if (calc.even(y+1)) { rgb(0, 0, 255, 45) } )
B: #set table( fill: (x, y) => if (calc.odd(y)) { rgb(0, 0, 0, 45) }, stroke: (x,y) => ( top: if (y > 0) {rgb(0,0,0)}, right: if (x == 0) {rgb(0,0,0)} ) )
And then I could call A somehow and get that formatting, then for the next table, I would like to use B and call that, saving me typing it out.
You can create a function for each table style you want to use, then call that function instead of the default table()
. One thing this code uses that is not obvious (or wasn’t to me at the beginning) was the use of .with()
. For instance, table.with(...)
will return a new function that calls table()
with the parameters you defined in .with(...)
. I’m not sure how good of an explanation this is.
#let tableA = table.with(
fill: (x, y) => if (calc.even(y+1)) {rgb(0, 0, 255, 45)}
)
#let tableB = table.with(
fill: (x, y) => if (calc.odd(y)) {rgb(0, 0, 0, 45)},
stroke: (x,y) => (
top: if (y > 0) {rgb(0,0,0)},
right: if (x == 0) {rgb(0,0,0)}
)
)
TableA
#tableA(
columns: 2,
..range(6).map(value => str(value))
)
TableB
#tableB(
columns: 2,
..range(6).map(value => str(value))
)
Also, I see that you used single back-ticks (`
) around your example code. This does change the font of the text to better show that it is code, but does not apply any further formatting. In your case because the text was long enough it spilled into a new line making it appear quite messy.
A better way would be to use triple back-ticks (```
) as mentioned in this how-to:
Awesome, thank you for that. The explanation you gave makes sense to me.
I did type 3 back ticks as per the how-to, but it seems I did something wrong.