You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Similar to how stroke allows converting a dictionary into stroke properties, there should be a margin type that converts a dictionary specifier into a page margin
Use Case
Page margins can be specified in numerous ways:
Inside/outside
Single argument vs several
x/y vs left/right etc.
unspecified => auto
This makes it difficult to consume through a context call due to the wide variety of potential outputs. Each of these specifiers results in a different-format output:
Similar to how a stroke evolved from a dict into its own type, I recommend the same thing for a margin. This way, users can expect to resolve top, bottom, left, and right without calculating it manually by checking the binding and page number, referencing auto against paper defaults, etc.
Real-world example: The "drafting" package would need to take all these into account to provide robust margin notes without user specifications.
Reacted by Leedehai, CGMossa, Andreas Kröpelin, Lukas Juhrich, Tim Fischer, Michael Sasser and bluss
The easiest example is to compare to a stroke. You can pass many forms of stroke into a shape, i.e.
black + 5pt
(paint: red, dash: "dashed")
1em
Each of these is a different type, but construct a stroke with the same set of fields. So having access to a stroke type means the user is not responsible to understand how to handle all these separate formats. They just need to do stroke(input).thickness to get the line thickness. This is why typst authors added the type in 0.10
In the same way, a margin can be constructed from all the types and arguments listed in the feature request. So if I wanted to know the page margin, it is possible the user passed in margin: (outside: 3in). In this case, just to know the left margin, I need to
Find out whether the binding is left/right/auto
Find out whether the text direction is ltr/rtl/auto
Find out whether the language is ltr/rtl by default (!)
Find out whether the page is even or odd
If the margin type exists, I can just call context margin(input).left and it will provide me this value "out-of-the-box"
Reacted by Leedehai, jathan, Andreas Kröpelin, Max Julian, bluss and Bastien Voirin
I'm not sure I see the link between resolving the page margin and making it into its own type with its own constructor. You can expose, through context, a value different from what the user specified directly - this happens today with most synthesized fields; e.g., in a show rule of table.cell, you will see the cell's final position, stroke etc. even if the user didn't specify those. The need to resolve properties for context shouldn't be our sole reason to create new types, as that would apply too broadly to other elements as well.
Indeed, what's missing here is a step in which we resolve page properties before each page is laid out (and, as a consequence, transform what users specified into what Typst effectively is aware of, i.e. into a single dictionary-like structure, through a custom type or not). I elaborate on this approach at #3671. We can see that #3811, for example, doesn't solve the resolving margin auto problem (the tests still show properties returning auto through context), only the structure problem. Resolving the margin and other page properties automatically, as I've previously suggested, would not only remove the auto problem, but also ensure the properties have a uniform structure when obtained through context, solving both problems.
Another problem not solved by a margin type: one can do set margin(3em) to have margins responsive to the main text size in force at the start of the page (I guess). But there's currently no way to make sense of this value in general since the meaning of em can be different in the context where margins are retrieved:
#letconfig= [Text #contexttext.size, margin#contextpage.margin]
#setpage(width: 8cm, height: 4cm, margin: 1em)
#settext(8pt)
#config#pagebreak()
#settext(16pt)
#config#settext(10pt)
#config
is meaningless in the current context
(Example compiled with main)
Here the margins nicely respond to the main text size, but the value returned by context page.margin is unusable in the last case. So I guess the mechanism that will resolve auto in the future should also resolve em lengths to absolute lengths.
Activity
ntjess commentedon Mar 12, 2024
Related note: Since
contextdoesn't providepage.paper, is there an existing way to resolve anautomargin?laurmaedje commentedon Mar 12, 2024
The auto margin can be resolved as
2.5 / 21 * calc.min(page.width, page.height). In the future, that might happen automatically.Leedehai commentedon Mar 13, 2024
Hi, could you pls elaborate on how making a
margintype could help with this use case?ntjess commentedon Mar 14, 2024
The easiest example is to compare to a
stroke. You can pass many forms of stroke into a shape, i.e.black + 5pt(paint: red, dash: "dashed")1emEach of these is a different type, but construct a
strokewith the same set of fields. So having access to a stroke type means the user is not responsible to understand how to handle all these separate formats. They just need to dostroke(input).thicknessto get the line thickness. This is why typst authors added the type in 0.10In the same way, a
margincan be constructed from all the types and arguments listed in the feature request. So if I wanted to know the page margin, it is possible the user passed inmargin: (outside: 3in). In this case, just to know the left margin, I need toIf the
margintype exists, I can just callcontext margin(input).leftand it will provide me this value "out-of-the-box"JustForFun88 commentedon Mar 28, 2024
Can we also consider the ability to specify different margins on different pages?
It will be very useful, especially for the first page!
PgBiel commentedon Mar 28, 2024
I'm not sure I see the link between resolving the page margin and making it into its own type with its own constructor. You can expose, through
context, a value different from what the user specified directly - this happens today with most synthesized fields; e.g., in a show rule oftable.cell, you will see the cell's final position, stroke etc. even if the user didn't specify those. The need to resolve properties for context shouldn't be our sole reason to create new types, as that would apply too broadly to other elements as well.Indeed, what's missing here is a step in which we resolve page properties before each page is laid out (and, as a consequence, transform what users specified into what Typst effectively is aware of, i.e. into a single dictionary-like structure, through a custom type or not). I elaborate on this approach at #3671. We can see that #3811, for example, doesn't solve the resolving margin
autoproblem (the tests still show properties returningautothroughcontext), only the structure problem. Resolving the margin and other page properties automatically, as I've previously suggested, would not only remove theautoproblem, but also ensure the properties have a uniform structure when obtained throughcontext, solving both problems.sijow commentedon Aug 28, 2024
Another problem not solved by a margin type: one can do
set margin(3em)to have margins responsive to the main text size in force at the start of the page (I guess). But there's currently no way to make sense of this value in general since the meaning ofemcan be different in the context where margins are retrieved:(Example compiled with
main)Here the margins nicely respond to the main text size, but the value returned by
context page.marginis unusable in the last case. So I guess the mechanism that will resolveautoin the future should also resolveemlengths to absolute lengths.