Is there a way in tex to test if an argument starts with some string or not?
And then extract the text that follows that start?
As an example, I want to define a command that makes a text bold if it starts with my
and italic otherwise.
\transform{my big world}
% Returns "\textbf{big world}".
\transform{our small world}
% Returns "\textit{small world}".
\transform{your world}
% Returns \textit{world}
% It would be great if the test is case insensitive
\transform{My world}
% Returns \textbf{world}
If possible, I prefer a method not using expl3 (I don't understand it enough to tinker with it).
The answers here gobbles a single character, which I can use for every character of my string. But that would get unwieldy rather quickly for long strings.
Best Answer
If your first word is always separated from the rest by a space, you can do with the following code:
The first word is isolated and its lowercase version is matched with
my
.Since you want a colon, rather than a space, it's possible to manage the absence of a prefix.
An
expl3
version, which is also (almost) fully expandable (it is not really because of\textit
and\textbf
, but we can use\text_expand:n
to take care of that.