Here's a solution in the "old style":
\documentclass[a4paper]{article}
\def\director{\begingroup\def\and{and }\dodirector}
\def\dodirector#1{%
\def\theargument{#1}\dodirectori#1\and\dodirector\endgroup}
\def\dodirectori#1\and#2\dodirector{%
\if\relax\detokenize{#2}\relax % no \and
Director: #1%
\else
Directors: \theargument
\fi}
\begin{document}
\director{John}
\director{John \and Paul}
\end{document}
If only and
is preferred, then a shorter code suffices
\def\director#1{\def\next{#1}\dodirector#1 and \dodirector}
\def\dodirector#1 and #2\dodirector{%
\if\relax\detokenize{#2}\relax % no \and
Director: \next
\else
Directors: \next
\fi}
The input would be
\director{John}
\director{John and Paul}
A "new style" solution (with expl3
)
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\director}{ m }
{
\ignasi_print_director:n { #1 }
}
\cs_new_protected:Npn \ignasi_print_director:n #1
{
\seq_set_split:Nnn \l_ignasi_director_seq { \and } { #1 }
\seq_pop_left:NN \l_ignasi_director_seq \l_ignasi_temp_tl
\seq_if_empty:NTF \l_ignasi_director_seq
{ Director: ~ \tl_use:N \l_ignasi_temp_tl }
{
Directors: ~ \tl_use:N \l_ignasi_temp_tl
\ignasi_andify:
}
}
\cs_new_protected:Npn \ignasi_andify:
{
\seq_pop_right:NN \l_ignasi_director_seq \l_ignasi_temp_tl
\seq_map_inline:Nn \l_ignasi_director_seq {, ~ ##1}
% \seq_if_empty:NF \l_ignasi_director_seq {,} % uncomment for ", and" style
\c_space_token and ~ \tl_use:N \l_ignasi_temp_tl
}
\seq_new:N \l_ignasi_director_seq
\tl_new:N \l_ignasi_temp_tl
\ExplSyntaxOff
\begin{document}
\director{John}
\director{John \and Paul}
\director{John \and Paul \and George}
\director{John \and Paul \and George \and Ringo}
\end{document}
This will output
Director: John
Directors: John and Paul
Directors: John, Paul and George
Directors: John, Paul, George and Ringo
Uncommenting the line in the last definition will add a comma before "and" in case of three or more directors (American style).