Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AMS template does not support math mode in title #12

Open
gdahia opened this issue Jul 18, 2023 · 2 comments
Open

AMS template does not support math mode in title #12

gdahia opened this issue Jul 18, 2023 · 2 comments

Comments

@gdahia
Copy link

gdahia commented Jul 18, 2023

Related to typst/typst#1623 (comment)

I wanted to have a content for the title, so it can have math in it, but the current template passes the entire title to set document(title: title, abstract: abstract) and gives an error because title should be string.

I am interested in extracting the plain text representation of the content for the title like mentioned in the linked issue, but I have failed to do so.

Meanwhile, do you think it would be interesting to avoid breaking compilation of titles with content type by doing something like

set document(title: if type(title) == "string" { title } else { none }, author: names)
@laurmaedje
Copy link
Member

I am considering to support passing arbitrary content for set document(title: ..). I also plan to give the templates a facelift fairly soon, so if that didn't happen until then, I'm open to the workaround.

@denkspuren
Copy link

denkspuren commented Nov 5, 2023

@laurmaedje, I have a solution to this problem (which is also mentioned here): To use a string or content for the title of a document, the only problem is to set the title metadata properly, which has to be of type string.

I wrote a function named extractText that concatenates all strings to be found in a value of type content, string or otherwise and return it. Given the function extractText the above mentioned line of code in template.typ can be written as

set document(title: extractText(title), author: authors.map(author => author.name))

The helper function can be outsourced and imported or included in the template:

#let extractText(element) = {
  if type(element) == content {
    if element == [ ] { return " " }
    return extractText(element.fields()).trim() }
  if type(element) == dictionary { return extractText(element.values()) }
  if type(element) == array {
    return element.fold("", (res, item) => res + extractText(item))
  }
  if type(element) == bool { return "" }
  return str(element)
}

#assert.eq(extractText("hey"), "hey")
#assert.eq(extractText(12), "12")
#assert.eq(extractText(12.0), "12")
#assert.eq(extractText(12.1), "12.1")
#assert.eq(extractText(false), "")
#assert.eq(extractText(version(1,2,3)), "1.2.3")
#assert.eq(extractText((1,2,3)), "123")
#assert.eq(extractText((4,(1,"Hey",12.0),(hey: 2))), "41Hey122")
#assert.eq(extractText([This is some text.]), "This is some text.")
#assert.eq(extractText([This is _some_ text.]), "This is some text.")
#assert.eq(extractText([ Is $x^2$ an _even_ Function? ]), "Is x2 an even Function?")
#assert.eq(extractText([Is $x^2$ an _even_ Function?]), "Is x2 an even Function?")
#assert.eq(extractText([[Hey] [you]]), "[Hey] [you]")

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants