5

In TeX I can define, say

\def\1#1 is #2.{#1 & is & #2.\\}

and

\def\2#1 has #2.{#1 & has & #2.\\}

with an easy usage: \1 Jim Jones is a fictional character. and \2 Tim Smith has two brothers.

This gives three columns of a tabular: Jim Jones, is, a fictional character. Similarly in the second case.

Can I write a definition depending on, in this case, is/has so I can write

\3 Jim Jones is a fictional character. and \3 Tim Smith has two brothers.

obtaing the previous results?

Solutions using Lua or expl3 are OK, but I hope that a pure TeX one is also possible.

4
  • 1
    At the TeX level for any \def you can have only one parameter text (the #1 is #2. and #1 has #2. in your \1 and \2 respectively). Mar 31, 2019 at 3:16
  • @ShreevatsaR That's why I am asking how to extend this possibility. Mar 31, 2019 at 3:36
  • @PrzemysławScherwentke What if \3 The third person singular form of have is has or \3 Be has is as the third person singular form? What should it produce? ;-)
    – L. F.
    Mar 31, 2019 at 5:43
  • 1
    @L.F. I have a limited number of sequences, with different words, so I know it will not happen. Mar 31, 2019 at 5:47

3 Answers 3

7

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}

enter image description here

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
8
  • Certainly +1. However, I will wait for alternative solutions. And I must reserve 3 days for reading your package. :-) Mar 31, 2019 at 3:10
  • 1
    @PrzemysławScherwentke Thanks. The package idea was mine, but the coding is all Christian's who made it come to life. Mar 31, 2019 at 3:14
  • 1
    @marmot I am not sure what you mean by "teach" in this context. However, look also at my tabstackengine package, which uses listofitems for parsing. It can digest matrices and remember all the cell content for future use. Mar 31, 2019 at 3:29
  • 1
    @marmot For example, \documentclass{article} \usepackage[T1]{fontenc} \usepackage{listofitems} \setsepchar{\\/&} \begin{document} \readlist*\myarray{x11&y12&z13\\x21&y22&z_{23}\\x31&y32&z33} Row 2, Col 3 = $\myarray[2,3]$; tokens \detokenize\expandafter\expandafter\expandafter{\myarray[2,3]} \end{document} Mar 31, 2019 at 3:41
  • 1
    @PrzemysławScherwentke listofitems was developed in response to my ill-fated ctan.org/pkg/getargs getargs package. It was a great idea, but just didn't do what needed to be done. Christian T. took that idea and turned it into listofitems. Mar 31, 2019 at 3:48
3

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
2

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

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .