typst / hayagriva

Rusty bibliography management.
Apache License 2.0
350 stars 55 forks source link

Incorrect formatting of collapsed in-text citations when using `collapse` and `after-collapse-delimiter` attribute in CSL #154

Closed csimide closed 3 months ago

csimide commented 7 months ago

I have encountered an issue with the collapsing behavior of consecutive citation numbers in in-text citations when using the bibliography function in Typst. Using the citation style "gb-7714-2015-numeric", the relevant CSL code for citation rendering is as follows:

<citation collapse="citation-number" after-collapse-delimiter=",">
  <layout vertical-align="sup" delimiter="," prefix="[" suffix="]">
    <text macro="citation-layout"/>
  </layout>
</citation>

According to this style, in-text citations should be rendered as superscript numbers enclosed in square brackets, e.g., [1]. If multiple references are cited at the same location, their numbers should be separated by commas, e.g., [1,3,5]. If the cited reference numbers are consecutive, they should be collapsed, e.g., [1-3,5]. Zotero also renders in-text citations in this manner.

However, in Typst, the collapsing behavior does not work as expected. When citing multiple consecutive references at the same location, Typst displays them as [1,3] instead of the expected [1-3]. Similarly, when citing references 1, 2, 3, and 5 at the same location, Typst renders them as [1,3,5] instead of the expected [1-3,5].

1 - @TestBook_1
2 - @TestBook_2
3 - @TestBook_3
4 - @TestBook_4
5 - @TestBook_5

Test 1: Expected #super[[1-3,5]] Actual @TestBook_1 @TestBook_2 @TestBook_3 @TestBook_5

Test 2: Expected #super[[1,3,5]] Actual @TestBook_1 @TestBook_3 @TestBook_5

Test 3: Expected #super[[1-3]] Actual @TestBook_1 @TestBook_2 @TestBook_3

#bibliography("ref.bib", style: "gb-7714-2015-numeric")

image

Through testing, I found that in Typst/hayagriva the after-collapse-delimiter attribute in the CSL file controls the delimiter used for collapsing consecutive numbers. Changing after-collapse-delimiter="," to after-collapse-delimiter="-" results in the expected behavior, displaying [1-3,5].

However, according to the CSL 1.0.2 documentation - Cite Collapsing, the after-collapse-delimiter attribute is described as:

Specifies the cite delimiter to be used after a collapsed cite group.

Based on the example provided in the documentation, I think this attribute should control the comma (,) in [1-3,5], not the hyphen (-). I have tested other numeric citation styles with the collapse attribute and observed similar issues.

I kindly request guidance on whether this behavior is intended or if there is a potential issue with the collapsing implementation in Typst/hayagriva.

reknih commented 6 months ago

This is indeed a bug, thank you for spotting! A PR would be most appreciated.