How to write a definition with variants?
A listofitems
approach. Also, listofitems
is available in Plain TeX.
\documentclass{article}
\usepackage{listofitems}
\def\variant#1.{%
\setsepchar{is||has}%
\greadlist*\varinput{#1}
\varinput[1] & \varinputsep[1] & \varinput[2].
}
\begin{document}
\begin{tabular}{|c|c|c|}
\variant Jim Jones is a fictional character.\\
\variant Tim Smith has two brothers.
\end{tabular}
\end{document}
Similarly done in plain TeX
\input listofitems
\def\variant#1.{%
\setsepchar{is||has}%
\greadlist*\varinput{#1}
\varinput[1] \& \varinputsep[1] \& \varinput[2].
}
\variant Jim Jones is a fictional character.
\variant Tim Smith has two brothers.
\bye
The \replacestrings
macro from OPmac can be used:
%from OPmac code:
\bgroup \catcode`!=3 \catcode`?=3
\gdef\replacestrings#1#2{\long\def\replacestringsA##1#1{\def\tmpb{##1}\replacestringsB}%
\long\def\replacestringsB##1#1{\ifx!##1\relax \else\addto\tmpb{#2##1}%
\expandafter\replacestringsB\fi}% improved version <May 2016> inspired
\expandafter\replacestringsA\tmpb?#1!#1%
\long\def\replacestringsA##1?{\def\tmpb{##1}}\expandafter\replacestringsA\tmpb
}
\egroup
\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
%variant definition:
\def\variant #1.{\def\tmpb{#1}%
\replacestrings{ is }{ \tabsep is \tabsep }%
\replacestrings{ has }{ \tabsep has \tabsep }%
\tmpb
}
\def\tabsep{&}
%% test:
\halign{#\hfil\vrule\strut\ &#\hfil\vrule\ &# \hfil\cr
%
\variant Jim Jones is a fictional character. \cr
\variant Tim Smith has two brothers. \cr
}
\end
With expl3
, of course. ;-)
\input expl3-generic
\ExplSyntaxOn
\cs_new_protected:Npn \3 #1.
{
\tl_set:Nx \l_tmpa_tl { \tl_trim_spaces:n { #1 } }
\regex_replace_once:nnN { \s*(is|has)\s* } { \cT\& \1 \cT\& } \l_tmpa_tl
\l_tmpa_tl. \cr
}
\ExplSyntaxOff
\halign{#\hfil&\ \hfil#\hfil\ &# \hfil\cr
\3 Jim Jones is a fictional character and is funny.
\3 Tim Smith has two brothers.
}
\bye