How to share bibliography in a multi-file setup?

In my setup where I include my chapters into separate typ files and include them in my main file, I can create the bibliography in the main file and it detects all references across the included subfiles. However when previewing the parts, the referenced citation can’t be found, throwing an error and file preview is disabled. I can’t import the same bibliography either, since only one is supported. It has to be shared somehow but I don’t know how.

1 Like

there was an workaround, but it stopped working in 0.12
Multiple bibliographies · Issue #1097 · typst/typst · GitHub

Hello @Daniel_Nief, have you looked at

I think it might help you!

In any case, if you could tell us more about how you “preview the parts”, we could help you more.

1 Like

Those are some interesting sources but it didn’t quite solve the problem.
Importing the bibliography directly into the parts will lead to multiple bibliographies, which his not allowed.

Here’s my setup:

  • main.typ
  • template.typ
  • parts/
    • part1.typ
    • part1.typ

main.typ:
#import “styles.typ” : *
#show: layout

@source
#include “parts/part1.typ”
#include “parts/part2.typ”
#bibliography(“references.yml”)

part1.typ & part2.typ:
#import “styles.typ” : *
#show: layout
@source

In my references if have a source, it can be accessed using the label <source> or @source. But only if there’s a bibliography present in the file.
So when I cite @source1 in main.typ, it works. When I cite @source in main.typ, I can preview main.typ because it knows the bibliography, I can’t preview part1.typ because it doesn’t recognize the label, because there’s no bibliography in part1.typ

Now if I store the bibliography in a variable and add it to main.typ and part1.typ, this will generate two bibliography objects, throwing the error that I can’t have two bibliographies.

I’ve seen the workaround of overwriting the labels at the beginning of the parts, but it’s a workaround, not a solution, it won’t show the source in the bibliography of main.typ with the workaround in place.

I’m wondering what your use case would be to individually preview each part of your document? Is the compile time an issue?

Well previewing main.typ would suffice, it really is nice, compared to other tools. There’s just little icks caused by the missing references like VS Code marking pages red adding to it.

Not a real problem but would be nice to have a consistent fix.

What I suggest is to setup a conditional bibliography command. See Can I configure my document (e.g. draft/release version, color theme) when creating a PDF without modifying the Typst file directly?, so it will only import the references in the subfiles when you are in “subfile mode”.

In any case, I am curious about your issues with missing references, could you give more details?

There’s actually not much more to it other than VS Code error detection and the subfile not being able to compile. I’ll just preview main.typ from now on and/or use the dummy workaround for now. There’s not much configuration needed for it so…

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.