On 12/3/2012 10:09 AM, Wolfgang Schuster wrote:
Am 02.12.2012 um 23:18 schrieb Hans Hagen
: On 12/2/2012 4:31 PM, Wolfgang Schuster wrote:
Hi,
how can I write the following two lines with Lua, I saw in supp-box.lua a few example how to loop through the characters but I don’t know how I can add glue between them.
\starttext
\ruledhbox to 6cm{\processtokens{\hfil }{\hfil\hfil}{\hfil }{}{WORD}}
\ruledhbox to 6cm{\processtokens{\relax}{\hfil\hfil}{\relax}{}{WORD}}
\def\HFILLED#1{\hfil#1\hfil}
\ruledhbox to 6cm{\applytocharacters\HFILLED{WORD}} \ruledhbox to 6cm{\hfilneg\applytocharacters\HFILLED{WORD}\hfilneg}
\stoptext
\startluacode function commands.ws_hfilled(str) context.hfil() for c in string.utfcharacters(str) do context(c) context.hfil() end end \stopluacode
\enabletrackers[context.trace]
\def\HFILLED#1{\ctxcommand{ws_hfilled("#1")}}
\ruledhbox to 6cm{\HFILLED{WORD}} \ruledhbox to 6cm{\hfilneg\HFILLED{WORD}\hfilneg}
This does the trick but can I check for the last char without the additional check (if n == string.utflength(str)) in the example below?
\startluacode function commands.ws_hfilled(str,margin) local n = 0 if margin then context.hfil() end for c in string.utfcharacters(str) do n = n + 1 context(c) if n == string.utflength(str) then if margin then context.hfil() end else context.hfil() end end end \stopluacode
\starttext \ruledhbox to 6cm{\ctxlua{commands.ws_hfilled("WORD")}} \ruledhbox to 6cm{\ctxlua{commands.ws_hfilled("WORD",true)}} \stoptext
easier: \startluacode function commands.ws_hfilled(str,margin) local done = false for c in string.utfcharacters(str) do if done or margin then context.hfil() else done = true end context(c) end if margin then context.hfil() end end \stopluacode \starttext \ruledhbox to 6cm{\ctxlua{commands.ws_hfilled("WORD")}} \ruledhbox to 6cm{\ctxlua{commands.ws_hfilled("WORD",true)}} \stoptext ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------