Can't preview singular files with reference to other files or bibliography - references errors in web app

So I’m quite certain I’m not the first to ask about this. But I haven’t found an answer anywhere for a year now or such, so I might aswell ask. How to do references in a way that allows the preview feature in not main to work in the web app.

So phrased again. If you have a file, fx analysis.typ and it contains a reference @ to another file, if you preview the file it doesn’t work because it doesn’t have that ref.

I’ve made the simplest example I could think of to demonstrate it with 3 files with common ancestor main, which #includes these 3 files. Analysis not having the bibtex reference, conclusion not having the reference to another file’s label and introduction that just works, no refs (@).

Example:
https://typst.app/project/r2OeoEEOrsao8sbT1Rk3To

I know there is a github post about suppressing the errors or such, but the whole preview feature just seems almost redundant if it can’t be used once there exists a singular reference in it to outside the file itself. Which is a shame as it’s really nice being able to just view a closed off part of a document, like if working on something collaboratively or just in general.

Looking forward to hear suggestions or ways to solve this.

I don’t know if this will ever be possible, but you can request this feature. Not sure how this can be implemented. I don’t really see the point of this in the first place.

What issues do you have by not being able to just view a closed off part of a document? Collaboration feature will still work. The preview can also follow where you are editing.

I do have a hack for this. The first one didn’t work, not sure why:

#context if query(bibliography).len() == 0 { bibliography("works.bib") }

It supposed to stop at the second iteration, but it doesn’t. Then @SillyFreak had an idea and it worked perfectly:

// preamble.typ
#let include-bibliography = {
  [#metadata(none)<bib>]
  context if query(selector(<bib>).before(here())).len() == 1 {
    bibliography("works.bib", style: "harvard-cite-them-right")
  }
}

You would use it in every file that references the bibliography and needs to be compiled:

// main.typ
// and
// analysis.typ
#import "preamble.typ": *
#include-bibliography

It can be masked as the bibliography function and be further modified:

#let bibliography() = {
  [#metadata(none)<bib>]
  context if query(selector(<bib>).before(here())).len() == 1 {
    std.bibliography("works.bib", style: "harvard-cite-them-right")
  }
}

Though the style perhaps can be moved to a set rule, then you would just call std.bibliography("works.bib"):

#set bibliography(style: "harvard-cite-them-right")

Well actually this also works:

#let bibliography() = context {
  if (query(selector(std.bibliography).before(here())).len() == 0) {
    std.bibliography("works.bib", style: "harvard-cite-them-right")
  }
}

I guess .before(here()) was the missing piece of puzzle.

First off thanks for the suggestions :slightly_smiling_face: it’s very much appreciated and sorry for late reply. And It’s definitely a solution or cool way to do a fix for the bibliography problem.

I’m split on doing a feature request as a solution to this might clash somewhat with what I could imagine or understand are the mindsets for typst structure, referencing other files, SOLID principles, idk, etc.

My interpretation of how to describe your solution “hack”:
Create a function that adds the bibliography isolated pages, if a bibliography isn’t present already. If the document already contains the bibliography or has more than one, use the “first/last” one only.

I’ve made a new project called “with fixes” with the suggestion implemented. However I’ve changed the code to use .after so it’s the latest time which is used as primary, as the bibliography would be at the end of a document.
Link to typst document with fixes implemented

Also I couldn’t get your second and third suggestions with std.bibliography to work, not sure why.

Benefits thoughts

I assume you meant “don’t really see the point” here. So I’ll try and first give some examples of what I can think of use for preview.

  • Imagine 5 or 100 people working on a project online simultaneously, (elementary school class fx), then it can be nice to sit in ones own little world without having to be in a separate project.
  • Having a section you don’t want to add yet to the primary document but still be able to work on it in isolation.
  • Not having to scroll through the main document if people add new pages or stuff is moving.

Summarized, work on stuff isolated can be nice and preview can be a simple way to do so and still be able to ref elements from current project. So I won’t necessarily have many problems myself as I’m hopefully not too far from ending my studies. But I can see it being a really nice feature for collaborative work and something I would have liked or been pleased with throughout my studies.

Figure or label references (Second problem)

You @Andrew have already come up with a cool solution for the bibliography references problem, but there still remains a problem for figure references, which might be the biggest one I guess. Don’t want to ask more of you. So best of luck to if someone tries to solve it.

If accessing either the first or second document TestRefs With Fixes or TestRefs (original link), in conclusion.typ file there is an example that the ref doesn’t work when viewed in preview / isolated to another file. Mostly with same reason for bibliography not working without the fix, as the ref is unavailable or missing from the file.

If however there was a good solution to this then the last of my two problems would be solved.


But i guess there no matter what might be a feature request or two for these two problems.

The general explanation is described here and here plus here.

Why do you have this?

#let bib_state = state("bib_state", 
bibliography("works.bib", style: "harvard-cite-them-right", title: auto))
#bib_state.update(none)

You mean this?

#let bibliography() = {
  [#metadata(none)<bib>]
  context if query(selector(<bib>).after(here())).len() == 0 {
    std.bibliography("works.bib", style: "harvard-cite-them-right")
  }
}

It works perfectly fine. The selector(std.bibliography) doesn’t work because the document doesn’t have anchors to check against, you either add the bibliography now or never. So I guess the label hack is fine.

Yeah, sometimes I do these things.

For the rest, I think that “ones own little world” is not really an argument, but if someone introduces an error, then yes, a separate preview would solve this potential issue. The scrolling also seems overblown, how many times would you have to scroll through the same document, plus there is an outline on the right side, so if you have a sectioned document, then you don’t have to scroll at all.

I alone can’t have enough thoughts on this, so an opened issue is probably a better place to discuss this. And the fact that figure/heading/etc. references are also should be included makes it much less clear how would this be implemented. And right now this is definitely an overcomplication of the project. I would advise you to change your mental model on this, just because it clearly doesn’t work (currently) and it introduces a lot of unnecessary complexity to the document. Maybe using the outline is the real solution for you. Because if realistically a document won’t have any errors most of the time, then there are no benefits left to this feature that are worth it. But that is just how I see it.

It’s a relic from some earlier testing in another project. That’s my mistake sorry about that.


It’s fair for the “ones own little world”, “scrolling” and similar not being seen as a good argument, it was more meant as a way to describe or conceptualize it and thereby showcase potential relevance or use-case. But the outline argument is a really great point or argument for easy or potential “isolation” and navigation. And I’m glad you can see it from the point of view of some of my examples, agreeing or not. And I don’t mind agreeing there are many ways to have a good experience and circumvent the problem.

The primary argument is mostly still:
Preview doesn’t work if you have either a label reference or bib reference if either of those isn’t part of the document itself.

This of course makes perfect sense and sounds stupid to point out. But this from my point of view makes preview on other files than a primary one almost redundant, as the other files can’t be previewed singularly.

preview_snippet

I see the small eye icon and think, I can easily preview this files contents, but it just gives the errors because of missing refs. And that to me feels weird, like why even have it then?

So again thank you for the discussion, contemplating, help and everything else. I’m glad I made the topic post even if it maybe should have been a feature request or similar. Overall I felt it was a little difficult to describe the problem easily, so this helped how to describe it and surrounding elements hopefully :smiley:.

1 Like

I have a project with 2 completely separate documents, so you can preview whichever you want. Moreover, one document contains 4 other ones that can be compiled by themselves (it’s as fancy as it sounds, see this). So I can preview a total of 6 files. It can also be useful for isolated figures like cetz/fletcher diagrams etc. So the eye gives you an ability to preview that file, but whether it will actually do it depends on the specifics of a particular project.