Skip to content

Set document conditionally #4769

Closed

Description

@cahalu

Description

// Definition
#let authors = context document.author.len()

// Output
Authors: #authors

// Error A (no error and nothing in about dialog)
#set document(author: "test") if authors == 0

// Error B (no error and nothing in about dialog)
#if (authors == 0) { set document(author: "test"); [Done] }

Reproduction URL

No response

Operating system

Windows

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Aug 16, 2024
  2. added a commit that references this issue on Aug 16, 2024
  3. laurmaedje commented on Aug 16, 2024

    @laurmaedje
    Member

    In your example, authors is of type content, not int so all the comparisons return false. Everything that depends on the contextual information needs to be inside of the context expression.

    That said, before #4768 was merged (which I coincidentally was just working on as you opened the issue) this still wouldn't have worked due to other bugs.

    However, with it now merged, you can do it like this:

    #set document(author: "Normal")
    #context {
      set document(author: "Changed") if "Normal" in document.author
    }

    Note that document set rules are a special case here. Usually having a set rule without any following content in the same block is pointless because the set rule is scoped to the code block. However, because the document is a singleton, scoping does not make sense. So for document set rules, the last one just wins.

    For other kinds of set rules, you'd usually have a setup like this, with an "everything show rule":

    #set document(author: "Normal")
    #show: body => context {
      set text(red) if "Normal" in document.author
      body
    }
    Hi there!
  4. added
    styling
    model
    on Aug 16, 2024
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

    bugmodelstyling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions