How to share bibliography in a multi-file setup?

This is a pretty well-known request: Downgrade missing references from errors to warning · Issue #4035 · typst/typst · GitHub

The main problem is that Typst can’t figure out what to reference if there’s no bibliography. What is being requested in the issue is the ability to ignore missing references so you can get an idea of how the file looks like (but it of course won’t be fully accurate). While that’s not implemented, you’ll have to resort to workarounds.

Here’s one potential way to do it: a common load-bib function which does not render duplicate bibliographies, but gives priority to the bibliography loaded with load-bib(main: true) in the main file:

// bib.typ
#let load-bib(main: false) = {
  counter("bibs").step()

  context if main {
    [#bibliography("bib.yml") <main-bib>]
  } else if query(<main-bib>) == () and counter("bibs").get().first() == 1 {
    // This is the first bibliography, and there is no main bibliography
    bibliography("bib.yml")
  }
}

You can use this in non-main files as follows:

// NON-MAIN FILE

// Assuming bib.typ is at the root of the project
// so we don't have to specify full relative paths each time
#import "/bib.typ": load-bib

// ...File contents...

#load-bib()

And in the main file as follows:

// MAIN FILE

#import "/bib.typ": load-bib

#include "some-non-main-file.typ"

// ...File contents...

// 'main: true' to indicate this bibliography
// must be prioritized
#load-bib(main: true)

Since only the first bibliography is loaded (or the main bibliography, if there is one), this also supports #include between auxiliary (non-main) files!

You can also use hide(bibliography("bib.yml")) in the else of the function if you’d like to turn the bibliography invisible when compiling a non-main file.

1 Like