[dev-context] Stretch charatcers

Wolfgang Schuster schuster.wolfgang at gmail.com
Mon Dec 3 10:09:45 CET 2012


Am 02.12.2012 um 23:18 schrieb Hans Hagen <pragma at wxs.nl>:

> 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

Wolfgang


More information about the dev-context mailing list