| 1 |
\ProvidesFile{bitelist.tex}[2012/03/29 documenting bitelist.sty] |
| 2 |
\title{\textsf{\huge bitelist.sty %% \huge 2012/03/19 |
| 3 |
}\\---\\``Splitting" a List at a List Inside |
| 4 |
\\in \TeX's Mouth\thanks{This |
| 5 |
document describes version |
| 6 |
\textcolor{blue}{\UseVersionOf{\jobname.sty}} |
| 7 |
of \textsf{\jobname.sty} as of \UseDateOf{\jobname.sty}.}} |
| 8 |
% \listfiles |
| 9 |
{ \RequirePackage{makedoc} \ProcessLineMessage{} |
| 10 |
\MakeJobDoc{17} |
| 11 |
{\SectionLevelTwoParseInput} } |
| 12 |
\documentclass[fleqn]{article}%% TODO paper dimensions!? |
| 13 |
\input{makedoc.cfg} %% shared formatting settings |
| 14 |
% \ReadPackageInfos{bitelist} |
| 15 |
\usepackage{bitelist} |
| 16 |
\sloppy |
| 17 |
\MDkeywords{macro programming, text filtering, substrings} |
| 18 |
\begin{document} |
| 19 |
\maketitle |
| 20 |
\begin{MDabstract} |
| 21 |
'bitelist.sty' provides commands for ``splitting" a token list |
| 22 |
at the first occurrence of a contained token list |
| 23 |
(i.e., for given $\sigma$, $\tau$, |
| 24 |
return $\beta$ and shortest $\alpha$ s.t.\ $\tau=\alpha\sigma\beta$). |
| 25 |
As opposed to other packages providing similar features, |
| 26 |
\ (\textit{i})\enspace the method uses \TeX's mechanism of reading |
| 27 |
delimited macro parameters; |
| 28 |
\ (\textit{ii})\enspace the splitting macros work by pure expansion, |
| 29 |
without assignments, provided the macro doing the search has been |
| 30 |
defined before processing (e.g., a file); |
| 31 |
\ (\textit{iii})\enspace instead of using one macro for a ``substring" |
| 32 |
test and another one to replace the ``substring"---which includes |
| 33 |
extracting corresponding prefix and suffix---, |
| 34 |
the \emph{same} macro that detects the occurrence returns |
| 35 |
the split; |
| 36 |
\ (\textit{iv})\enspace |
| 37 |
\httpref{ctan.org/pkg/e-tex}{$\varepsilon$\hbox{-}\TeX} is not required. |
| 38 |
\ (And \LaTeX\ is not required.) |
| 39 |
|
| 40 |
This improves the author's \CtanPkgRef{fifinddo}{fifinddo.sty} |
| 41 |
(v0.51---and may once be used there). An elaborated approach |
| 42 |
(additionally to a simpler one) is provided that does not loose |
| 43 |
outer braces of prefix/suffix. |
| 44 |
|
| 45 |
``Substring" detection and ``string" replacement are (implicitly) included |
| 46 |
with respect to certain representations of characters by tokens. |
| 47 |
Counting occurrences and ``global" replacement could be achieved |
| 48 |
by applying the operation to earlier results, etc.---so |
| 49 |
this approach seems to be ``fundamental" for a certain larger |
| 50 |
set of list analysis tasks. |
| 51 |
|
| 52 |
The documentation aims to prove the correctness of the methods |
| 53 |
with mathematical rigour. |
| 54 |
\par\smallskip\noindent |
| 55 |
\strong{Related packages:}\quad |
| 56 |
\ctanpkgref{datatool}, \ctanpkgref{stringstrings}, \ctanpkgref{ted}, |
| 57 |
\ctanpkgref{texapi}, \ctanpkgref{xstring} |
| 58 |
\end{MDabstract} |
| 59 |
\newpage |
| 60 |
\tableofcontents |
| 61 |
|
| 62 |
\section{Task, Background Reasoning, and Usage} |
| 63 |
\subsection{The Task Quite Precisely} |
| 64 |
\label{sec:task} |
| 65 |
|
| 66 |
Perhaps I should not have written ``splitting" before, |
| 67 |
see Section~\ref{sec:name} why I did so though. |
| 68 |
Actually: |
| 69 |
|
| 70 |
At first we are dealing with token lists $\tau$ and $\sigma$ |
| 71 |
without braces |
| 72 |
(unless their category code has been changed appropriately) |
| 73 |
that can be stored as macros without parameter or in token list registers. |
| 74 |
We want to find out whether $\tau$ contains $\sigma$ (``as a subword") |
| 75 |
in the sense that there are such token lists $\alpha$ and $\beta$ that |
| 76 |
$\tau$ is composed as $\alpha\sigma\beta$, i.e., |
| 77 |
\[\tau=\alpha\sigma\beta\] |
| 78 |
and in this case |
| 79 |
we want to get $\alpha$ and $\beta$ of this kind with |
| 80 |
$\alpha$ being the \emph{shortest} possible. |
| 81 |
I.e., if there are such $\gamma$ and $\delta$ that $\tau$ |
| 82 |
is composed as $\gamma\sigma\delta$, $\alpha$ must be contained |
| 83 |
as a ``prefix" in $\gamma$, |
| 84 |
i.e., $\gamma$ is composed as $\alpha\eta$ for some token list $\eta$. |
| 85 |
The token lists $\alpha$, $\beta$, $\gamma$, $\delta$, $\eta$, |
| 86 |
$\sigma$, and $\tau$ are allowed to be empty throughout. |
| 87 |
|
| 88 |
The task will be extended for some braces in Section~\ref{sec:braces}. |
| 89 |
|
| 90 |
\subsection{Idea of Solution} |
| 91 |
|
| 92 |
\TeX's mechanism of expanding macros (\TeX book Chapter~20) |
| 93 |
at least has a built-in mechanism to return such $\alpha$ and $\beta$ |
| 94 |
\emph{provided} $\tau$ contains $\sigma$. Define |
| 95 |
\[`\def<cmd>#1'\sigma`#2'\theta`{<replace-def>}'\] |
| 96 |
where $\theta$ must be a token list (maybe of a single token) |
| 97 |
that won't occur in $\tau$.\footnote{I am still following others in confusing |
| 98 |
source code and tokens. I have better ideas, but must expand on them |
| 99 |
elsewhere. Writing `&\def' rather indicates that it is source code, |
| 100 |
then $\sigma$ etc. should be replaced by strings that are converted |
| 101 |
into tokens $\sigma$ etc. |
| 102 |
<cmd> sometimes is a \emph{string} starting with an escape character, |
| 103 |
or it is an active character; but sometimes it rather is an ``active" |
| 104 |
\emph{token} converted from such an escape string or an active character.} |
| 105 |
This is a \strong{limitation} of the approach: |
| 106 |
It works for sets of such $\tau$ only that do not contain |
| 107 |
any of a small set of tokens or combinations of them. |
| 108 |
('bitelist' will use `\BiteSep', `\BiteStop', and `\BiteCrit', |
| 109 |
or any other three that can be chosen.) |
| 110 |
|
| 111 |
On the other hand, \TeX's \emph{category codes} |
| 112 |
(\TeX book Chapter~7) can ensure this quite well. |
| 113 |
E.g., we may assume that input ``letters" always have category code 11 |
| 114 |
(or 12, or one of them), and for $\theta$ we can choose letters |
| 115 |
with \emph{different} category codes such as 3. |
| 116 |
Without such tricks, you may often assume that nobody will input |
| 117 |
certain ``silly" commands such as `\BiteStop'. |
| 118 |
(But it may become difficult when you use a package for |
| 119 |
replacement macros for generating its own documentation \dots) |
| 120 |
|
| 121 |
With a <cmd> as defined above, \TeX\ will |
| 122 |
\[\mbox{expand\quad} |
| 123 |
`<cmd>'\tau\theta |
| 124 |
\quad\mbox{to}\quad |
| 125 |
<replace>,\] |
| 126 |
where <replace> will be the result of replacing |
| 127 |
\ (a)\enspace all occurrences of `#1' in <replace-def> by $\alpha$ as wanted and |
| 128 |
\ (b)\enspace all occurrences of `#2' in <replace-def> by $\beta$ as wanted. |
| 129 |
\ |
| 130 |
I.e., <cmd> returns $\alpha$ as its first argument and $\beta$ as its second argument. |
| 131 |
The reason is that <cmd>'s first parameter is delimited by $\sigma$ and the second one by |
| 132 |
$\theta$ in the sense of The~\TeX book p.~203. |
| 133 |
Our requirement to get the \emph{shortest} $\alpha$ for the composition of $\tau$ as |
| 134 |
$\alpha\sigma\beta$ is met because \TeX\ indeed looks for the \emph{first} occurrence of |
| 135 |
$\sigma$ at the right of <cmd>. |
| 136 |
|
| 137 |
|
| 138 |
\subsection{When We Don't Know \dots} |
| 139 |
When $\sigma$ does \emph{not} occur in $\tau$ and we present $\tau\theta$ to <cmd> as |
| 140 |
before, \TeX\ will throw an error saying |
| 141 |
``Use of <cmd> doesn't match its definition." |
| 142 |
When the purpose is ``substring detection" only, without returning $\beta$, |
| 143 |
many packages have solved the problem by issuing something like |
| 144 |
\[`<cmd>'\tau\sigma\theta\] |
| 145 |
Then (still provided $\theta$ does not occurr in $\tau$) |
| 146 |
<cmd>'s second argument is empty \emph{exactly} if $\sigma$ occurs in $\tau$. |
| 147 |
This method has, e.g., been employed in \LaTeX's internal &\in@ mechanism |
| 148 |
(e.g., for dealing with package options) and by the \ctanpkgref{substr} package. |
| 149 |
\ctanpkgref{datatool} has used the latter's substring test (for $\sigma$) |
| 150 |
before calling a macro for replacing |
| 151 |
($\sigma$ by another token list, perhaps thinking of character tokens). |
| 152 |
|
| 153 |
This way you get the wanted $\alpha$ as the first macro argument immediately indeed. |
| 154 |
An obstacle for getting $\beta$ is that <cmd>'s \emph{second} argument now contains |
| 155 |
an occurrence of $\sigma$ that is not an occurrence in $\tau$. |
| 156 |
In \CtanPkgRef{fifinddo}{fifinddo.sty} I didn't have a better idea than using |
| 157 |
another macro to remove the ``dummy text" from the second argument. |
| 158 |
I considered it an advantage as compared with 'datatool' that |
| 159 |
\emph{one} macro could do this for \emph{all} replacement jobs, |
| 160 |
while 'datatool' uses \emph{two} macros with $\sigma$ as a delimiter |
| 161 |
for each $\sigma$ to be replaced. |
| 162 |
|
| 163 |
But still, 'fifinddo' has used \emph{two} macros for each replacement, |
| 164 |
the extra one being for presenting $\tau$ to <cmd>, using a job identifier. |
| 165 |
This could be improved within 'fifinddo', but I could never afford |
| 166 |
to take the time for this. |
| 167 |
|
| 168 |
\subsection{The Trick} |
| 169 |
\label{sec:trick} |
| 170 |
|
| 171 |
The solution presented here is not very ingenious, |
| 172 |
many students would have found it in an exercise for a math course. |
| 173 |
My personal approach was looking at &\GetFileInfo from \LaTeX's |
| 174 |
\ctanpkgref{doc} package. There they try to get \emph{two} occurrences |
| 175 |
of a space token this way:\footnote{We are undoubling the hash marks |
| 176 |
inside the definition text of |
| 177 |
&\GetFileInfo.} |
| 178 |
\[`\def\@tempb#1 #2 #3\relax#4\relax{%'\] |
| 179 |
and &\@tempb is called as |
| 180 |
\[`\@tempb'\tau`\relax? ? \relax\relax'\] |
| 181 |
or with $\tau=<list>$ |
| 182 |
\[`\@tempb<list>\relax? ? \relax\relax'\] |
| 183 |
The final &\relax may not be removed, but for 'doc' it doesn't harm. |
| 184 |
It harms for \emph{me} when I don't want to have a `\relax' in a `.log' file list. |
| 185 |
`\empty' would be better, however \dots |
| 186 |
|
| 187 |
The idea is to use a \emph{three}-parameter macro for that \emph{single} occurrence |
| 188 |
of $\sigma$. We introduce a |
| 189 |
``dummy separator" $\zeta$ (or <sep>, `\BiteSep') |
| 190 |
between $\tau$ and the ``dummy text" and a |
| 191 |
``criterion" $\rho$ ($=<crit>$, `\BiteCrit') |
| 192 |
for determining occurrence of $\sigma$ ($=<find>$) in $\tau$ ($=<list>$). |
| 193 |
Neither $\zeta$ nor $\rho$ must occur in $\tau$. |
| 194 |
We will have definitions about as |
| 195 |
\[`\def<cmd>#1'\sigma`#2'\zeta`#3'\theta`{<replace-def>}'\] |
| 196 |
or |
| 197 |
\[`\def<cmd>#1<find>#2<sep>#3<stop>{<replace-def>}'\] |
| 198 |
and $\tau$ will be presented with context |
| 199 |
\[`<cmd>'\tau\zeta\sigma\rho\zeta\theta |
| 200 |
\quad\mbox{or}\quad |
| 201 |
<cmd><list><sep><find><crit><sep><stop> |
| 202 |
\] |
| 203 |
This ensures that <cmd> finds its parameter delimiters $\sigma$, $\zeta$, |
| 204 |
and $\theta$, in this order. $\sigma$ occurs in $\tau$ exactly if the second |
| 205 |
argument of <cmd> is $\rho$, and in this case the first occurrence |
| 206 |
of the second parameter delimiter $\zeta$ delimits $\tau$. |
| 207 |
Then <cmd>'s first argument is $\alpha$, and the second one is $\beta$, |
| 208 |
as wanted. |
| 209 |
|
| 210 |
<cmd>'s \emph{third} parameter is delimited by the final $\theta$ (`\BiteStop'). |
| 211 |
When $\sigma$ occurs in $\tau$, <cmd>'s third argument starts after the first |
| 212 |
of the two $\zeta$, so it is $\sigma\rho\zeta$. |
| 213 |
It is just ignored, this way <cmd> removes all the ``dummy" material |
| 214 |
after $\tau$. When $\sigma$ does \emph{not} occur in $\tau$, |
| 215 |
we ignore all of its arguments, and the macro that invoked <cmd> |
| 216 |
must decide what to do next, e.g., keeping $\tau$ elsewhere |
| 217 |
for presenting it to another parsing macro resembling <cmd>. |
| 218 |
|
| 219 |
|
| 220 |
\subsection{Installing and Calling} |
| 221 |
The file 'bitelist.sty' is provided ready, installation only requires |
| 222 |
putting it somewhere where \TeX\ finds it |
| 223 |
(which may need updating the filename data |
| 224 |
base).\urlfoot{ukfaqref}{inst-wlcf} %% corr. 2011/02/08 |
| 225 |
|
| 226 |
Below the `\documentclass' line(s) and above `\begin{document}', |
| 227 |
you load 'bitelist.sty' (as usually) by |
| 228 |
\begin{verbatim} |
| 229 |
\usepackage{bitelist} |
| 230 |
\end{verbatim} |
| 231 |
between the `\documentclass' line and `\begin{document}'; |
| 232 |
or by |
| 233 |
\begin{verbatim} |
| 234 |
\RequirePackage{bitelist} |
| 235 |
\end{verbatim} |
| 236 |
within a package file, or above or without the `\documentclass' line. |
| 237 |
Moreover, the package should work \emph{without} \LaTeX\ and may be |
| 238 |
loaded by |
| 239 |
\begin{verbatim} |
| 240 |
\input bitelist.sty |
| 241 |
\end{verbatim} |
| 242 |
Actually, using the package for macro programming requires understanding |
| 243 |
of pp.~20f.\ of The~\TeX book. On the other hand, the package may be loaded |
| 244 |
(without the user noticing it) automatically by a different package that |
| 245 |
uses programming tools from the present package. |
| 246 |
|
| 247 |
\section{Implementation Part I} |
| 248 |
\subsection{Package File Header (Legalize)} |
| 249 |
\input{bitelist.doc} |
| 250 |
|
| 251 |
\section{Examples/Tests} |
| 252 |
\label{sec:demo} |
| 253 |
You should find a separate file `bitedemo.tex' |
| 254 |
with examples. It may be run separately with `tex' |
| 255 |
(Plain \TeX)---demonstrating that 'bitelist' is ``\strong{generic}", |
| 256 |
then finish by entering `\bye'. |
| 257 |
With ```latex bitedemo.tex'", end the job by entering `\stop'. |
| 258 |
\strong{Expandability} is demonstrated by the `\BiteFind' commands |
| 259 |
running with `\typeout'. |
| 260 |
\medskip |
| 261 |
\noNiceVerb |
| 262 |
\hrule |
| 263 |
\verbatiminput{bitedemo.tex} |
| 264 |
\hrule |
| 265 |
\useNiceVerb |
| 266 |
|
| 267 |
\section{The Package's Name} |
| 268 |
\label{sec:name} |
| 269 |
|
| 270 |
This package deals with \TeX's expansion mechanism. |
| 271 |
In Knuth's metaphor, this is \TeX's mouth. |
| 272 |
I am not entirely sure, I have never understood it, |
| 273 |
or I have understood it only for a few days or hours. |
| 274 |
However, the package deals with ``Lists in \TeX's Mouth" |
| 275 |
as described in Alan Jeffrey's 1990 |
| 276 |
\tugbartref{tb11-2/tb28jeffrey}{\acro{TUG}boat paper} |
| 277 |
(Volume~11, No.~2, pp.~237--245).\foothttpurlref{% |
| 278 |
tug.org/TUGboat/tb11-2/tb28jeffrey.pdf} |
| 279 |
|
| 280 |
``Splitting" in title and abstract is an attempt to describe |
| 281 |
the package brief{}ly without speaking Mathematicalese. |
| 282 |
It roughly refers to certain \Wikienref{string functions} |
| 283 |
in various programming languages\foothttpurlref{% |
| 284 |
en.wikipedia.org/wiki/String\string_functions\string#split} |
| 285 |
with ```split'" in their name. |
| 286 |
However, there strings are splitted at separators such as commas. |
| 287 |
I am thinking here that a comma is a certain string ```,'", |
| 288 |
and this can be generalized to ``splitting" at any substring. |
| 289 |
With \TeX, the analogues are (a)~the token with the character code |
| 290 |
of the comma and category code 12, or the token list consisting of this |
| 291 |
single token,---and (b)~other lists of tokens~\dots |
| 292 |
|
| 293 |
Anyway, calling a triple $(\alpha,\sigma,\beta)$ of token lists |
| 294 |
such that $\tau=\alpha\sigma\beta$ a ``split" of $\tau$ |
| 295 |
is not necessarily a bad idea. |
| 296 |
Moreover, the blank space example (Section~\ref{sec:space}) |
| 297 |
is very close to the original idea of splitting at separators, |
| 298 |
a blank space is about as common as a separator as the comma is. |
| 299 |
|
| 300 |
Finally, according to \urlhttpref{en.wiktionary.org}, |
| 301 |
the Proto-Indo-European origin of |
| 302 |
\httpref{en.wiktionary.org/wiki/bite}{``to bite"} |
| 303 |
just means ``to split."\foothttpurlref{en.wiktionary.org/wiki/bite\string#Etymology} |
| 304 |
So in \TeX's mouth, splitting and biting is the same. |
| 305 |
|
| 306 |
|
| 307 |
\end{document} |
| 308 |
|
| 309 |
VERSION HISTORY |
| 310 |
|
| 311 |
2012/03/26 for v0.1 started |
| 312 |
2012/03/27 pages of motivation etc. |
| 313 |
2012/03/28 abstract: "mathematical rigour"; |
| 314 |
\section{Implementation}, \section{Task, ...}; |
| 315 |
\newpage, \LaTeX\; reference to sec:braces; |
| 316 |
"Examples/Tests" halfway; "Package's"; |
| 317 |
LaTeX not required, ... |
| 318 |
2012/03/29 "Implementation Part I", label sec:demo; |
| 319 |
keywords etc. |