Skip to content

Support for multiple bibliographies - #7277

Closed
thuiop wants to merge 41 commits into
typst:mainfrom
thuiop:multiple-bibliographies
Closed

thuiop wants to merge 41 commits into
typst:mainfrom
thuiop:multiple-bibliographies

Conversation

@thuiop

@thuiop thuiop commented Oct 31, 2025

Copy link
Copy Markdown

This implements support for multiple bibliographies, as wanted in #1097 and discussed on the Discord.

It introduces two new parameters to the #bibliography element:

  • target indicates what is covered by the bibliography. It can either be a selector of cite elements, or auto (default case). In the latter case, priority is given to the bibliographies with selectors; then, each citation is assigned to the next auto bibliography which contains the citation, or the previous one if it cannot be found.
  • kind can be either "shared" or "standalone" (actually anything other than "shared" right now); in the first case, the numbering will keep across bibliographies, in the second one the bibliography will start from one.

This requires typst/hayagriva#409 to be merged in order for the numbering to work correctly.

@thuiop
thuiop force-pushed the multiple-bibliographies branch from f9ad72e to f92d4f1 Compare October 31, 2025 13:27

@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.

I didn't yet have a chance to do a proper full review, but I have a few initial comments already. Feel free to act on these already, but of course you can also wait for the full review (will get to that next week).

/// When using multiple bibliographies in a single document, indicates
/// whether this bibliography should start numbering its citations after
/// the ones from previous bibliographies (`shared`) or start at 1 (`standalone`).
#[default("shared".into())]

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.

I'd have imagined kind to default to auto as well. And standalone wouldn't mean start at 1, but rather "count together with all bibliographies that also have kind: "standalone". Basically each key is one counting track and auto is just the default track.

It still doesn't feel 100% great though as auto implies some extra smartness compared to a string, but there wouldn't be any.

@laurmaedje laurmaedje added bibliography Anything about citations and bibliographies. interface PRs that add to or change Typst's user-facing interface as opposed to internals or docs changes. labels Nov 7, 2025

@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.

Thanks for your work, this is going into a good direction! :)

I left some comments. Besides that, it would be good if you could add some tests for the new functionality. Currently, CI is failing because of the missing hayagriva change. I'd like to wait with merging this until this PR is merge-ready. Until then, you could point from Cargo.toml to your hayagriva fork with the relevant change.

The PR will also need a merge or rebase because it has some conflicts with the introspection warnings changes.

// 2. First preceding auto bibliography containing the label
let citations_and_bib = introspector
.query(&Selector::Or(eco_vec![CiteElem::ELEM.select(), Self::ELEM.select()]));
for (i, citation) in citations_and_bib.iter().enumerate() {

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.

This is a bit problematic as the linear scans for the prev/following bibliography result in overall quadratic complexity. It would be good if we could get this down to $O(n \log n)$. This will probably need one extra data structure containing the bibliographies (e.g. a vector with bibliographies and indices which we can binary search.) Maybe there's a better approach.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

For now I changed it so that it scans only the bibliography elements

Comment on lines +236 to +251
else if let Some(prev_bib) = citations_and_bib[..i]
.iter()
.rev()
.filter_map(|content| {
if content.elem() == Self::ELEM {
Some(content.to_packed::<Self>().unwrap().clone())
}
else {
None
}
})
.filter(|bibliography| {
bibliography.target.get_ref(StyleChain::default()).is_auto()
&& bibliography.sources.derived.has(citation_key)
})
.next()

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.

The code duplication with the previous branch would ideally be avoided.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I tried to factorize the code a bit, not completely happy about it but there is no more duplication.

self.references
self.works
.get(&elem.location().unwrap())
.unwrap()

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.

these unwraps are not ideal. I think they maybe (?) could panic if a bibliography appears in the very last introspection iteration (see the added tests from #7364)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I removed the unwrap there and returned the error instead

Comment thread crates/typst-realize/src/lib.rs Resolved
if content.elem() == Self::ELEM {
Some(content.to_packed::<Self>().unwrap().clone())
}
else {

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.

The formatting is a bit off here. Could you format the files with rustfmt?

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Dec 1, 2025
@laurmaedje

Copy link
Copy Markdown
Member

I looked into the span situation.

In BibliographyElem::assign_citations:

  • The QueryIntrospection that searches for CiteElem | BibliographyElem could be moved outside of the memoized part and use the span of the source citation/bibliography.

  • The related QueryIntrospection for Self::ELEM could be fully removed in favor of filtering the existing citations_and_bib result to save one introspection.

  • The QueryIntrospection for bibliography_selector can use bibliography.span()

In Generator::new:

  • The BibliographyElem::find introspection and CiteGroupIntrospection introspection can be moved into Works::generate and passed down.

This should at least work and unblock the PR. The overhead might be a bit higher than before, but the core logic could stay memoized. I might look into perf impact separately and would potentially adjust queries to return prehashed results to reduce hashing overhead at the memoization boundary, but that's not in scope for the PR.

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed blocked This PR is blocked by something. waiting-on-review This PR is waiting to be reviewed. labels Dec 4, 2025
@thuiop

thuiop commented Dec 22, 2025

Copy link
Copy Markdown
Author

Hi, sorry I have been a bit out of touch these past few weeks. I am ok with making these changes; I think there are other instances that should be moved upwards outside of the memoization? E.g. calling assign_citations should be done in Works::generate instead of Works::generate_impl, and so on.

@thuiop

thuiop commented Dec 22, 2025

Copy link
Copy Markdown
Author

I did what seemed best; not entirely sure about the correct span to give when calling assign_citations during grouping though.

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Jan 5, 2026
@laurmaedje laurmaedje changed the title Implementing support for multiple bibliographies. Support for multiple bibliographies Jan 7, 2026
@laurmaedje laurmaedje mentioned this pull request Feb 16, 2026
@breezykermo

Copy link
Copy Markdown
Contributor

It's great to see that this is being worked on again! The authors may already be aware of this: but since bundling was merged there is a new urgency for this, because AFAIK this extends to Typst documents that are being exported to the bundle format. This means that sites like this one, which has per-page bibliographies, cannot be created with the Typst bundle format currently. (This site was produced with Rheo.)

@laurmaedje

Copy link
Copy Markdown
Member

@breezykermo This is mostly blocked on me right now, but it's a priority for 0.15 and it's still in my plans to get this in.

@laurmaedje

Copy link
Copy Markdown
Member

Hi! I've opened #8307 to complete this. The design is still mostly the same, but I wanted to do some refactoring first and also change up how the citation grouping and assignment interacts, so in the end it made more sense to open a new PR. Thank you for your work nonetheless! It was quite useful to build upon and the design idea with having a selector that targets citations was very nice.

@laurmaedje laurmaedje closed this May 18, 2026
@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label May 18, 2026
@thuiop

thuiop commented May 18, 2026

Copy link
Copy Markdown
Author

Hi! I've opened #8307 to complete this. The design is still mostly the same, but I wanted to do some refactoring first and also change up how the citation grouping and assignment interacts, so in the end it made more sense to open a new PR. Thank you for your work nonetheless! It was quite useful to build upon and the design idea with having a selector that targets citations was very nice.

Hi Laurenz! Makes sense to me; glad that a similar design was retained. Looking forward to see the feature implemented in the next version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bibliography Anything about citations and bibliographies. interface PRs that add to or change Typst's user-facing interface as opposed to internals or docs changes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants