Skip to content

Different heading text for outline/bookmark and displaying #1889

Open

Description

@balthild

Description

When using LaTeX, I'm able to specify an alternate text for \part, \chapter, \section, etc.

\chapter[alternate text]{display text}

If specified, the alternate text is used in ToC and PDF bookmarks.

Use Case

This is useful when the title is long and I want to break it into lines manually without affecting the outline and bookmark.

image

It is also useful when I want to put some additional information (e.g. author, year) into the heading but not into the ToC.

Activity

  1. added
    feature request
    on Aug 8, 2023
  2. added
    model
    and removed on Nov 24, 2023
  3. Andrew15-5 commented on Feb 9, 2024

    @Andrew15-5
    Contributor

    I found one hacking solution in this repo: https://github.com/nju-lug/nju-thesis-typst/blob/main/nju-thesis/utils/invisible-heading.typ

    Pretty sure you want to use this instead (#2991 (comment)):

    #outline()
    
    = Introduction
    
    #{
      show heading: none
      heading[Secret heading]
    }
    
    = Background
  4. laurmaedje commented on Jul 15, 2024

    @laurmaedje
    Member

    Related: #1295

  5. johnmapeson commented on Oct 8, 2024

    @johnmapeson

    If I understand correctly, @Andrew15-5 's solution should use #heading(outlined: false)[Introduction] instead of just = Introduction:

    #outline()
    
    #heading(outlined: false)[Introduction]
    
    #{
      show heading: none
      heading[Secret heading]
    }
    
    = Background
    Contents
    
    Secret heading .............
    Background .................
    
  6. Andrew15-5 commented on Oct 9, 2024

    @Andrew15-5
    Contributor

    I specifically corrected the given example. But for the OP's solution you're right, although at this point I might use a wrapper function that mimics the one provided in the OP:

    #let chapter(alt, body) = {
      heading(outlined: false, body)
      show heading: none
      heading(alt)
    }
    
    #chapter[alternate text][display text]

    You can make the alt parameter optional, but instead of that you might as well just use = Heading syntax.

  7. johnmapeson commented on Oct 9, 2024

    @johnmapeson

    This looks useful, thanks. Do you know, is it possible to fix it so that when I click a ToC record, the document is scrolled so that the "display text" line is not scrolled over?

    #outline()
    
    #lorem(1000)
    
    #let chapter(alt, body) = {
      heading(outlined: false, body)
      show heading: none
      heading(alt)
    }
    
    #chapter[alternate text][display text]
    
    #lorem(1000)
    image
  8. Andrew15-5 commented on Oct 9, 2024

    @Andrew15-5
    Contributor

    Do you know, is it possible to fix it so that when I click a ToC record, the document is scrolled so that the "display text" line is not scrolled over?

    I think I actually was thinking that this could happen, but in any case, simply changing the order shouldn't break anything else and will fix the thing:

    #let chapter(alt, body) = {
      {
        show heading: none
        heading(alt)
      }
      heading(outlined: false, body)
    }
  9. goyalyashpal commented on Feb 6, 2025

    @goyalyashpal
    #{
      show heading: none
      heading[Secret heading]
    }
    
    = Background

    - @ Andrew15-5 at #1889 (comment)
    - @ EpicEricEE at https://forum.typst.app/t/685/2 "how-to-add-an-invisible-heading-to-an-outline/"

    great. i used it to add an index entry titled "Contents" in the PDF outline-metadata (aka PDF-Index). the #outline() call has already added the corresponding visible content on the page.

    i think that call to outline() should create a bookmark to itself by default.

    there have been coutless examples in pdf books having looooong TOC contents and maaaannny pages before TOC too, but no entry to TOC in the PDF-Index. so to reach the top of the TOC, the reader is left to scroll and scroll and scroll and scroll.

    #{
        show heading: none
        heading(bookmarked: true, outlined: false,)[Contents]
    }
    
    #outline(
        indent: {1em},
        depth: 3,
        // fill: none,
    )
    
  10. Andrew15-5 commented on Mar 15, 2025

    @Andrew15-5
    Contributor

    It looks like the last workaround I posted isn't perfect: Andrew15-5/rubby#6 (comment). If you have nested heading levels after the chapter call, then you have to insert alt heading last, otherwise the PDF outline will break. Which I think is a separate bug.

  11. MaxenceG2M commented on Jun 24, 2025

    @MaxenceG2M

    This bug is due to the increment of counter each time heading is called.

    Image

    I haven't found a solution yet, since counter is hard to update / decrement

  12. Andrew15-5 commented on Jun 25, 2025

    @Andrew15-5
    Contributor
    #set heading(numbering: "1.")
    
    /// Correct PDF Document Outline, but the outline link is below the heading.
    #let chapter(alt, body) = {
      heading(bookmarked: false, body)
      show heading: none
      heading(outlined: false, bookmarked: true, numbering: none, alt)
    }
    
    /// Incorrect PDF Document Outline, but the outline link is correct.
    #let chapter(alt, body) = {
      {
        show heading: none
        heading(outlined: false, bookmarked: true, numbering: none, alt)
      }
      heading(bookmarked: false, body)
    }
    
    #chapter[alt][body]
    == sub
    #chapter[alt2][body2]

    Image

  13. MaxenceG2M commented on Jun 25, 2025

    @MaxenceG2M

    Interessting alternative, thank you!
    Sadly the outline use the "body" title, not the alt... (you can say I'm never happy 😄)
    If we add in outline the "unumbered" title, numbering is OK but the final outline looks like crap...

    Image

    (i'm in a context where I want to use alternative title for other levels of titles)

    No matter how many times I turn it, I don't think the tool provides a satisfying solution :(

    Thanks again for your thoughts and help

  14. Andrew15-5 commented on Jun 25, 2025

    @Andrew15-5
    Contributor

    Sadly the outline use the "body" title, not the alt.

    What does this even mean? The functions produce the documented result.

  15. MaxenceG2M commented on Jun 25, 2025

    @MaxenceG2M

    Yeah, but not the result wanted in this issue.

    In the @balthild (from LaTeX), you have:

    • a title that appear in the body of the document ("display text" in original example case)
    • an alterative text that appear in the ToC with numerotation and in the PDF bookmarks

    Wanted solution (LaTeX):
    Image

    Actual typst solution:
    Image

  16. bluss commented on Jun 25, 2025

    @bluss
    Contributor

    Here is an alternate workaround.

    Use headings with short and long titles. The real body of the heading is the short title, so without change it will appear in bookmarks and outlines. But the heading itself has a show rule so that it is displayed using its long title.

    #outline()
    
    #set heading(numbering: "1.")
    
    #let findtitle(body) = {
      if body.has("children") and type(body.children.at(-1, default: none)) == content {
        let mt = body.children.at(-1)
        if mt.func() == metadata and mt.at("label", default: none) == <altheading> {
          return mt.value.longtitle
        }
      }
    }
    
    #show heading: it => {
      let longtitle = findtitle(it.body)
      block({
        if it.numbering != none {
          numbering(it.numbering, ..counter(heading).get())
          h(0.3em)
        }
        if longtitle != none { longtitle } else { it.body }
      })
    }
    
    /// Create a chapter heading
    ///
    /// The document data/outline/bookmark title is `title`
    /// but the displayed title by the heading itself is `longtitle`.
    #let chapter(title, longtitle) = {
      heading({
        title
        [#metadata((title: title, longtitle: longtitle))<altheading>]
      })
    }
    
    #chapter[Short A][Long title A]
    == Normal Sub
    #chapter[Short B][Long title B]
  17. Andrew15-5 commented on Jun 26, 2025

    @Andrew15-5
    Contributor

    an alterative text that appear in the ToC with numerotation and in the PDF bookmarks

    Oh, well I didn't think about it like that, not sure why.

    Actually, Andrew15-5/rubby#6 has a different problem, hence why I implemented the functions this way. Since the Document Outline only supports plain text, you can't include ruby in it, but you can in the outline()/heading(), therefore the alt is only to fix the Document Outline.

  18. szanni commented on Mar 24, 2026

    @szanni

    My suggestion would be to add an optional parameter to heading such as:

    #heading(alt: "Alt Text")[Long Text]

    Call it alt, outline-text, short or something else.

    And then think about a short hand syntax like:

    =[Alt] Long Text or so.

    Because the hacky solutions presented here are sadly all very clunky. Some break the pdf bookmarks, some jump to low, others jump to high or to the previous page and if I stick the double header into an unbreakable block my show rules that add pagebreaks before headers explode.

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