Hi Marco, The heart of what you need is a lua function that will take an array of numbers, and return an array of consective-number runs, like so: fignumarray = {1, 2, 3, 5, 6, 8, 10, 11, 12} -- figruns = get_runs(fignumarray) -- returns a table structured like so: --> figruns[1].start = 1, figruns[1].stop = 3, --> figruns[2].start = 5, figruns[2].stop = 6, --> figruns[3].start = 8, figruns[3].stop = 8, --> figruns[4].start = 10, figruns[4].stop = 12 I've written one below, plus the scaffolding required to feed it the right input, and print its output to ConTeXt. Only two functions still need writing --- one to turn the context argument [fig:f1, fig:f2, fig:f4] into a lua array of strings, and one to turn each reference string into a figure number. But this should get you on your way. If you're not comfortable with programming LuaTeX, say so and I can fill in the two missing functions sometime this weekend. But they should be doable, really. (And, of course, you may be far more experienced in LuaTeX than I, I don't know.) Cheers, Sietse % should be below startluacode block, but clearer like so \def\inwithranges[#1]% {\ctxlua{u.inwithranges("#1")}} \startluacode u = userdata or { } function get_runs(a) runs = { } run_start = 1 while run_start <= #a do run_stop = run_start while a[run_stop + 1] == a[run_stop] + 1 do run_stop = run_stop + 1 end print(a[run_start] .. "-" .. a[run_stop]) table.insert(runs, {["start"] = a[run_start], ["stop"] = a[run_stop]}) run_start = run_stop + 1 end return runs end function u.inwithranges(ref_string) -- CTX... means I expect ConTeXt already has this function in a library somewhere local ref_array = CTXstring_to_array(refs_string) --FIXME local ref_numbers = { } for _, v in ipairs(ref_array) do ref_numbers[i] = CTXref_to_fignumber(array) --FIXME end ref_numbers.sort() runs = get_runs(ref_numbers) for i, run in ipairs(runs) do context.in( {run.start} ) context("-") context.in( {run.stop} ) if i < #runs then context(',') end end end \stopluacode