Unclear index out of bounds assertion, when using multiple references in an Alexandria citegroup

Hi,
I am using Alexandria’s citegroups to collapse multiple bibliographic references. I have been doing so with no issues up until trying to include my last thesis chapter 6_Conclustions.typ. Here is a brief overview of my setup:
main.typ

#import "Config/lib.typ": thesis, loadBibliography

#show: thesis.with(title: ...)

#let chapters = (
  "1_Introduction",
  ...
  "6_Conclusions",
)
#for chapter in chapters.enumerate().map(
  it => { (int(it.at(1).first()), "Chapters/$1.typ".replace("$1", it.at(1)), it.at(0) < chapters.len() - 1) }
) {
  let (nth, thePath, shouldBreak) = chapter
  include thePath
  if (nth not in (3,)) { loadBibliography(str(nth)) }
  if shouldBreak { pagebreak() }
}

lib.typ

#let loadBibliography(n) = {
  let pref = "c" + n + ":"
  pagebreak()
  load-bibliography(
    "BIBLIOGRAPHY.bib",
    prefix: pref,
    style: "american-sociological-association"
  )
  // Modifing Appearence
  context {
    // Rendering the bibliography as instructed by
    // https://github.com/ensko/typst-alexandria/blob/3125e05e9dbf2dc802cd01baafd2331d67788b16/docs/manual.pdf#page=5
    ...
  }
}

// Here is the critical part!
// I use the following function to make references to bibliography,
// (singular or as a group)
#let bibref(refs) = {
  if refs == none { panic("refs == none") }
  if type(refs) not in (array, str) { panic("type(refs) not in (array, str)") }
  context {
    let prefix = "c" + str(counter(heading).get().first()) + ":"
    if type(refs) == str {
      ref(label(prefix + refs))
    } else if type(refs) == array {
      import "@preview/alexandria:0.2.2": citegroup
      citegroup(prefix: prefix)[#{
        for r in refs.map(it => ref(label(prefix + it))) { r }
      }]
    }
  }
}

#let thesis(
  // The thesis's title.
  title: [Thesis Title],
  // Parameters...
  
  body
) = {
  // Configurations..

  // Bind Alexandria's prefixes to the same `.bib` file.
  show: alexandria(prefix: "c1:", read: path => read("../" + path))
  ...
  show: alexandria(prefix: "c6:", read: path => read("../" + path))
  
  body
}

As said previously all worked well up until including chapter 6, where i got multiples of index out of bounds errors seemingly occuring at state.typ in Alexandria’s /src/, like the following:

array index out of bounds (index: 1, len: 1) and no default value was specified [Ln 116, Col 14]
lib.typ[Ln 135, Col 31]: error occurred in this call of function `get-citation`

array index out of bounds (index: 12, len: 1) and no default value was specified [Ln 116, Col 14]
lib.typ[Ln 135, Col 31]: error occurred in this call of function `get-citation`

I was unable to thoroughly debug and source the root of the problem nor did these errors hinted me in the right direction. Commenting out the citegroup part of bibref did not produce any errors but obviously sacrificing citegroups is not something in mind. Can you help me?

Thank you for your time!

Hi, would it be possible for you to post a MWE that compiles, preferably as a single file? I am having trouble trying to fill in the gaps in the above example due to a lot of code being left out.
For example, I was not able to include any citations after removing all the "…"s, and I am not sure which parts of the code are alexandra boilerplate and which are written by you. Thanks! :smile:

p.s. you can include a mock bibliography in the code itself with

#let bib = bytes(
```
@book{knuth1998art,
  title={The Art of Computer Programming: Sorting and Searching, volume 3},
  author={Knuth, Donald E},
  year={1998},
  publisher={Addison-Wesley Professional}
}
```.text,
)

@knuth1998art
#bibliography(bib)

There are many external bib files,

  • it is not easy for you to post question
  • or for us to solve your question.

But it is a kind of “out bound” question, so it is NOT HARD to solve.

If I’m not wrong, the author of alexandria pkg is @ensko and he is one of the most active members in the forum. Just wait for the pkg authors’ answer.

Tip: Be sure ur Chapters6.bib is correct. As Typst said it contains 1 item but u referenced it for 13 times (index 0~12).

Perhaps share a MWE via Typst.app so we can see more easily?

Thanks for tagging me @Maxwell, I would have missed this otherwise! I think I will also need an MWE, though.

Does this still happen if you remove a previous chapter (e.g., in your loop, add if nth == 3 { continue })? Or, is there a warning that layout did not converge in addition to the error? If it doesn’t, or if there is, it’s not actually (immediately) Alexandria.

Unfortunately, Alexandria uses almost all the iteration budget that Typst gives: bibliographies are first configured, then citations are collected, then the bibliography is created by Alexandria, and finally the citations are actually rendered. Using the heading counter to contextually determine the prefix adds another iteration, and that may be too much. Maybe there’s also something else going on that adds an iteration?

The iteration before the one that actually succeeds doesn’t have the correct bibliography yet, so the citations fail. Normally you don’t see that, but when the iterations run out, the errors of that iteration are visible, even though they’re not the root of the problem.

1 Like