How to transform all uppercase words into smallcaps? #3927
-
In my poor understanding of Typst's scripting, I tried using the below without success: // set all caps to smallcaps
#show regex("[A-Z\s]+"): scaps => {
set text(weight: "regular")
align(left, smallcaps(scaps))
} Please can anyone show me how to auto transform all uppercase words into smallcaps (without having to increase the verbosity of body with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Ten minutes of trial and error, and I have a working solution for the above: // turn all uppercase words (with 2 or more characters) into smallcaps
#show regex("([A-Z]{2,})+"): sc => {
smallcaps(lower(sc))
} The above one assumes the content has UPPERCASE words, which will remain unmodified. This above in the template preamble retains the content as is, but transforms all UPPERCASE words into lowercase first, and then into smallcaps in the compiled document. Still if there is a smart(er) way of doing this, then please do share. Thank you. |
Beta Was this translation helpful? Give feedback.
-
OpenType FeaturesIf your font implements the OpenType features, you could use the combination of E.g., you could use // turn all uppercase words (with 2 or more characters) into smallcaps
#show regex("([A-Z]{2,})+"): sc => {
text(features: ("smcp", "c2sc",))[#sc]
} Enabling RegexYou might also be able to simplify your Regex string from With regex change and only small caps-ing uppercase, // turn all uppercase letters (with 2 or more uppercase characters in a row) into smallcaps
#show regex("[A-Z]{2,}"): sc => {
text(features: ("c2sc",))[#sc]
} The edge-case I notice is that any single letter all-cap words (e.g. Verbosity SpecificallyFor cases where you can't avoid using the styling markup in the text (say regex can't find a word without also styling a bunch of words you don't want styled), you can make your own shorter named function to use instead. An example of this would be: #let sc(content) = smallcaps(content)
An #smallcaps([example]) line of #smallcaps([text]).
Another #sc([example]) line of #sc([text]). The trouble here is remembering what My RecommendationI'd probably use the code: // turn only uppercase letters to small caps
#let SC(content) = text(features: ("c2sc",))[#content]
// turn all uppercase words (any 2 or more grouping of A-Z ASCII characters) into smallcaps
#show regex("[A-Z]{2,}"): match => {
SC(match)
}
THIS LINE WOULD BE ALL SMALL CAPS.
#SC([I]) HAVE TO ADD SMALL CAPS MANUALLY TO #SC([A]) LINE LIKE THIS. TO CONVERT #SC([I]) AND #SC([A]).
#SC([Which I could also do this way.]) // Only the 2 uppercase characters in this line would be small caps (W and I).
These kinds of words might be trouble with the regex: A.V., T.V., A, I, O'Brien, O'BRIEN, MAdeUPwoRD, so be sure to take a close look over your document and handle any special cases you have. This gives a little less verbosity and will only ever change uppercase letters to small caps (which may be a help or a hindrance, depending on your needs). And I would use the highlight function while drafting (i.e., use |
Beta Was this translation helpful? Give feedback.
OpenType Features
If your font implements the OpenType features, you could use the combination of
"smcp"
and"c2sc"
in thefeatures
parameter fortext
.E.g., you could use
Enabling
"smcp"
is whatsmallcaps
does behinds the scenes. And"c2sc"
is an OpenType feature that substitutes capital letters with small caps. If you only want uppercase letters in small caps, you could use("c2sc",)
instead of("smcp", "c2sc",)
.Regex
You might also be able to simplify your Regex string from
"([A-Z]{2,})+"
to"[A-Z]{2,}"
. I'm not particularly proficient …