Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Incubator Q&A

Welcome to the staging ground for new communities! Each proposal has a description in the "Descriptions" category and a body of questions and answers in "Incubator Q&A". You can ask questions (and get answers, we hope!) right away, and start new proposals.

Are you here to participate in a specific proposal? Click on the proposal tag (with the dark outline) to see only posts about that proposal and not all of the others that are in progress. Tags are at the bottom of each post.

Comments on How can Typst check whether a string is alphanumeric?

Post

How can Typst check whether a string is alphanumeric? Question

+3
−0

I personally think that alphanumeric postal codes (such as used in the UK, Canada, and Ireland) look best in small block capitals and old-style numbers. So I created a postcode function to do that:

#let postcode(it) = {
  set text(number-type: "old-style")
  let l = lower(it)
  smallcaps(l)
}

However, most countries use purely numeric postal codes, and they probably look better in lining numbers. Can Typst check whether a string is alphanumeric or numeric?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Temporary hack (4 comments)
Temporary hack
trichoplax‭ wrote 2 days ago

Hopefully someone can reveal a better way to do this in Typst, but for programming languages in general my favourite hacky approach is to check for equivalence between the string converted to lowercase and the string converted to uppercase. There will be equivalence only when there are no letters.

This only works for written languages that have the concept of upper and lower case, but that covers the countries mentioned.

TRiG‭ wrote 1 day ago

It seems that two content blocks are always distinct, so I first need to convert the content to a string. But that does indeed work.

#let postcode(it) = {
  let s = it.text
  let l = lower(s)
  if l == s {
    it
  } else {
    set text(number-type: "old-style")
    smallcaps(l)
  }
}
trichoplax‭ wrote 1 day ago

This will work if the input postcode always contains upper case letters. If you need the function to be able to handle input postcodes with lower case letters (rather than rejecting them as incorrect), you could use something like if upper(s) == lower(s).

TRiG‭ wrote 1 day ago

Thanks. You're right. I've made that tweak now. And given the user options.

#let postcode_set(it, apply: "alphanumeric") = {
  if apply == none {
    upper(it)
  } else {
    let s = it.text
    let l = lower(s)
    let u = upper(s)
    if (l == u) and (apply == "alphanumeric") {
      it
    } else {
      set text(number-type: "old-style")
      smallcaps(l)
    }
  }
}

// Use one of these options:
#let postcode = postcode_set.with(apply: "all");
#let postcode = postcode_set;
#let postcode = postcode_set.with(apply: none)