[texhax] (no subject) [xy -> x or y]

Uwe Lück uwe.lueck at web.de
Sun Jul 9 20:34:47 CEST 2006


At 10:08 09.07.06, Zalman Rubinstein wrote:
>
>I am a Plain TeX user ans while writing a macro for mathematical thesis 
>typing I have encountered the following problem:
>
>I have a string of tokens:
>
>\t={ab...xy.....cd....xy....ef....xy....}
>
>that is the string \t contains tokens and accasionally the pair "xy" 
>appears side by side.
>
>I need to modify the string \t as to erase from each couple  "xy" either 
>the token "x" or the token "y". The rest should remain unchanged.
>
>That is I nead to store in a token register, say \tt the token
>
>\tt={ab...y...cd...y...ef...y...}     or 
>\tt={ab...x...cd...x...ef...x...}.

`either ... or ...' may mean `... in one run and ... in another run'.
Please see the attached macro and demo file.
(Another replacing macro of mine.)

Hope this helps

-- Uwe Lueck.
-------------- next part --------------
%% plainrpl.tex -- replace with Plain TeX 
%% Uwe Lueck 2006/07/09 for Zalman Rubinstein on texhax. 

%% To use it as a mere package, replace `% \endinput' by 
%% `\endinput' below. 

%% Macros: 

\def\replaceInOutThisBy#1#2#3#4{% 
%% #1 input token register, #2 output token register, 
%% #3 string to replace,    #4 replacing string. 
%% #1 must not contain #3\RIOTBprivate! 
  #2{}% initiate output register 
  \def\RIOTBloop##1#3##2\RIOTBprivate{% 
%% #3 and \RIOTBprivate are used as parameter delimiters, 
%% cf. TeXbook p. 202f. 
    \ifx\RIOTBprivate##2\RIOTBprivate 
      %% this case: ##2 nothing, no more #3 in #1 -- finish
      #2\expandafter{\the#2##1}%
      \let\RIOTBloop\relax \let\RIOTBnext\RIOTBgobble 
    \else 
      %% #3 to be replaced by #4 has been found; 
      %% ##2 ends on dummy #3
      #2\expandafter{\the#2##1#4}% 
    \fi 
    \RIOTBnext ##2\RIOTBprivate 
  }% 
  \let\RIOTBnext\RIOTBloop 
  \expandafter #1\expandafter {\expandafter }\expandafter 
  %% <- empty #1 immediately after reading -- or want keep it? 
    \RIOTBloop\the#1#3\RIOTBprivate 
}
\def\RIOTBprivate{RIOTB}
\def\RIOTBgobble\RIOTBprivate{} 

% \endinput 

%% Examples: 

\newtoks\inputtoks \newtoks \outputtoks 
\def\RIOTBmessage#1#2{\immediate\write16{RIOTB #1: \the#2}}
\def\RIOTBemptyscreenline{\immediate\write16{}}

\RIOTBemptyscreenline

%% Reducing `xy' to `x': 

\inputtoks{ABxyCDExyFGHI}
\RIOTBmessage{ input}\inputtoks
%  \tracingmacros=1 
\replaceInOutThisBy\inputtoks\outputtoks{xy}{x} 
\RIOTBmessage{output}\outputtoks

\RIOTBemptyscreenline

%% Reducing `xy' to `y': 

\inputtoks{ABxyCDExyFGHI}
\RIOTBmessage{ input}\inputtoks
\replaceInOutThisBy\inputtoks\outputtoks{xy}{y} 
\RIOTBmessage{output}\outputtoks

\RIOTBemptyscreenline

\bye 


More information about the texhax mailing list