Skip to content

Turning content into string: str(content) #2196

Closed as not planned
Closed as not planned

Description

@zouharvi

Description

The str() function could accept content out of which it would extract the textual parts. For example: str([Hello _there_]) would be "Hello there".

Use Case

In a few projects I want to set the document (PDF) title but also display the title in the doc itself. However, the title might be content and not a string. That means that it needs to be duplicated:

set document(title: "This cool project")

align(center)[
  #set text(size: 1.5em)
  This _*cool*_ project
]

This is not a big deal but leads to these kinds of templates which feel weird because of the duplicity:

#show: project.with(
  codename: "This cool project",
  title: [This _*cool*_ project],
)

Ideally, one would be able to write the following, with conversion from content to string inside of the template.

#show: project.with(
  title: [This _*cool*_ project],
)

I acknowledge that there are many vague cases and lots of complexity -- I'm just putting this out. Maybe there's also an existing solution to this which I overlooked, such as iterating the children of the content and only collecting the textual yield.

Activity

  1. added
    feature request
    on Sep 20, 2023
  2. changed the title [-]Turning content into string with str(content)[/-] [+]Turning content into string: `str(content)`[/+] on Sep 20, 2023
  3. EpicEricEE commented on Sep 20, 2023

    @EpicEricEE
    Contributor

    You can indeed iterate over the children and collect the text recursively. It's definitely not perfect, but it works for your case:

    #let to-string(it) = {
      if type(it) == str {
        it
      } else if type(it) != content {
        str(it)
      } else if it.has("text") {
        it.text
      } else if it.has("children") {
        it.children.map(to-string).join()
      } else if it.has("body") {
        to-string(it.body)
      } else if it == [ ] {
        " "
      }
    }
    
    #to-string[This _*cool*_ project] // => "This cool project"
  4. zouharvi commented on Sep 20, 2023

    @zouharvi
    ContributorAuthor

    Thanks @EpicEricEE. That feels like should be the implementation of str(content) so I'll leave the issue open to keep this under consideration but anyone else feel free to close it.

  5. laurmaedje commented on Sep 20, 2023

    @laurmaedje
    Member

    Internally, there is already a plain text functionality (via the PlainText trait), which is used for instance for the PDF outline.

    Since the issue mostly comes up with set document(title: _), I'm considering to just allow content there and do the plain-text conversion internally.

    Long-term, with get rules, this would also mean that more "standard" metadata like title, author, etc. could be specified with document set rules instead of on the template and templates could read that. For that, the title would also need to be content.

  6. added
    scripting
    on Sep 21, 2023
  7. laurmaedje commented on Nov 20, 2023

    @laurmaedje
    Member

    I have pushed a commit that allows arbitrary content as the document title. It will then be turned into a string internally. This should cover your use case. I will also update the web app templates to default to a content block instead of a string with the next release.

    I'm not comfortable with exposing the internal plain-text functionality at the moment because it's very lossy for more complex content.

  8. zouharvi commented on Nov 20, 2023

    @zouharvi
    ContributorAuthor

    Thank you for resolving this!

  9. emilyyyylime commented on Feb 27, 2024

    @emilyyyylime
    Contributor

    @laurmaedje would it be possible to apply the same change for document author?

  10. laurmaedje commented on Feb 27, 2024

    @laurmaedje
    Member

    Does it really make sense for an author name to have markup?

  11. emilyyyylime commented on Feb 28, 2024

    @emilyyyylime
    Contributor

    in Hebrew for example, I might write ' and use a show rule to turn it into a Geresh ׳, or someone might want smart quotes when entering their Name "nickname" Surname. It's mostly about simple conversions like that and about being able to reuse the same variable later in like the title page.

  12. laurmaedje commented on Feb 28, 2024

    @laurmaedje
    Member

    Unfortunately the plain text conversion employed to convert the title to a string is very naive, so it wouldn't be able to handle show rules. I think it also doesn't properly handle smartquotes, but that would be fixable.

  13. gasche commented on Jan 21, 2025

    @gasche

    I looked for this feature to have a convenient shortcut for inline code. I'm writing content with a lot of inline OCaml code, and instead of writing

     ... ```ocaml Foo``` ...
    

    I would like to be able to write #code[Foo] or even #c[Foo]. This suggests the definition

    #let code(s) = raw(s, lang: "ocaml")
    

    but this does not work because s is content in #code[Foo], but raw expects a string argument. The current solutions are to call this like #c("Foo"), which is conceptually cleaner but also noticeably less convenient to type, or to assume that the caller will only pass string-like content (no nested structure) and call s.text:

    #let code(s) = raw(s.text, lang: "ocaml")
    
  14. goyalyashpal commented on Jun 18, 2025

    @goyalyashpal

    most of the time, i need this kind of functionality as the string concatenation with . + " " + . becomes VERY ugly, very fast.

    i don't need to use content in place of strings, or even perform any conversion per say, but i just need some easy way to join strings.

    think: the new f-strings in python, vs. doing via + strcat operator in olden times.

    context:

    let filecontents = if prependName == true {
    
        if type(comment) == str {
          comment + " " + filename + "\n" + read(file)
    
        } else if type(comment) == array [
          #comment.at(0) #filename #comment.at(1)
          #read(file)
        ]  // <~~~~ Intended easy way to concatenate strings.
    
      } else {
        read(file)
      }

    focussing in on the intended part:

    • some string templating construct akin to typ marking content:

      [
        #comment.at(0) #filename #comment.at(1)
        #read(file)
      ] 
    • versus current way to fight with typc

      {
        comment.at(0) + " " + filename + " " + comment.at(1) + "\n" + read(file)
      }
      
  15. NamorNiradnug commented on Oct 12, 2026

    @NamorNiradnug

    This would actually be useful for keywords as well, especially in math, where some math stuff can be a part of a keyword

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