ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Sietse Brouwer <sbbrouwer@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>,
	Hans Hagen <pragma@wxs.nl>
Subject: Re: Referring to multiple elements
Date: Fri, 28 Sep 2012 03:39:31 +0200	[thread overview]
Message-ID: <CAF=dkzzsmCWv+ZUyKMP67R0d+mUXbhHf4HhZeiJvjJkx1yYtvA@mail.gmail.com> (raw)
In-Reply-To: <20120916145646.64648cc8@homerow>

[-- Attachment #1: Type: text/plain, Size: 1232 bytes --]

Hi Marco,
(PS for Hans),

Marco wrote:
> \in{figure}[alpha,beta,gamma]
>
> This outputs “figure 1”. What I'd like to have is “figure 1-3”.

The attached quasi-module seems to do it! On my computer, at least. (I
call it 'quasi' because it is really nothing more than code in a file
of its own. No configurability whatsoever, except to the extent that I
tried to comment well, and split things up into functions.) I'd have
written it so that it properly identifies runs like '1.2.1, 1.2.2,
1.2.3', too, but I can't find a function that will turn reference
strings into such a prefixed strings.

I hope it does what you want! Let me know if you want/need any
alterations, or discover bugs.
Sietse

PS @Hans: if figure numbers have prefix segments, is there any
(combination of) helper function that will return e.g. the string
'1.2.3' given the reference string "ref:fig-three"? I found
commands.savedlistprefixednumber(nil, numberinlist), but that is a
wrapper around sections.typesetnumber; and that last function injects
the string into the TeX stream instead of returning it. Is there
another function I haven't found yet that will do this, or are
prefixes implemented print-only at the moment?

[-- Attachment #2: inwithranges.mkiv --]
[-- Type: application/octet-stream, Size: 100 bytes --]

\ctxlua{require('inwithranges')}

\def\inwithranges[#1]{\ctxlua{
    userdata.inwithranges('#1')
}}

[-- Attachment #3: inwithranges.lua --]
[-- Type: application/octet-stream, Size: 3752 bytes --]

--  Create a function that turns
--  \refwithranges[fig:a, fig:b, fig:c, fig:x, fig:z]
--  into
--  Figures 1-3,24,26

local report = logs.reporter("inwithranges")
local errorcode = -99

-- Given an array of numbers, return an array of runs in that list.
-- Each run is itself an array with elements ["start"] and ["stop"]
-- Pre-sorting is left in the user's hands
local function get_runs(a)
    runs = { }
    run_start = 1
    while run_start <= #a do
        run_stop = run_start
        -- TODO replace a[run_stop] + 1 with
        -- increment_number_string(a[run_stop])
        -- that turns '1.2.1' into '1.2.2'
        -- so we can get runs among prefixed numbers, too.
        if a[run_stop] <= -100 then
            report("Ignoring entry %d", a[run_stop])
        else
            while a[run_stop + 1] == a[run_stop] + 1 do
                run_stop = run_stop + 1
            end
            report("%s--%s", run_start, run_stop)
            table.insert(runs, {["start"] = a[run_start],
                                ["stop"]   = a[run_stop]})
        end
        run_start = run_stop + 1
    end
    return runs
end

-- Given a reference string, return the figure/section/table number
-- Yes, invoking this on multiple strings operates in quadratic time.
-- Solution: assume n to be small
-- A helper function for this should exist somewhere
local function number_from_ref(refstring)
    -- TODO ensure we only run when structures.lists.ordered.float
    -- already exists
    for k,v in pairs(structures.lists.ordered.float.figure) do
        -- TODO if we return the full '1.2.1' string here
        -- then adapt get_runs as stated there, we can process prefixed
        -- numbers, too.
        if refstring == v.references.reference then
            report("%s --> %d", refstring, v.numberdata.numbers[1])
            return v.numberdata.numbers[1]
        end
    end
    errorcode = errorcode - 1
    report("Unknown reference: %s, returning %d", refstring, errorcode)
    return errorcode
end


-- Input: an array of runs,
-- Action: print something like '1, 3-5, and 8'
local function typeset_runs(runs, args)
    args = args or { }
    range_char = args["range_char"] or '-'
    run_sep = args["run_sep"] or ', '
    last_sep = args["last_sep"] or run_sep

    local i = 0
    require('showtable')
    userdata.showtable(runs)
    for _, run in pairs(runs) do
        if 0 < i and i < #runs - 1 then
            context(run_sep)
        end
        if 0 < i and i == #runs - 1 then
            context(last_sep)
        end
        i = i + 1

        context("\\in[%s]", run.start)
        if run.start ~= run.stop then
            context("%s\\in[%s]", range_char, run.stop)
        end
    end
end

-- User-facing function: 
local function inwithranges(str)
    if not structures.lists.ordered["float"] then
        -- float table does not yet exist, do nothing this run
        return false
    end

    local refstrings_unsorted = utilities.parsers.settings_to_array(str)
    local refstrings = { }
    local numbers = { }

    -- turn refstrings into numbers, and remember what goes with what
    for _, ref in pairs(refstrings_unsorted) do
        local n = number_from_ref(ref)
        table.insert(numbers, n)
        refstrings[n] = ref
    end
    -- sort the numbers, and turn them into a runs table
    table.sort(numbers)
    local runs = get_runs(numbers)

    -- replace the numbers in the runs table with refstrings, and
    -- typeset
    for k, run in pairs(runs) do
        runs[k].start = refstrings[run.start]
        runs[k].stop = refstrings[run.stop]
    end
    typeset_runs(runs, {last_sep = ' and '})
end

userdata = userdata or { }
u = userdata
u.get_runs = get_runs
u.inwithranges = inwithranges

[-- Attachment #4: test-inwithranges.tex --]
[-- Type: application/x-tex, Size: 1426 bytes --]

[-- Attachment #5: test-inwithranges.pdf --]
[-- Type: application/pdf, Size: 13975 bytes --]

[-- Attachment #6: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

  reply	other threads:[~2012-09-28  1:39 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
2012-09-16 12:56       ` Marco Patzer
2012-09-28  1:39         ` Sietse Brouwer [this message]
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=dkzzsmCWv+ZUyKMP67R0d+mUXbhHf4HhZeiJvjJkx1yYtvA@mail.gmail.com' \
    --to=sbbrouwer@gmail.com \
    --cc=ntg-context@ntg.nl \
    --cc=pragma@wxs.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).