# Split string variable by spaces

I am updating a custom latex class but I am a newbie with Latex and I need to parse a command like that \author{First Middle Last} into a new one \author{Last}{First Middle}. I cannot provide the \author{First Middle Last} directly in the format \author{Last}{First Middle} since I have no control over the backend that generates it. I took a look on the stringstrings package, but I am not sure about how I can use it here.

I've also tried something like that \let\auxauthor\author \renewcommand\author[1]{\auxauthor{#2}{#1}}, but certainly I'm ignoring some important detail.

• Welcome to TeX.SX! Perhaps this helps: tex.stackexchange.com/a/104469/134574 Nov 20 '18 at 13:13
• Welcome to TeX.SX! If you use a document class that requires \author{Last}{First Middle} it is necessary to know about it. Nov 20 '18 at 13:13
• As the author of stringstrings, I recommend using it only as a last resort. Instead, here, I think listofitems package can help: \setsepchar{ } \readlist\nameparts{First Middle Last} \author{\nameparts[3]}{\nameparts[1] \nameparts[2]} Nov 20 '18 at 13:14
• Thank you guys. For now, I will use listofitems as suggested by @StevenB.Segletes.
– GP.
Nov 20 '18 at 15:22

I assume you are using a document class that requires authors are specified with

\author{Last}{First Middle}


It would be better to know the class, so as to make the experiment on the real example. Anyway, here's how you could do.

I use the article class, but fake yours by redefining the \author command and also defining a \useauthor for testing; the class will probably use the data in a different way, but it should be unimportant.

\documentclass{article}

% let's emulate what the (unknown) used class does
\makeatletter
\def\author#1#2{\gdef\@authorlast{#1}\gdef\@authorfirstmiddle{#2}}
\def\useauthor{\@authorlast, \@authorfirstmiddle} % for testing
\makeatother

% your modification should go in the preamble, before any \author command is used
\makeatletter
\let\class@author\author
\renewcommand{\author}[1]{%
% split the data
\def\gp@firstmiddle{}%
\gp@divide{#1}%
}
\newcommand\gp@divide[1]{%
\gp@@divide#1 \@nil
}
\def\gp@@divide#1 #2\@nil{%
\if\relax\detokenize{#2}\relax
\def\gp@last{#1}%
\expandafter\@firstoftwo
\else
\ifx\gp@firstmiddle\@empty
\def\gp@firstmiddle{#1}%
\else
\edef\gp@firstmiddle{\unexpanded\expandafter{\gp@firstmiddle} \unexpanded{#1}}%
\fi
\expandafter\@secondoftwo
\fi
{\gp@author}%
{\gp@@divide#2\@nil}%
}
\def\gp@author{%
\expandafter\expandafter\expandafter\class@author
\expandafter\expandafter\expandafter
{\expandafter\gp@last\expandafter}\expandafter{\gp@firstmiddle}%
}
\makeatother

\begin{document}

\author{First Middle Last}

\useauthor

\author{First Last}

\useauthor

\author{Last}

\useauthor

\end{document}


A shorter code:

\documentclass{article}

% let's emulate what the (unknown) used class does
\makeatletter
\def\author#1#2{\gdef\@authorlast{#1}\gdef\@authorfirstmiddle{#2}}
\def\useauthor{\@authorlast, \@authorfirstmiddle} % for testing
\makeatother

\usepackage{xparse}

\ExplSyntaxOn
\cs_set_eq:NN \gp_author_class:nn \author

\RenewDocumentCommand{\author}{m}
{
\seq_set_split:Nnn \l_tmpa_seq { ~ } { #1 }
\seq_pop_right:NN \l_tmpa_seq \l_tmpa_tl % last
\use:x
{
\exp_not:N \gp_author_class:nn
{ \exp_not:V \l_tmpa_tl }
{ \seq_use:Nn \l_tmpa_seq { ~ } }
}
}
\ExplSyntaxOff

\begin{document}

\author{First Middle Last}

\useauthor

\author{First Last}

\useauthor

\author{Last}

\useauthor

\end{document}