Skip to content

Entry point (main file) detection within Typst document for nested projects #4502

Open

Description

@Andrew15-5

Description

I have a few standalone documents that are self-contained:

sub-document

But I also want to join them into a bundle while preserving their internal look and structure, plus adding an outline and other inter-document links/references so that I can quickly jump to any of the sub-document:

bundle document

The core problem now is less flexible project configuration in the web app:

#let is-bundle-document = false
// #let is-bundle-document = true

...

#let settings(doc, n: auto) = {
  show: set-rules
  show: show-rules

  show: it => {
    import "info.typ": is-bundle-document
    if is-bundle-document { return it }
    if n == auto { return it }
    let n = if n == auto { n } else { n.find(regex("\d+")) }
    show: set-document.with(n: n)
    show: set-page
    set-bibliography
    if n != auto { state("lab-number").update(n) }
    it
  }

  show: turn-on-first-line-indentation
  doc
}

I have to manually toggle the is-bundle-document boolean to be able to successfully compile either bundle or any of the sub-documents. And this problem exists only in the web app. Locally, I have this:

#let settings(doc) = {
  show: set-rules
  show: show-rules

  show: it => {
    import "stuff.typ": get-lab-number
    // FS hack to successfully compile both bundle and individual documents.
    let is-bundle-document = get-lab-number() == none
    if is-bundle-document { return it }
    show: set-document
    show: set-page
    set-bibliography
    it
  }

  show: turn-on-first-line-indentation
  doc
}

where get-lab-number() is just a wrapper for read("/src/lab-number").find(regex("\d+")). Here I utilize the "feature" of absolute file paths as "/src/lab-number" for bundle document and for a sub-document mean/point to different files! All because the TYPST_ROOT or simply PWD are different. So with "realpath" they become project/src/lab-number and project/src/sub1/src/lab-number respectively. And since they (files) can hold different content, I can just read() it without any context and instantly know what is the current "entry point" or which document is being compiled: a specific sub-document or the whole bundle. This one simple trick automates the whole process, so I don't have to touch the content of any document whatsoever and just execute typst c at the right dir or with the right root specified. Pretty neat, huh?

Details

Here is how bundle document starts:

#import "/global/settings.typ": set-document, set-page, show-rules, set-rules
#show: set-document
#show: set-page
#show: set-rules
#show: show-rules
#show bibliography: none
#bibliography("/global/bibliography/main.yaml")

vs. how a sub-document starts:

#import "/global/settings.typ": settings
#show: settings

This is because set document() can be used only once and before any content, nested set page() cause weird behavior and can introduce an empty page at the end, can't use multiple #bibliography statements. And you have to keep in mind that the bundle document literally embeds the whole sub-document simply by using include "sub" + n + "/src/main.typ" in a for loop.

But in order to use the same "global settings" in all (sub-)documents I had to symlink stuff from project/global/ to project/src/sub1/global/ (because the root changes and this way global stuff will be immune to it, i.e., to the root change).

Why not just use context and query or whatever? The main reasons why it should be avoided:

  1. read() must receive str (not content/context());
  2. context conditions are not allowed with set/show rule, at least generally speaking (here are issues for the page (#2326) and document);
  3. global show rule that inserts indentations in correct spots throughout the document will break if the whole sub-document is wrapped in a context.

In the web app, you can't configure TYPST_ROOT or use symlinks. So something that will provide some form of identification of which "main file" or which "document" is being watched/compiled to the web is necessary.

The easiest (maybe not) solution is to add something like if /..../file.typ is main then TYPST_ROOT=/..../ else if ... maybe in typst.toml or just in the web project settings. Or add get-main-file-path() which would return a string with absolute path to the main file (without context). Or something else. Adding symlinks would also be nice (depending on the solution).

Use Case

This is probably a niche request, but this "configurability" of the project should be a general issue, a thing that can and should be improved. So that web app users can have a bit more control, making it more competitive to the local/CLI approach.

But for now, I literally thought of all sorts of crazy ideas, but none of them would work, so I have to use a simple global boolean variable. Toggling it depending on which document is being worked on. This is actually what I come up with initially when building locally, but then I realized that it can be fully automated. Unlike the web.

Can also be discussed here.
Related: #3723.

Activity

  1. added
    feature request
    on Jul 4, 2024
  2. added
    scripting
    on Jul 14, 2024
  3. janosh commented on Jan 22, 2025

    @janosh
    Sponsor

    my (only weakly related) use case is to import a standalone cetz diagram and render as a figure in the main document. for viewing the cetz diagram by itself while coding it, it's nice to set #set page(width: auto, height: auto) but this messes up the page of the main document when left in place once finished. i'm looking for a way to apply that #set command conditionally based on whether the diagram is rendered standalone or imported by the main file. pinging @johannes-wolf in case there already is a solution i missed

  4. Andrew15-5 commented on Jan 22, 2025

    @Andrew15-5
    ContributorAuthor

    This sounds more like #3723.

    I don't see the need to shrink page size if you are not exporting it as SVG. If you don't use the set rule, then you can directly embed the figure with #figure(include "fig.typ").

  5. janosh commented on Jan 22, 2025

    @janosh
    Sponsor

    yeah, i saw and upvoted #3723 as well. include "fig.typ" is what i'm currently doing. setting auto-page size on diagrams is a must. i don't want to render small diagrams floating on a big page. besides the worse UX, it obfuscates when objects (e.g. long labels) extend beyond the intended drawing region

  6. Andrew15-5 commented on Jan 22, 2025

    @Andrew15-5
    ContributorAuthor

    i don't want to render small diagrams floating on a big page.

    Have you tried zooming in on the diagram?

    Here is a workaround by @SkiFire13 that I actually utilized in my web app version of the document (though it's definitely not ideal):

    // fig.typ
    #let my-fig(main: false) = [
      #set page(width: auto, height: auto, margin: 5pt) if main
      #figure(rect(), caption: [classic ```typc rect()```])
    ]
    #my-fig(main: true)
    // main.typ
    #import "fig.typ": my-fig
    #my-fig()

    P.S. For some figures that have a stroke on the edge, the margin: 0pt can cut away half of that stroke thickness, so better to not use 0pt blindly. Not sure if this can be solved.

  7. janosh commented on Jan 23, 2025

    @janosh
    Sponsor

    Have you tried zooming in on the diagram?

    yeah, that's what i meant by worse UX

    thanks for sharing the workaround! 👍

  8. Andrew15-5 commented on Jan 23, 2025

    @Andrew15-5
    ContributorAuthor

    IDK, zooming in on some things is what zoom feature is for. I don't see what is bad about it.

  9. Andrew15-5 commented on Mar 22, 2025

    @Andrew15-5
    ContributorAuthor

    This will also make embedding the main file in the PDF very portable: #pdf.embed(entrypoint()). Now I need to copy over the main file's name inside the file, and if I rename it, I have to edit its content to compile it again. This is more of an issue with a one-file document that you will not just name main.typ.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestscripting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions