I wish to write a LaTeX macro, which expands something like this:
\x{abc}
gives
\y{a}\y{b}\y{c}
that is: foreach of the letter in the argument return
another macro with the letter as an argument.
Is this possible in TeX? If anyone could point me to a place in
the TeX or LaTeX book, where I can read about the matter,
I would be grateful. (So would I be if anyone could help me write the
macro).
regards Henrik
>Hi,
>I wish to write a LaTeX macro, which expands something like this:
>\x{abc}
>gives
>\y{a}\y{b}\y{c}
I once wrote a macro to do the same thing, but allowing a range of characters.
Might this be what you are after?
(\def\x#1{\processcharlist{#1}{\y}} would be the syntax, but beware that
I copied it from a .sty file without verification)
Peter Dalgaard
(p...@kubism.ku.dk)
Stat. Res. Unit, U.Copenh.
% First two macros are just to allow indentation, etc.
\def\macsreadable{
\catcode`\ =9 % ignore spaces
\catcode`\^^I=9 % ignore tabs
\catcode`\^^M=9 % ignore newline
}
\def\macsunreadable{
\catcode`\ =10 % normal spaces
\catcode`\^^I=10 % normal tabs
\catcode`\^^M=5 % normal newline
}
% ------------------------------------------------------
%
\macsreadable
\def \processcharlist #1#2 { % Expand list in #1 and apply #2 (a
% one-parameter macro) to each
% character of the expansion
\def\processitem{#2} % Place item processor where co-macros
% below can see it.
\xlistfirst #1 \end % Recursively process character list
% in #1 using \end as sentinel.
% (\xlistfirst takes one *character*
% of #1 as argument, processes it and
% emits a control sequence that
% handles the rest of the list, etc.)
}
\def \xlistfirst #1 { % First char is special in that there
% can be no range expansion here.
\itemno`#1 % Save charcode (for range computation)
\processitem\itemno
\xlist % - and \xlist handles the rest
}
\def \xlist #1 {
\ifx #1 \end % Finished ?
\let\next\relax
\else\ifx #1 - % Range
\let\next\xrange
\else % Business as usual
\itemno`#1
\processitem\itemno
\let\next\xlist
\fi\fi
\next
}
\def \xrange #1 {
\loop % Process characters \itemno+1,...,#1
\advance \itemno 1
\processitem\itemno
\ifnum \itemno < `#1 \repeat
\xlist % - and handle rest of list
}
\macsunreadable
% ------------------------------------------------------------
Try:
›def›Y#1{›ifx#1|›else›y{#1}›fi}
›def›x#1{›Y#1|}
(assuming that | will not appear in the argument of ›x)
(I did not test it, but it should work)
Peter
>I wish to write a LaTeX macro, which expands something like this:
>\x{abc}
>gives
>\y{a}\y{b}\y{c}
>
>that is: foreach of the letter in the argument return
>another macro with the letter as an argument.
Look in latex.tex under the section "PROGRAM CONTROL STRUCTURE
MACROS". There are several macros for doing this sort of thing,
depending on exactly what you want the input to look at. For example,
your macro \x can be implemented as follows using the \@tfor command:
\makeatletter
\def\x#1{\@tfor \@tempa := #1 \do {\y{\@tempa}}}
\makeatother
This will execute the command \y for each token in the argument of \x
before expansion.
If you change \@tfor to \@for above, then you get a macro with
slightly different semantics: \x would take a comma-separated list of
items after expansion and execute \y for each element in the list.
I.e.,
\x{foo,bar,a}
would expand to
\y{foo}\y{bar}\y{a}
--
David M. Jones "Trifles! Trifles light as air!"
dmj...@theory.lcs.mit.edu -- the Hystricide