The title pretty much says it all, I need a command to get the first word in a string.
Based on this answer to another question of mine, I tried this:
\documentclass{article}
\makeatletter
\newcommand\FirstWord[1]{\@firstword#1 \@nil}%
\newcommand\@firstword{}%
\def\@firstword#1 #2\@nil{#1\unskip}%
\makeatother
\begin{document}
\FirstWord{John, Paul, George and Ringo}
\end{document}
It almost works, except for the fact it includes the comma. I get:
John,
While I want just:
John
So how can I do that?
PS: Ideally, if more than one word is inside braces, they should count as one. So \FirstWord{{John, Paul}, George and Ringo}
should print "John, Paul".
Best Answer
You're almost there, just remove the trailing comma
You can add further tests for removing other delimiters
If you don't need expandability, you can use
l3regex
: