From: luigi scarso <luigi.scarso@gmail.com>
To: Hans Hagen <pragma@wxs.nl>,
mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: TeX macro to merge two lists, bit like Python's zip
Date: Wed, 9 Jun 2010 00:16:37 +0200 [thread overview]
Message-ID: <AANLkTimZFT7MLfYQ2v71ETUmSJ0Pdp81O5ONMijkoB0p@mail.gmail.com> (raw)
J. Fine asked sometime ago something like python's zip tha merge two
"lists" element by element, cfr.
http://groups.google.com/group/comp.text.tex/browse_thread/thread/2c1feea4ad19501e/cf6f849c5b81e76c?show_docid=cf6f849c5b81e76c
Here is the idea: a list is zero or more control sequence, ie
{} is an empty list,
\Alpha\Beta
is a list with 2 elements, and so on.
\zip{\Alpha\Beta}{\Gamma\Delta} gives \Alpha\Gamma\Beta\Delta
\starttext
% Hans's hint
% \def\zipthem#1#2%
% {\def\dozipthem##1{\def\dodozipthem####1{1\string##1
2\string####1}\handletokens#2\with\dodozipthem}%
% \handletokens#1\with\dozipthem}
\def\myzipthem#1#2%
{\ctxlua{A = {}; B = {}}
\def\dozipthemA##1{\ctxlua{table.insert(A,[[\string##1]])}}%
\def\dozipthemB##1{\ctxlua{table.insert(B,[[\string##1]])}}%
\handletokens#1\with\dozipthemA
\handletokens#2\with\dozipthemB%
\ctxlua{%
for i=1,math.max(table.maxn(A),table.maxn(B)) do
local a = ""
local b = ""
if A[i] then a = '\\string' ..A[i] end
if B[i] then b = '\\string' ..B[i] end
tex.sprint(a,b)
end
tex.print([[\blank]])
for i=1,math.max(table.maxn(A),table.maxn(B)) do
local a = A[i] or ""
local b = B[i] or ""
tex.sprint(a,b)
end
tex.print([[\blank]])
}
}
\def\Alpha{A}
\def\Beta{B}
\def\Gamma{C}
\def\Delta{D}
\myzipthem{\Alpha\Beta}{\Gamma\Delta}%gives \Alpha\Gamma\Beta\Delta
\myzipthem{}{\Gamma\Delta}
\stoptext
For ours bloggers.
--
luigi
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
reply other threads:[~2010-06-08 22:16 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=AANLkTimZFT7MLfYQ2v71ETUmSJ0Pdp81O5ONMijkoB0p@mail.gmail.com \
--to=luigi.scarso@gmail.com \
--cc=ntg-context@ntg.nl \
--cc=pragma@wxs.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).