How to Hack Together an Author-Date sorting for Bibliography?

From Ability to select how a bibliography is sorted · Issue #3913 · typst/typst · GitHub, No alphabetic sorting of bibliography in author-date style · Issue #5914 · typst/typst · GitHub, and Bibliography customization from within Typst · Issue #942 · typst/typst · GitHub, my understanding is that typst does not yet support the ability to select a Bibliography/citation style (in my case alphanumeric for both) while altering the sorting from the default first-appearance method.

I eagerly wait for the day when this does get implemented, but I am lost when trying to figure out if there is a way to hack together a way of getting the sorting right. I have heard about creating something like a hidden module to insert citations without them appearing, but details weren’t given and I couldn’t follow on how to do that.

Thus, is there a way for now to hack together an author-date sorting to the bibliography?

Hello @Paul744, I’m having trouble following your thoughts. I think there is a misconception, you cannot load a style and define a custom sorting, because sorting is defined by the style.

For example, by selecting the “alphanumeric” style, you would load the following CSL style hayagriva/styles/alphanumeric.csl at main · typst/hayagriva · GitHub (if I’m not wrong) which defines sorting as

    <sort>
      <key variable="author" />
      <key variable="issued" />
    </sort>

The CSL spec can be read here.

If you want author-date sorting, you need to load a style that sorts in this way, e.g., chicago-author-date. Below, they sort using three different keys “contributors”, “date-sort” and “title”.

    <sort>
      <key macro="contributors"/>
      <key macro="date-sort"/>
      <key variable="title"/>
    </sort>

I also think that it’s not easy to customize bibliography layout currently in Typst, because there is no dedicated interface, but since there is a standard (CSL), I think it’s the proper way to support customization.

Also, not everything should be done in Typst.

tl;dr: best hack is to define your own sorting in the CSL file. You can use the fantastic visualEditor here! (or mash together different styles).

Hopefully, my explanation was on topic, please tell me if I misunderstood your question!

P.S.: I think a good way to support custom sorting would be to allow taking into account biblatex’s sortkey.

1 Like

to add to @quachpas’s excellent answer, if you don’t want to write your own .csl file, you can find a good implementation here: styles/din-1505-2-alphanumeric.csl at master · citation-style-language/styles · GitHub. This is the .csl that I use, and it sorts the bibliography.

Thank you @quachpas and @aarnent for your explanations.

@quachpas To clarify my point, when I say “while altering the sorting from the default …,” I’m thinking that selecting a new sorting method overrides the one present in the CSL file. If I remember correctly, that was an idea put forward on one of the Github issues, but I might be misremembering. My assumption was that was how LaTeX’s bibtex did it with:

\usepackage[
	backend = biber,
	style = alphabetic,
	sorting = nyt
]{biblatex}

Regarding your point about not everything being done in Typst, while I do agree, formatting citations carefully is an integral-enough part of academic work that if one is going to jump ship from LaTeX, I imagine it could be a deal-breaker. At the very least, if modifying stuff like this isn’t planned, it’s something that should definitely be addressed in the page about moving from LaTeX to Typst, if it isn’t addressed already (I could very well have missed it).

Also, thanks for bringing up the visual editor. I was not aware of that before, but that’s going straight into my bookmarks.

@aarnent Thanks for linking that. I’ll be sure to try it out.

1 Like

Probably what is needed, it that the bibliography element exposes the entries. Then you could control the layout completely. A dependent PR is Add `bibliography.entry` element by xkevio · Pull Request #5932 · typst/typst · GitHub by @xkevio.

Thinking on it, I think there is merit for that and it should be fairly straightforward to implement.