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.

3 Answers3

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
  • Certainly +1. However, I will wait for alternative solutions. And I must reserve 3 days for reading your package. :-) – Przemysław Scherwentke Mar 31 '19 at 03:10
  • 1
    @PrzemysławScherwentke Thanks. The package idea was mine, but the coding is all Christian's who made it come to life. – Steven B. Segletes Mar 31 '19 at 03:14
  • That's really cool! (Note to my self: perhaps that allows one to write a nice coordinate parser for 3d vector operations?) –  Mar 31 '19 at 03:23
  • @marmot It is the ultimate nested parser. Here is the ultimate example of it: https://tex.stackexchange.com/questions/332012/translate-in-line-equations-to-tex-code-any-package/332061#332061 – Steven B. Segletes Mar 31 '19 at 03:24
  • Thanks! (TikZ already has a parser but I have a conceptual/bookkeeping problem. Did you by chance ever teach LaTeX matrix operations like a = matrix . b where a and b are, say, 3d vectors? ) –  Mar 31 '19 at 03:27
  • 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. – Steven B. Segletes Mar 31 '19 at 03: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} – Steven B. Segletes Mar 31 '19 at 03:41
  • 1
    @PrzemysławScherwentke listofitems was developed in response to my ill-fated https://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. – Steven B. Segletes Mar 31 '19 at 03: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
wipet
  • 74,238
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

egreg
  • 1,121,712