From: Sietse Brouwer <sbbrouwer@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: Referring to multiple elements
Date: Fri, 14 Sep 2012 14:08:38 +0200 [thread overview]
Message-ID: <CAF=dkzzdRQYjBoHra1uo_qc2ZcTDCsp1MCkPUA1BFDZz-AWtnQ@mail.gmail.com> (raw)
In-Reply-To: <20120914093302.7c98411a@homerow>
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
___________________________________________________________________________________
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
___________________________________________________________________________________
next prev parent reply other threads:[~2012-09-14 12:08 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-13 10:15 Marco Patzer
2012-09-13 10:48 ` Andreas Mang
2012-09-14 7:33 ` Marco Patzer
2012-09-14 12:08 ` Sietse Brouwer [this message]
2012-09-16 12:56 ` Marco Patzer
2012-09-28 1:39 ` Sietse Brouwer
2012-09-28 8:56 ` Marco Patzer
2012-09-28 11:28 ` Sietse Brouwer
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='CAF=dkzzdRQYjBoHra1uo_qc2ZcTDCsp1MCkPUA1BFDZz-AWtnQ@mail.gmail.com' \
--to=sbbrouwer@gmail.com \
--cc=ntg-context@ntg.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).