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#1
in the function code) and the function code itself ({...}
). See xparse.pdf, p.7.\tl_set:Nn \l_tmpa_tl {#1}
:\tl_set:Nn
set the value of some variable (or "token list", hencetl
).N
andn
define the usual type of parameters for this function,N
standing for a single token andn
for a list of tokens between brackets.\l_tmpa_tl
is a standard name of local temporary assigned token list.- the first argument of
\regex_replace_all:nnN
is 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
).\0
stands 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_tl
simply display the resulting token.
range
from ctan.org/pkg/unicode-math.