Open gdahia opened 1 year ago
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.
@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?
Related to https://github.com/typst/typst/issues/1623#issuecomment-1623194643
I wanted to have a
content
for the title, so it can have math in it, but the current template passes the entire title toset document(title: title, abstract: abstract)
and gives an error becausetitle
should bestring
.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