Skip to content

Option to hide last dot in refs to heading that had custom numbering. #6032

Open

Description

@ertugruluyar-github

Description

If:

#set heading(numbering: "1.1.", supplement: "APPENDIX ")
#heading(level: 1, numbering: none, "APPENDICES")
== Heading 2 <heading-heading-2>

Output of '@heading-heading-2': APPENDIX 1.1
This is a default behavior for only refs to headings.
BUT:

#set heading(
  numbering: (..nums) => "APPENDIX " + numbering("1.1.", ..nums.pos().slice(1)),
  supplement: none,
)
== Heading 2 <heading-heading-2>

Output of '@heading-heading-2': APPENDIX 1.1.

Use Case

Because, in some cases in some languages as Turkish, there is no need to "." to after "Ek 1" in "Ek 1'de".

Solution 1: Supplement accepts a function as numbering field. Similar to #2990

#set heading(
  numbering: (..nums) => "APPENDIX " + numbering("1.1.", ..nums.pos().slice(1)),
  supplement: (..nums) => "APPENDIX " + numbering("1.1", ..nums.pos().slice(1)),
)
== Heading 2 <heading-heading-2>

Output of '@heading-heading-2': APPENDIX 1.1

Solution 2: Supplement has field called seperator as in the figure.caption's.

#set heading(
  numbering: (..nums) => "APPENDIX " + numbering("1.1.", ..nums.pos().slice(1)),
  supplement: (seperator: none, none),
),
== Heading 2 <heading-heading-2>

Output of '@heading-heading-2': APPENDIX 1.1
OR

#set heading(
  numbering: (..nums) => "APPENDIX " + numbering("1.1.", ..nums.pos().slice(1)),
  supplement: (seperator: ":", none),
),
== Heading 2 <heading-heading-2>

Output of '@heading-heading-2': APPENDIX 1.1:

Activity

  1. added
    model
    on Mar 10, 2025
  2. hongjr03 commented on Apr 14, 2025

    @hongjr03
    Contributor
  3. ertugruluyar-github commented on Apr 15, 2025

    @ertugruluyar-github
    Author

    a much simpler example: https://typst.app/project/rGzkNPIy3OGOqT0u2F9FbS

    @hongjr03 But it is different from slice() function because slice take the all elements from starting index while at() function get only one value of the index. See https://typst.app/docs/reference/foundations/array/#definitions-slice

  4. hongjr03 commented on Apr 15, 2025

    @hongjr03
    Contributor

    a much simpler example: https://typst.app/project/rGzkNPIy3OGOqT0u2F9FbS

    @hongjr03 But it is different from slice() function because slice take the all elements from starting index while at() function get only one value of the index. See https://typst.app/docs/reference/foundations/array/#definitions-slice

    Oh yes, I didn't notice that. I should fix the example :)

  5. EpicEricEE commented on Apr 15, 2025

    @EpicEricEE
    Contributor

    One idea would be to simply add a named parameter trim to the numbering function, which gets passed on to the custom function. In your case, this could for example look like

    #set heading(numbering: (..nums, trim) => {
      [APPENDIX #numbering("1.", trim: trim, ..nums.pos().slice(1))]
    })

    The ref element could then by default pass trim: true to the numbering. When creating a show rule for references, this would then also allow you to manually get the trimmed numbering, which isn't currently possible. A disadvantage is that this would be a breaking change for cases where the custom function isn't defined with an argument sink (which is often done for equations or figures, as they commonly only have a single number).

  6. hongjr03 commented on Apr 15, 2025

    @hongjr03
    Contributor

    One idea would be to simply add a named parameter trim to the numbering function, which gets passed on to the custom function. In your case, this could for example look like

    #set heading(numbering: (..nums, trim) => {
      [APPENDIX #numbering("1.", trim: trim, ..nums.pos().slice(1))]
    })

    The ref element could then by default pass trim: true to the numbering. When creating a show rule for references, this would then also allow you to manually get the trimmed numbering, which isn't currently possible. A disadvantage is that this would be a breaking change for cases where the custom function isn't defined with an argument sink (which is often done for equations or figures, as they commonly only have a single number).

    However it's hard to determine which parts belong to prefix or suffix in the result value of function. I would wonder if we could have a numbering rework 😞

  7. hongjr03 commented on Apr 16, 2025

    @hongjr03
    Contributor

    Not sure if this is a good idea to start with: hongjr03@37bd77a

    In this commit, if the numbering function return a dictionary with prefix, numbering and suffix keys, then it can be trimmed in reference.

    An example:

    #let numbly(..arr, default: "1.") = (..nums) => {
      let arr = arr.pos()
      nums = nums.pos()
      
      if nums.len() > arr.len() {
        if default == none {
          return none
        }
        if type(default) == function {
          return default(..nums)
        }
        return numbering(default, ..nums)
      }
      
      let format = arr.at(nums.len() - 1)
      if format == none {
        return none
      }
      if type(format) == function {
        return format(..nums)
      }
      
      let segments = format.split(regex("[\{:\}]"))
      let prefix = segments.first()
      let suffix = segments.last()
      let fparts = segments.slice(1, -1).chunks(3)
      
      (
        prefix: prefix,
        numbering: {
          let result = ""
          for part in fparts {
            let level = part.first()
            let fnum = part.at(1)
            let suff = if part.len() > 2 {
              part.last()
            } else {
              none
            }
            
            if fnum == none {
              continue
            }
            
            result += fnum
                    
            if suff != none {
              result += suff
            }
          }
          result
        },
        suffix: suffix,
      )
    }
    
    #set heading(numbering: numbly("{1:1} |", "{1:1}.{2:1}."))
    
    = title1 <title1>
    
    @title1
    
    = title2 <title2>
    
    @title2
    
    == title3 <title3>
    @title3
    Image
  8. Andrew15-5 commented on Nov 20, 2025

    @Andrew15-5
    Contributor

    From the forum:

    #let in-heading = state("in-heading", false)
    #show heading: it => in-heading.update(true) + it + in-heading.update(false)
    
    #set heading(numbering: (_, ..n) => context {
      if in-heading.get() { numbering("A.", ..n) } else { numbering("A.A", ..n) }
    })
    #let in-heading = state("in-heading", false)
    #show heading: it => in-heading.update(true) + it + in-heading.update(false)
    
    #set heading(numbering: (_, ..n) => context {
      if in-heading.get() { numbering("A.", ..n) } else { numbering("A.A", ..n) }
    })

    Not sure about edge cases.

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 requestmodel

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions