I have the following (specific) problem which has its cause in a more general problem: I would like to create a grid, where one or more rows are only printed depending on some condition.
E.g. in the following example we have a grid with four rows. The third row should only be printed if some condition is true. If the condition is false, a grid with only three rows should be printed:
#grid(
columns: (auto, auto),
row-gutter: 1em,
column-gutter: 1em,
align: (right, left),
"hallo1", "world1",
"hallo2", "world2",
if (false) {
"hallo3", "world2"
},
"hallo4", "world4",
)
For the sake of simplicity I’ve just used false
as a condition within the if-statement.
Unfortunately the result is:
I.e. Typst produces a grid with four rows, where the content of the third row is sort of an empty element (presumably with height 0). But as row-gutter
is 1em
, we have a vertical gap of that size.
There is a work-around in this case: I can set row-gutter
to 0em
and use a box
in each row to give it the desired vertical space as follows:
#grid(
columns: (auto, auto),
row-gutter: 0em,
column-gutter: 1em,
align: (right, left),
box("hallo1", height: 16pt), "world1",
box("hallo2", height: 16pt), "world2",
if (false) {
box("hallo3", height: 16pt)
},
if (false) {
text("world3")
},
box("hallo4", height: 16pt), "world4",
)
This leads to the desired output, but the code necessary to produce it is not ideal (to put it mildly):
In my opinion, the basic problem is that the if-statement produces some sort of empty element, if the condition evaluates to false
. In my opinion it should produce “nothing”, i.e. it should render a grid which consists of only three rows.
Is there any way to achieve this in Typst or is this a known behaviour? … and if so, why is Typst implemented this way?