I would like to define a new mathmode command such that:
- the input is a string s;
- the output is the same string where the font of each caracter has been changed depending on whether the character is in lower case or upper case.
In pseudo-code, that would look like:
\newcommand{\cat}{
forEach (character char in #1) {
if (isLowerCase(char)) -> print \mathit{char}
else -> print \mathcal{char}
}
}
It seems I can use ifthenelse from the package ifthen for the if/else. I couldn't find how to:
- extract characters from string;
- loop for each of these characters;
- check whether a character is in lower case or not.
By the way, if anyone knows about a readable but nonetheless exhaustive reference on defining commands, I would appreciate (I could only find either too basic or "way over my head" references).
Thanks for your help!
PS: if anyone needs some context, I'm working in category theory, where the typical font for categories is \mathcal. Since lower case in mathcal is not defined, I need to find a way around it.
EDIT: I describe here my understanding of Ulrike Fischer's solution, as it might be useful to others.
\ExplSyntaxOn
\NewDocumentCommand\cat{m}
{
\tl_set:Nn\l_tmpa_tl {#1}
\regex_replace_all:nnN {[a-z]}{\c{mathit}\cB\{\0\cE\}}\l_tmpa_tl
\regex_replace_all:nnN {[A-Z]}{\c{mathcal}\cB\{\0\cE\}}\l_tmpa_tl
\l_tmpa_tl
}
\ExplSyntaxOff
ExplSyntaxOn...ExplSyntaxOff: change code régime where spaces are ignored and ":" and "_" are treated as letters. Necessary to access functions and variables. See interface3.pdf, p.7.\NewDocumentCommand \cat {m} {...}: create a new document-level function. You shall first describe the name (\cat), the set of parameters ({m}, one parameter which is refered as#1in the function code) and the function code itself ({...}). See xparse.pdf, p.7.\tl_set:Nn \l_tmpa_tl {#1}:\tl_set:Nnset the value of some variable (or "token list", hencetl).Nandndefine the usual type of parameters for this function,Nstanding for a single token andnfor a list of tokens between brackets.\l_tmpa_tlis a standard name of local temporary assigned token list.- the first argument of
\regex_replace_all:nnNis a list of tokens to be replaced, e.g.{[a-z]}. The second is what to do with these tokens, e.g.{\c{mathit}\cB\{\0\cE\}}.\c{mathit}is the equivalent of\mathit(for some reason, you can't just write\mathit).\0stands for everything that as been selected through the first argument. For some reason, it should be surrounded by\cB\{...\cE\}. Finally, the third argument is the token list on which the function is applied. - the last
\l_tpma_tlsimply display the resulting token.


rangefrom https://ctan.org/pkg/unicode-math. – CampanIgnis Dec 27 '19 at 18:11