Skip to content

Document associated constants - #8743

Merged
laurmaedje merged 4 commits into
typst:mainfrom
MDLC01:constant-doc
Sep 23, 2026
Merged

laurmaedje merged 4 commits into
typst:mainfrom
MDLC01:constant-doc

Conversation

@MDLC01

@MDLC01 MDLC01 commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #8355.

This PR adds infrastructure to support documentation for associated constants in the documentation, and adds documentation for almost all associated constants.

AFAIK, the only constants that still aren't documented are math.dif and math.Dif because I could not find a right place to put them. Note that math.dif is currently only documented on the General Symbols list, and math.Dif not documented at all. I also excluded individual color definitions from the color type, because they are better documented together in the "Predefined colors" section. On the other hand, the "Predefined color maps" section has been moved to the documentation for the color.map module, which is documented as a constant.

Documentation for constants are stored in BindingInfo, as part of a new BindingDocumentation structure, which ensures that a binding cannot have only partial documentation information. When creating a binding from a function, type or element, its documentation is copied to the binding. There is AFAIK a single exception: symbol functions, for which the binding does not have any documentation information (which makes sense, because the binding is for the symbol, not the function).

I would argue that this closes #6567 by making sys.version a searchable item in the documentation, such that searching for "version" yields the result "Compiler Version".

A few screenshots showing some newly-documented constants. A documentation page for all mathematical spaces, with examples. A documentation section for constants from the `calc` module. The documentation page for the `sys` module, where `sys.version` and `sys.inputs` are documented individually.

@MDLC01 MDLC01 added docs Improvements or additions to documentation's content. docs-infra Related to the PDF and website documentation system. labels Aug 12, 2026
@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Aug 17, 2026
Comment thread crates/typst-macros/src/scope.rs Resolved

@laurmaedje laurmaedje left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! This is exciting. :)

Aside from my typical nits, one thing: I feel like it may be a bit hard to distinguish at a glance what is a constant and what is a function.

Image

One solution would be to split up into "Constants" and "Functions" or "Methods", but there are also elements and it would become a bit of a mess with the URL anchors (they should remain stable ideally). Though the group pages do just this (probably for consistency with the existing "Functions" heading?).

Alternatively, we could try to distinguish it visually within the Definitions section (I think I prefer this route).

The question is how. I could potentially see extra parens after the name of a function. E.g. something like is-infinite() and slice(..) be used there (the dots would depend on whether there is at least one argument). But we could also try to do it just visually. I'm open to suggestions.

Also, this looks wrong in PDF:

Image

Comment thread crates/typst-macros/src/util.rs Resolved
Comment thread crates/typst-library/src/layout/dir.rs Resolved
Comment thread docs/components/category.typ Resolved
Comment thread docs/components/category.typ Resolved
@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Aug 26, 2026
@MDLC01
MDLC01 force-pushed the constant-doc branch 7 times, most recently from db8ba07 to e67ea9a Compare August 26, 2026 21:06
@MDLC01

MDLC01 commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator Author

Though the group pages do just this (probably for consistency with the existing "Functions" heading?).

I think the main reason why I implemented a separate section for constants for group pages but not for function pages was because function pages would need to have many other sections as you noted: for constants, methods, associated functions, associated elements, etc..

Alternatively, we could try to distinguish it visually within the Definitions section (I think I prefer this route).

I would personally prefer going the other route, with separate sections for constants, methods, etc.. This would make the documentation more organized and also make it clearer to the user that, e.g., associated functions and methods are different things (currently, the only way to distinguish them is by seeing whether they use the self. prefix or not).

Also, this looks wrong in PDF:
Image

Fixed.

@laurmaedje

Copy link
Copy Markdown
Member

[...] associated functions and methods are different things […]

But they are not. It's rather the opposite; they are exactly the same. Method call syntax is purely call site sugar.

Overall, I don't have a problem with separation sections per se, but I would like to avoid changing the HTML ids and breaking links ("Cool URIs don't change"). Also, which concrete sections would you add?

@MDLC01

MDLC01 commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator Author

[...] associated functions and methods are different things […]

But they are not. It's rather the opposite; they are exactly the same. Method call syntax is purely call site sugar.

That's interesting... I had never realized how far this went. For example, #1.0.from-bytes() errors at 1.0 with "Expected bytes, found float".

I would like to avoid changing the HTML ids and breaking links

I believe this can be implemented without affecting the existing ids.

Also, which concrete sections would you add?

In order, I would replace the "Definitions" section with the following sections (keeping only those that are non-empty):

  • "Associated constants"
  • "Associated functions" for functions, methods, and elements
  • "Associated types"

I think that should cover everything. Optionally, associated elements could be in a section separate from associated functions, but this may be excessive.

Comment thread docs/components/category.typ Resolved
@laurmaedje

Copy link
Copy Markdown
Member

I am as of yet undecided which approach I prefer. I will ask the other team members for their opinion and get back to you.

@laurmaedje laurmaedje added waiting-on-decision A decision must be made to proceed. and removed waiting-on-author Pull request waits on author labels Aug 27, 2026
@laurmaedje

Copy link
Copy Markdown
Member

I discussed this with @reknih. We agreed on having separate sections, but would like to drop the "Associated" prefix. It feels a bit unncessarily technical. So just "Constants", "Functions", and "Types". That's also consistent with the current group pages. And the HTML IDs should ideally remain stable.

@laurmaedje laurmaedje removed the waiting-on-decision A decision must be made to proceed. label Sep 17, 2026
@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Sep 17, 2026
@MDLC01

MDLC01 commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator Author

I implemented the sections for associated definitions. The IDs should remain stable compared to the live version currently at typst.app/docs. The only exception is bare definitions, which currently exists on non-group pages like for the color type, but have been split into constants, functions and types. The IDs for the individual associated items are stable, though (they all use the definitions- prefix).

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Sep 22, 2026
@MDLC01

MDLC01 commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator Author

I am working on fixing the conflicts, sorry for the delay

@laurmaedje

Copy link
Copy Markdown
Member

Oh no, I already started work on that, too. I thought it would be easy at first, so I thought I'll just do it, but it's not. But I'm pretty much done with it now.

@MDLC01

MDLC01 commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator Author

I'm stuck with a gazillion introspection errors that are likely caused by two mistakenly swapped arguments somewhere in category.typ (I had a similar issue when initially working on it), so I'll let you do it if you are almost done.

@laurmaedje

Copy link
Copy Markdown
Member

I pushed my rebase. I also made some additional changes to be able to keep the panic for missing since. It required copying over since information for floor and ceil from the symbol functions and also adding retaining since information for the new format elements.

Since the rebase was quite hairy, I would appreciate if you could double-check whether the final diff matches what you would expect (especially in category.typ). git range-diff main a638105 22de77f39 may also be useful. The output is somewhat hard to read, but at least it shows which parts of the code were touched.

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Sep 23, 2026
@MDLC01

MDLC01 commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator Author

I would appreciate if you could double-check whether the final diff matches what you would expect (especially in category.typ).

The changes to category.typ look good to me, and so does the range diff. I don't think I would be able to be more confident without investing an unreasonable amount of time into it.

@laurmaedje laurmaedje removed the waiting-on-author Pull request waits on author label Sep 23, 2026
@laurmaedje
laurmaedje merged commit f83ff40 into typst:main Sep 23, 2026
9 checks passed
@laurmaedje

Copy link
Copy Markdown
Member

Thank you! A very nice addition to the docs. :)

@MDLC01
MDLC01 deleted the constant-doc branch September 23, 2026 19:40
YDX-2147483647 added a commit to typst-doc-cn/docs-8138 that referenced this pull request Sep 24, 2026
…ember`

This commit migrate to the changes in 83ff40ad (`9f2b6e871^`) _Display constants in the documentation_ in typst/typst#8743.
See also my previous commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation's content. docs-infra Related to the PDF and website documentation system.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Docs for associated constants set typst version in document

4 participants