Make newlines expand to custom command
One slight speedbump here is that you have linebreaks within your arguments as well, and of course you dn't want them to turn into \guest commands as well.
But if you're not doing this as a programming exercise, I'd recommend looking at the datatool package which lets you do things with CSV files, so you'd need a guest file that looks like
Obi-Wan Kenobi
Luke Skywalker
Han Solo;Princess Leia
or similar.
Apparently my first answer was based on a (partial?) misunderstanding of the question, so here is take 2. No external file this time.
\documentclass{article}
% this is just for demonstration purposes:
\newcommand{\guest}[1]{{\message{[Inviting: #1]}}}
\begin{document}
\begingroup\obeylines%
\def\doguests#1^^M{%
\ifx\relax#1\relax\else%
\guest{#1}\expandafter\doguests\fi}%
\def^^M{ and }\doguests%
Luke Skywalker
Obi-Wan Kenobi
{Han Solo
Princess Leia}
\endgroup
\end{document}
As Ulrich says, it is best to use a package. However, if the package isn't easily made to do what you want, you can roll your own:
\documentclass{article}
% this is just for demonstration purposes:
\newcommand{\guest}[1]{{\let\\\relax\message{[Inviting: #1]}}}
\newread\guests
\newcommand{\readguests}
{\read\guests to \invitee
\ifeof\guests\else
\guest{\invitee}\expandafter\readguests\fi
}
\begin{document}
\openin\guests=guests.list
{\endlinechar=-1 \readguests}
\closein\guests
\end{document}
Edit: I should perhaps have added a bit of explanation. Without \endlinechar=-1
, each read would include a space token at the end. More importantly, the final read would return a \par
token instead of being empty, so the check for end-of-file would have to be changed.
Edit 2: Used the \ifeof
primitive to check for end of file. Note the somewhat nonintuitive placement of this after the \read
, though: At the end of file, the empty token list will be read in, and only afterwards does \ifeof
become true.