TeX macro to merge two lists, bit like Python's zip
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/2c1feea4ad... 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
participants (1)
-
luigi scarso