ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Referring to multiple elements
@ 2012-09-13 10:15 Marco Patzer
  2012-09-13 10:48 ` Andreas Mang
  0 siblings, 1 reply; 8+ messages in thread
From: Marco Patzer @ 2012-09-13 10:15 UTC (permalink / raw)
  To: context

Hi,

how can I refer to multiple elements at the same time?

\starttext

\startplacefigure [title=foo, reference=alpha]
\stopplacefigure
\startplacefigure [title=bar, reference=beta]
\stopplacefigure
\startplacefigure [title=bar, reference=gamma]
\stopplacefigure

\in{figure}[alpha,beta,gamma]

\stoptext

This outputs “figure 1”. What I'd like to have is “figure 1-3”.


Marco

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Referring to multiple elements
  2012-09-13 10:15 Referring to multiple elements Marco Patzer
@ 2012-09-13 10:48 ` Andreas Mang
  2012-09-14  7:33   ` Marco Patzer
  0 siblings, 1 reply; 8+ messages in thread
From: Andreas Mang @ 2012-09-13 10:48 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi Marco,

I do not know if there is a solution to this. The following would work for your example:

\in{figure}[alpha]--\in[gamma]

Of course this is a quite rigid solution, that does not safeguard against changes in figure order...So probably not what you were looking for.

Cheers,
Andreas

Am Sep 13, 2012 um 12:15 PM schrieb Marco Patzer:

> Hi,
> 
> how can I refer to multiple elements at the same time?
> 
> \starttext
> 
> \startplacefigure [title=foo, reference=alpha]
> \stopplacefigure
> \startplacefigure [title=bar, reference=beta]
> \stopplacefigure
> \startplacefigure [title=bar, reference=gamma]
> \stopplacefigure
> 
> \in{figure}[alpha,beta,gamma]
> 
> \stoptext
> 
> This outputs “figure 1”. What I'd like to have is “figure 1-3”.
> 
> 
> Marco
> 
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________


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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Referring to multiple elements
  2012-09-13 10:48 ` Andreas Mang
@ 2012-09-14  7:33   ` Marco Patzer
  2012-09-14 12:08     ` Sietse Brouwer
  0 siblings, 1 reply; 8+ messages in thread
From: Marco Patzer @ 2012-09-14  7:33 UTC (permalink / raw)
  To: ntg-context

2012-09-13 Andreas Mang <mang@imt.uni-luebeck.de>:

> I do not know if there is a solution to this. The following would
> work for your example:
> 
> \in{figure}[alpha]--\in[gamma]
> 
> Of course this is a quite rigid solution, that does not safeguard
> against changes in figure order

That's what I am using at the moment. Apparently there is no such
functionality. This one would go on my wish list.


Marco

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Referring to multiple elements
  2012-09-14  7:33   ` Marco Patzer
@ 2012-09-14 12:08     ` Sietse Brouwer
  2012-09-16 12:56       ` Marco Patzer
  0 siblings, 1 reply; 8+ messages in thread
From: Sietse Brouwer @ 2012-09-14 12:08 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Referring to multiple elements
  2012-09-14 12:08     ` Sietse Brouwer
@ 2012-09-16 12:56       ` Marco Patzer
  2012-09-28  1:39         ` Sietse Brouwer
  0 siblings, 1 reply; 8+ messages in thread
From: Marco Patzer @ 2012-09-16 12:56 UTC (permalink / raw)
  To: ntg-context

2012-09-14 Sietse Brouwer <sbbrouwer@gmail.com>:

Hi Sietse,

I'm sorry for my late reply.

> 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:
>
> […]
>
> I've written one below, plus the scaffolding required to feed it the
> right input, and print its output to ConTeXt.

I wonder if this function is not already present in ConTeXt. The
register mechanism, for instance, already does something very
similar:

\setupregister
  [index]
  [compress=yes]

\starttext

A\index{A} \page
A\index{A} \page
aaa        \page
bbb        \page
A\index{A} \page
A\index{A} \page
A\index{A}

\placeindex %% => A 1-2, 5-7

\stoptext

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

I'm quite sure that all the building blocks are already present. I'm
not sure about the case when the float number is preceded by a
chapter number.

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

Someone with more expertise and knowledge about the ConTeXt
internals should be able to point to the relevant functions. I guess
the code that produces the index compression is located in
registers.flush() in strc-reg.lua, but apparently it's not a
function and I don't know if it can be adopted for the float
referencing.

> (And, of course, you may be far more experienced in LuaTeX than I,
> I don't know.)

Probably not. I can read and write simple Lua scripts, but I lack
knowledge about the LuaTeX and ConTeXt internals and the data
structures used. For LuaTeX there is a manual that explains a lot,
but it doesn't help figuring out how ConTeXts float referencing
mechanism works.

Thanks for the work you spend on this. I don't need this function
for the current project (I use the workaround), but I would like to
see this feature in ConTeXt. If you have time and expertise to
implement this, it would be great. Otherwise I'll do the range
referencing manually.


Regards
Marco

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Referring to multiple elements
  2012-09-16 12:56       ` Marco Patzer
@ 2012-09-28  1:39         ` Sietse Brouwer
  2012-09-28  8:56           ` Marco Patzer
  0 siblings, 1 reply; 8+ messages in thread
From: Sietse Brouwer @ 2012-09-28  1:39 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Hans Hagen

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Referring to multiple elements
  2012-09-28  1:39         ` Sietse Brouwer
@ 2012-09-28  8:56           ` Marco Patzer
  2012-09-28 11:28             ` Sietse Brouwer
  0 siblings, 1 reply; 8+ messages in thread
From: Marco Patzer @ 2012-09-28  8:56 UTC (permalink / raw)
  To: ntg-context

2012-09-28 Sietse Brouwer <sbbrouwer@gmail.com>:

Hi Sietse,

> > \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.

Thanks a lot for that. I'd adjust the interface, so that it matches
the behaviour of \in:

\def\inrange
  {\dodoublegroupempty\doinrange}

\def\doinrange#left#right#dummy[#label]
  {\iffirstargument
     {#left }%%
   \fi
   \ctxlua{userdata.inwithranges("#label")}%%
   \ifsecondargument
     {#right}%%
   \fi}

Then you can write:

See \inrange{figures}     [fig:a,fig:c,fig:d,fig:e,fig:g,fig:x,fig:h]
See \inrange{figures}{TT} [fig:a,fig:c,fig:d,fig:e,fig:g,fig:x,fig:h]
See \inrange              [fig:a,fig:c,fig:d,fig:e,fig:g,fig:x,fig:h]

Maybe worth mentioning: You need the additional module
`showtable.lua` from http://article.gmane.org/gmane.comp.tex.context/78336


Marco

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Referring to multiple elements
  2012-09-28  8:56           ` Marco Patzer
@ 2012-09-28 11:28             ` Sietse Brouwer
  0 siblings, 0 replies; 8+ messages in thread
From: Sietse Brouwer @ 2012-09-28 11:28 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi Marco,

Marco wrote:
> Maybe worth mentioning: You need the additional module
> `showtable.lua` from http://article.gmane.org/gmane.comp.tex.context/78336

Oops, that was a debugging statement. Removed.

> Thanks a lot for that. I'd adjust the interface, so that it matches
> the behaviour of \in:

> See \inrange{figures}     [fig:a,fig:c,fig:d,fig:e,fig:g,fig:x,fig:h]
> See \inrange{figures}{TT} [fig:a,fig:c,fig:d,fig:e,fig:g,fig:x,fig:h]
> See \inrange              [fig:a,fig:c,fig:d,fig:e,fig:g,fig:x,fig:h]

Done. New version uploaded to Github:
https://github.com/sietse/context-inrange

Cheers,
Sietse
___________________________________________________________________________________
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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-09-28 11:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-13 10:15 Referring to multiple elements 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
2012-09-28  8:56           ` Marco Patzer
2012-09-28 11:28             ` Sietse Brouwer

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).