I have a simple dictionary that stores short strings and would like to use it to insert those in the text like:
Here’s some text #mydict.key and more text
I would like to get all the inserted text from the dictionary formatted in bold (ideally a show rule) but can’t figure it out.
Of course it works with a function that accepts a string representing the key, but I loose the autocompletion capability (and therefore is prone to typos in the keys).
Hi @AMendoza, thank you for your question! I have changed your post’s title to bring it in line with the question guidelines and thus make it easier to understand from the title:
Good titles are questions you would ask your friend about Typst.
I also added the scripting tag, as it makes your question easier to find.
I would recommend preparing a dictionary in which the strings are already formatted:
#let mydict = (key: "something")
#let mydict = {
mydict
// convert the tict into an array of (key, value) pairs
.pairs()
// replace each pair ...
.map(pair => {
// by making the second item (the value) *strong*
pair.last() = strong(pair.last())
// return that pair, so that it is used for the replacement
pair
})
// convert back to a dict
.to-dict()
}
Here’s some text #mydict.key and more text
The values in this dictionary will no longer be strings but content. If you need the original strings, you can of course use a different name for the variable instead of redefining let mydict.
I think you really should just use destructuring of the pair and then creating a new one. Or, to save 1 extra character, you can create a dictionary instead and join them:
#let mydict = (key: "something")
#let mydict = mydict.pairs().map(((a, b)) => (a, strong(b))).to-dict()
#let mydict = mydict.pairs().map(((a, b)) => ((a): strong(b))).join()
Here’s some text #mydict.key and more text
I do that in my code, but for the explanation I wasn’t sure what would be the easiest to read (I’m not always going for maximum conciseness here). I’ll leave it as-is, but your reply is helpful in showing additional ways to write the same thing.