| 1 |
\def\filename{bitelist} \def\filedate{2012/03/29} |
| 2 |
\def\fileversion{v0.1} \def\fileinfo{split lists in TeX's mouth (UL)} |
| 3 |
%% Copyright (C) 2012 Uwe Lueck, |
| 4 |
%% http://www.contact-ednotes.sty.de.vu |
| 5 |
%% -- author-maintained in the sense of LPPL below -- |
| 6 |
%% |
| 7 |
%% This file can be redistributed and/or modified under |
| 8 |
%% the terms of the LaTeX Project Public License; either |
| 9 |
%% version 1.3c of the License, or any later version. |
| 10 |
%% The latest version of this license is in |
| 11 |
%% http://www.latex-project.org/lppl.txt |
| 12 |
%% There is NO WARRANTY - this rather is somewhat experimental. |
| 13 |
%% |
| 14 |
%% Please report bugs, problems, and suggestions via |
| 15 |
%% |
| 16 |
%% http://www.contact-ednotes.sty.de.vu |
| 17 |
%% |
| 18 |
%% === Proceeding without \LaTeX === |
| 19 |
%% Some tricks from Bernd Raichle's \CtanPkgRef{ngerman}{ngerman.sty}---I |
| 20 |
%% need \LaTeX's `\Provides'\-`Package' for \ctanpkgref{fileinfo}, |
| 21 |
%% my package version tools. With 'readprov.sty', it issues `\endinput', |
| 22 |
%% close conditional before: |
| 23 |
\begingroup\expandafter\expandafter\expandafter\endgroup |
| 24 |
\expandafter\ifx\csname ProvidesPackage\endcsname\relax \else |
| 25 |
\edef\fileinfo{\noexpand\ProvidesPackage{\filename}% |
| 26 |
[\filedate\space \fileversion\space \fileinfo]} |
| 27 |
\expandafter\fileinfo |
| 28 |
\fi |
| 29 |
\chardef\atcode=\catcode`\@ |
| 30 |
\catcode`\@=11 % \makeatletter |
| 31 |
%% Providing \LaTeX's `\@firstoftwo' and `\@secondoftwo': |
| 32 |
\long\def\@firstoftwo #1#2{#1} |
| 33 |
\long\def\@secondoftwo#1#2{#2} |
| 34 |
%% |
| 35 |
%% === Basic Parsing (No Braces) === |
| 36 |
%% |
| 37 |
%% |\BiteMake{<def>}{<cmd>}{<find>}| provides the parameter text |
| 38 |
%% (\TeX book p.~203) for defining (by <def>) a macro <cmd> that will |
| 39 |
%% search for <find>: |
| 40 |
\def\BiteMake#1#2#3{#1#2##1#3##2\BiteSep##3\BiteStop} |
| 41 |
%% With |\BiteFindByIn{<find>}{<cmd>}{<list>}|, |
| 42 |
%% you can use a <cmd> (perhaps defined by &\BiteMake) |
| 43 |
%% in order to search <find> in <list>. |
| 44 |
%% This is expandable as promised: |
| 45 |
\def\BiteFindByIn#1#2#3{% |
| 46 |
#2#3\BiteSep#1\BiteCrit\BiteSep\BiteStop} |
| 47 |
%% Preparing a possible &\edef as <def>: |
| 48 |
\let\BiteSep\relax \let\BiteStop\relax |
| 49 |
%% And this is important in any case for correct testing of |
| 50 |
%% occurrence:\footnote{The idea for the ``funny `Q'" |
| 51 |
%% is from the \ctanpkgref{ifmtarg} package.} |
| 52 |
\catcode`\Q=7 \let\BiteCrit=Q \catcode`\Q=11 |
| 53 |
%% Perhaps you could increase safety of tests by using something similar to the funny `Q' |
| 54 |
%% for &\BiteSep and &\BiteStop. |
| 55 |
%% %% 2012/03/28: |
| 56 |
%% However, this would additionally require reimplementation of |
| 57 |
%% the macros for keeping braces (Section~\ref{sec:braces}) using `\edef'. |
| 58 |
%% % It appears to me, however, that expandable tests like this one |
| 59 |
%% % never are perfectly safe; you only can say that it is safe with a |
| 60 |
%% % source meeting certain conditions. \ctanpkgref{fifinddo} originally |
| 61 |
%% % was made for ``plain text," to be read from files without assigning |
| 62 |
%% % \TeX's special category codes. \emph{Here} we assume that the source |
| 63 |
%% % (text in \cs{Provides.\empty..} arguments) will never contain such a |
| 64 |
%% % ``funny `Q'". |
| 65 |
%% |
| 66 |
%% === Simple Conditionals === |
| 67 |
%% By |\BiteMakeIfOnly{<def>}{<cmd>}{<find>}|, you can make a command <cmd> |
| 68 |
%% that with |
| 69 |
%% \[|\BiteFindByIn{<find>}{<cmd>}{<list>}{<yes>}{<no>}|\] |
| 70 |
%% chooses <yes> if <find> occurs in <list> and <no> otherwise. |
| 71 |
\def\BiteMakeIfOnly#1#2#3{\BiteMake{#1}{#2}{#3}{\BiteIfCrit{##2}}} |
| 72 |
%% |\BiteIfCrit{<suffix>}{<yes>}{<no>}| |
| 73 |
%% is the basic test for occurrence of <find> in <list>: |
| 74 |
\def\BiteIfCrit#1{\ifx\BiteCrit#1\expandafter\@secondoftwo |
| 75 |
%% If <cmd>'s second argument---same as &\BiteIfCrit's first argument---is |
| 76 |
%% empty, &\BiteCrit is compared with &\expandafter, so <yes> is chosen. |
| 77 |
%% That is correct, it happens when <find> is a suffix of <list>. |
| 78 |
\else \expandafter\@firstoftwo \fi } |
| 79 |
%% |
| 80 |
%% === Passing Results Completely---No Braces === |
| 81 |
%% So the previous `\BiteMakeIfOnly' generates pure tests on occurrence, |
| 82 |
%% giving away information about prefix and suffix. |
| 83 |
%% It may be considered a didactical step fostering understanding of the following. |
| 84 |
%% % \medskip\noindent |
| 85 |
%% % ** Generic Fundamental Splitter---No Braces ** |
| 86 |
%% %% Wrong: |
| 87 |
%% % With the above `\BiteMakeIfOnly', the user can choose on her own |
| 88 |
%% % information about the composition of <list> to use. |
| 89 |
%% % Perhaps it is not easy to understand. |
| 90 |
%% % `\BiteMakeIfOnlySplit{<def>}{<cmd>}{<find>}' by contrast, |
| 91 |
%% % should satisfy ``all needs" by providing \emph{all} the information |
| 92 |
%% % about splitting a <list>. It may also be a template for using |
| 93 |
%% % `\BiteMakeIfOnly'. |
| 94 |
%% When, by contrast \[|\BiteMakeIf{<def>}{<cmd>}{<find>}|\] |
| 95 |
%% has been issued, a later |
| 96 |
%% $$|\BiteFindByIn{<find>}{<cmd>}{<list>}{<list>}{<yes>}{<no>}|\eqno(*)$$ |
| 97 |
%% will expand to |
| 98 |
%% \[`<yes>{<prefix>}{<find>}{<suffix>}'\] |
| 99 |
%% if <list> is composed as <prefix><find><suffix> |
| 100 |
%% and <prefix> is the shortest $\alpha$ such that there is some $\beta$ |
| 101 |
%% with $<list>=\alpha<find>\beta$. Otherwise, $(*)$ will expand to |
| 102 |
%% \[`<no>{<list>}'\] |
| 103 |
%% This gives all the information available. |
| 104 |
%% For actual applications, it may be too much, and the macro programmer |
| 105 |
%% may do something in between of `\BiteMakeIfOnly' and `\BiteMakeIf': |
| 106 |
\def\BiteMakeIf#1#2#3{% |
| 107 |
\BiteMake{#1}{#2}{#3}##4##5##6{% |
| 108 |
%% In the replacement text, we first do the same as with `\BiteMakeIfOnly': |
| 109 |
\BiteIfCrit{##2}% |
| 110 |
%% What follows is new. <cmd>'s third argument is ignored. |
| 111 |
%% The fourth keeps the original <list>. |
| 112 |
%% <yes> is <cmd>'s fifth and <no> is its sixth argument. |
| 113 |
{##5{##1}{#3}{##2}}% %% if #3 in ##4 |
| 114 |
{##6{##4}}% %% otherwise |
| 115 |
}% |
| 116 |
} |
| 117 |
%% In $(*)$, <list> has been doubled. That was no mistake. |
| 118 |
%% It is due to a shortcoming of `\BiteFindByIn'. |
| 119 |
%% With |
| 120 |
%% \[|\BiteFindByInIn{<find>}{<cmd>}{<list>}{<yes>}{<no>}|\] |
| 121 |
%% you get the same result as with $(*)$: |
| 122 |
\def\BiteFindByInIn#1#2#3{\BiteFindByIn{#1}{#2}{#3}{#3}} |
| 123 |
%% TODO not sure about command names yet |
| 124 |
%% |
| 125 |
%% == Example Applications == |
| 126 |
%% === Splitting at Space === |
| 127 |
%% \label{sec:space} |
| 128 |
%% This work actually arose from modifying `\GetFileInfo' |
| 129 |
%% as provided by \LaTeX's \ctanpkgref{doc} package |
| 130 |
%% so that it would deal reasonably with ``incomplete" file info---for |
| 131 |
%% the \ctanpkgref{nicefilelist} package. |
| 132 |
%% `\GetFileInfo' works best when the file info contains |
| 133 |
%% at least \emph{two} blank spaces. But how many are there indeed?---And |
| 134 |
%% I wanted to do it \emph{expandably:} while `\GetFileInfo' issues |
| 135 |
%% \emph{definitions} of `\filedate', `\fileversion', and `\fileinfo', |
| 136 |
%% date, version, and info should be passed as \emph{macro arguments}. |
| 137 |
%% \medbreak\noindent |
| 138 |
%% |\BiteIfSpace| tries splitting at the next blank space passes results: |
| 139 |
\BiteMake{\def}{\BiteIfSpace}{ }#4#5#6{% |
| 140 |
\BiteIfCrit{#2}{#5{#1}{#2}}{#6{#4}}} |
| 141 |
%% The difference to the `\BiteMakeIf' construction is that we do not |
| 142 |
%% pass <find>, the space---it's not essential. |
| 143 |
%% (TODO names may change ...) |
| 144 |
%% |
| 145 |
%% Now \[|\BiteFindByInIn{ }{\BiteIfSpace}{<list>}{<yes>}{<no>}|\] |
| 146 |
%% will pass prefix/suffix to <yes> or <list> to <no>. |
| 147 |
%% If this is needed frequently, here is a shorthand |
| 148 |
%% |\BiteGetNextWord{<list>}{<yes>}{<no>}|: |
| 149 |
\def\BiteGetNextWord{\BiteFindByInIn{ }\BiteIfSpace} |
| 150 |
%% See a test in `bitedemo.tex' (Section~\ref{sec:demo}). |
| 151 |
%% |
| 152 |
%% === Splitting at Comma === |
| 153 |
%% ... left as an exercise to the reader ... |
| 154 |
%% |
| 155 |
%% == Keeping Braces: Reasoning == |
| 156 |
%% \label{sec:braces} |
| 157 |
%% Now we want to generalize task (Section~\ref{sec:task}) |
| 158 |
%% and solution (Section~\ref{sec:trick}) for the case that |
| 159 |
%% $\tau=<list>$ has (balanced) braces |
| 160 |
%% (with category codes for argument delimiters), |
| 161 |
%% while $\sigma=<find>$ still has not (does not work with our method). |
| 162 |
%% So with $\tau=\alpha\sigma\beta$, |
| 163 |
%% $\alpha$ (``prefix") or $\beta$ (``suffix") or both |
| 164 |
%% may contain braces. But we consider another restriction: |
| 165 |
%% braces must be balanced in $\alpha$ and in $\beta$, |
| 166 |
%% we don't try parsing inside braces |
| 167 |
%% (as opposed to the search for asterisks in Appendix~D |
| 168 |
%% of The~\TeX book). |
| 169 |
%% |
| 170 |
%% According to \TeX book p.~204, when a macro <cmd> finds an argument |
| 171 |
%% formed as `{<tokens>}', in <cmd>'s replacement text only <tokens> |
| 172 |
%% is used, i.e., outer braces are removed. |
| 173 |
%% So when $\alpha=`{<tokens>}'$, a parser <cmd> as defined by our |
| 174 |
%% methods above will return <tokens> instead of `{<tokens>}'---likewise |
| 175 |
%% for $\beta$. We are now trying to \emph{keep} outer braces in prefix/suffix |
| 176 |
%% by a more elaborate method. |
| 177 |
%% |
| 178 |
%% The idea is to present $\tau=<list>$ with context\footnote{Perhaps |
| 179 |
%% I am confusing `&\empty' and the token list containing just `&\empty' here?} |
| 180 |
%% \[`<cmd>\empty<list><stop><sep><find><crit><sep><stop>'\] |
| 181 |
%% or in the notation of Section~\ref{sec:trick} |
| 182 |
%% \[`<cmd>\empty'\tau\theta\zeta\sigma\rho\zeta\theta\] |
| 183 |
%% Then, if <find> occurs in <list>, we must remove the `\empty' |
| 184 |
%% from the prefix that we get with the earlier method (easy) |
| 185 |
%% and <stop> from the suffix (tricky, similar problem recurs). |
| 186 |
%% Using old $\theta$ for a new purpose works here because |
| 187 |
%% <cmd> will look for $\theta$ only when it has found $\zeta$ before. |
| 188 |
%% |
| 189 |
%% Mere testing for occurrence is not affected. |
| 190 |
%% \[`\BiteMakeIfOnly' \quad \mbox{and} \quad `\BiteFindByIn'\] |
| 191 |
%% still can be used. |
| 192 |
%% We provide an improved version of |
| 193 |
%% \[`\BiteMakeIf' \quad (`\BiteMakeIfBraces')\] and of |
| 194 |
%% \[`\BiteFindInIn' \quad (`\BiteFindInBraces').\] |
| 195 |
%% |
| 196 |
%% == Implementation Part II == |
| 197 |
%% === Keeping Braces === |
| 198 |
%% \[|\BiteFindByInBraces{<find>}{<cmd>}{<list>}{<yes>}{<no>}|\] |
| 199 |
%% varies `\BiteFindByInIn' according to the previous: |
| 200 |
\def\BiteFindByInBraces#1#2#3{% |
| 201 |
#2\empty#3\BiteStop\BiteSep#1\BiteCrit\BiteSep\BiteStop{#3}} |
| 202 |
%% Such a <cmd> can be made by |\BiteMakeIfBraces{<def>}{<cmd>}{<find>}|: |
| 203 |
\def\BiteMakeIfBraces#1#2#3{% |
| 204 |
\BiteMake{#1}{#2}{#3}##4##5##6{% |
| 205 |
\BiteIfCrit{##2}% |
| 206 |
%% <no> works as before. For <yes>, first the `\empty' in the prefix |
| 207 |
%% is expanded for vanishing. |
| 208 |
%% `\BiteTidyI' and `\BiteTidyII' continue tidying. |
| 209 |
{\expandafter \BiteTidyI %% if #3 in ##4 |
| 210 |
\expandafter{##1}% %% prefix |
| 211 |
%% Another `\empty' avoids that removal of `\BiteStop' in suffix |
| 212 |
%% by `\BiteTideII' removes outer braces: |
| 213 |
{\BiteTidyII\empty##2}% %% suffix |
| 214 |
{#3}% %% find |
| 215 |
{##5}}% %% yes |
| 216 |
{##6{##4}}% %% otherwise |
| 217 |
}% |
| 218 |
} |
| 219 |
%% |\BiteTidyI{<prefix>}{<suffix>}| \ first expands `\BiteTidyII' |
| 220 |
%% for removing `\BiteStop' in <suffix>. |
| 221 |
%% `\empty' from `\BiteFindByInBraces' remains and is expanded next |
| 222 |
%% for vanishing. Finally, `\BiteTidied' reorders arguments |
| 223 |
%% for operation of <yes>: |
| 224 |
\def\BiteTidyI#1#2{% |
| 225 |
\expandafter\expandafter\expandafter \BiteTidied |
| 226 |
\expandafter\expandafter\expandafter {#2}{#1}} |
| 227 |
\def\BiteTidyII#1\BiteStop{#1} |
| 228 |
\def\BiteTidied#1#2#3#4{#4{#2}{#3}{#1}} |
| 229 |
%% |
| 230 |
%% === Leaving the Package File === |
| 231 |
\catcode`\@=\atcode |
| 232 |
\endinput |
| 233 |
%% |
| 234 |
%% === VERSION HISTORY === |
| 235 |
|
| 236 |
v0.1 2012/03/26 started |
| 237 |
2012/03/27 continued, restructured |
| 238 |
2012/03/28 continued, separate sections for "Mere Occurrence" |
| 239 |
vs. ...; keeping braces, \BiteIfCrit |
| 240 |
2012/03/29 proceeding without LaTeX corrected, restructured |