Skip to content

Can't use function for heading numbering if level is greater than 1 #2101

Closed

Description

@Andrew15-5

Description

This is ok:

#heading(numbering: it => it)[]
#heading(level: 1, numbering: it => it)[]

Compiler error:

#heading(level: 2, numbering: it => it)[]

I've checked the documentation and I don't see any warnings that this wouldn't work. Either add a note about this, or this is a bug.


I was using it like this: #heading(numbering: it => none)[] and wanted to pass a level, but suddenly got an error. Then I've checked, and I can actually pass none directly and therefore don't need to decrease heading counter. But this removes the nested indent in outline (with it => none the indent is present, but the number is missing, which is what I want).

Reproduction URL

No response

Operating system

Linux

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Sep 10, 2023
  2. freundTech commented on Sep 10, 2023

    @freundTech
    Contributor

    This is a bit of a quirk due to the fact that numberings use varargs arguments.
    Look at the numbering function:

    numbering("1.", 3)   // 3.
    numbering("1.", 3, 7)  // 3.7.

    Your custom numbering function must also accept a variable amount of arguments. This is done like this:

    #set heading(numbering: (..n) => {
      // use n.pos().at(x) to access level x+1
    }

    If the level is fixed like in your example you can also accept a fixed number of arguments like this:

    #heading(level: 2, numbering: (n1, n2) => none)[]
  3. Andrew15-5 commented on Sep 10, 2023

    @Andrew15-5
    ContributorAuthor

    https://typst.app/docs/reference/meta/outline/:

    How to indent the outline's entries.

    auto: Indents the numbering of the nested entry with the title of its parent entry. This only has an effect if the entries are numbered (e.g., via heading numbering).

    Ok, I've found everything that I wanted, including that I used true instead of auto. But now the question is what is auto exactly? The documentation doesn't say that.

    But I've found a solution to my problem:

    #set outline(indent: it => for _ in range(it) { h(1em) }) // I'm guessing `auto` is an alias for `1em`
    #let unnumbered-heading(name, ..args) = {
      heading(numbering: none, name, ..args)
    }

    Update:
    Actually, outline can be just #set outline(indent: 1em). I've tried bigger nesting level directly after 1st level, which doesn't work either way. You have to have at least 1 entry of every single level from 1 to n (no skips).

  4. Andrew15-5 commented on Sep 10, 2023

    @Andrew15-5
    ContributorAuthor

    @freundTech, I guess it is kinda understanding, if I dig deep:

    https://typst.app/docs/reference/meta/heading/#parameters-numbering:

    image

    https://typst.app/docs/reference/meta/numbering/:

    image


    But the problem is more to do with the error:

    error: unexpected argument
       ┌─ /file.typ:42:21
       │
    42 │   heading(numbering: it => it, name, ..args)
       │                      ^^
    

    Like, what does it say? How it is unexpected, when with level: 1 everything works? The function has to get some arguments, right? So maybe change the error to be more user-friendly? I also probably should open a separate issue.

  5. Enivex commented on Sep 10, 2023

    @Enivex
    Collaborator

    It's unexpected because the way it's written you can receive only a single argument.

  6. laurmaedje commented on Sep 10, 2023

    @laurmaedje
    Member

    The diagnostic is bad, you can open a separate issue about that.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions