3

I have a macro placed throughout my document marking key places within a long passage. These have no relation to the chapter or section titles.

 _____________ _____________
|             |             |
| .....a..... | ........... |
| .........b. | ..d........ |
| ........... | ........... |
| ........... | ........... |
| ........... | ........... |
| ....c...... | .......e... |
| ........... | ........... |
|______1______|______2______|

 _____________ _____________
|             |             |
| ....f...... | ........... |
| .......g... | ........... |
| ...h....... | .....j..... |
| ........... | ........... |
| ........... | Section.... |
| .......i... | ..........k |
| ........... | ........... |
|______3______|______4______|

I need to place information in the headers, to tell readers the current page range of the markings of the portion of text they are currently reading, e.g.:

 _____________ _____________
| c: 1-2      | e: 2-3      |
| .....a..... | ........... |
| .........b. | ..d........ |
| ........... | ........... |
| ........... | ........... |
| ........... | ........... |
| ....c...... | .......e... |
| ........... | ........... |
|______1______|______2______|

 _____________ _____________
| e: 2-3      | j: 4-4      |
| ........... | ........... |
| ........... | ........... |
| ........... | .....j..... |
| ........... | ........... |
| ........... | Section.... |
| ........... | ........... |
| ........... | ..........k |
|______3______|______4______|
  • As some pages, have multiple ranges, the last range appearing on the page can be displays.

How can I place information in the headers of my document to tell readers the page range of the current portion of the text they are reading?

5
+100

Make a \rangestart{rangename} and \rangestop{rangename} macro, make them store the page numbers across runs, and make a function to select and print any ranges that are active on a given page number.

I stole the job.datasets.setdata / job.datasets.getdata syntax from phg's construction for a different multi-pass question. Hope this helps!

\startluacode
    userdata = userdata or {}
    local ranges_start_tmp = {}

    -- record range name and start in temp. array, to be
    -- flushed to the .tuc file when we also know the stop.
    function userdata.rangestart(rangename, page)
        ranges_start_tmp[rangename] = page
    end

    -- flush range name, start, and stop to the .tuc file.
    function userdata.rangestop(rangename, page)
        job.datasets.setdata({
            name = "ranges",
            data = {
                ["name"]  = rangename,
                ["start"] = ranges_start_tmp[rangename],
                ["stop"]  = page,
            }
        })
        ranges_start_tmp[rangename] = nil
    end

    -- Takes 3 arguments: the page whose active ranges to describe,
    -- a template{name, start, stop} for formatting a single range,
    -- and a separator to print between multiple ranges
    function userdata.printranges(page, template, sep)
        local ranges = {}

        local i = 1
        local range = job.datasets.getdata("ranges", i) -- default: nil
        while range do
            if range.start <= page and page <= range.stop then
                ranges[#ranges + 1] = string.formatters[template](
                    range.name, range.start, range.stop
                )
            end
            i = i + 1
            range = job.datasets.getdata("ranges", i)
        end

        context(table.concat(ranges, sep))
    end
\stopluacode

% The TeX side of the mechanism
\define[1]\rangestart{\ctxlua{userdata.rangestart([[#1]], \userpage)}}
\define[1]\rangestop{\ctxlua{userdata.rangestop([[#1]], \userpage)}}
\define[3]\printranges{\ctxlua{userdata.printranges(#1, #2, #3)}}

% -------------------------
% Let's try it out

% Templates contain percent signs -- better to write them in a lua
% block.
\startluacode
    userdata.mytemplate = "%s, from %i to %i"
\stopluacode
\setupheadertexts[\printranges{\userpage}{userdata.mytemplate}{"; "}]
\setupfootertexts[\userpage]

\setuppapersize[A6, landscape]

\starttext
    \section{A}
        \input knuth
        \input knuth
    \section{B}
        \rangestart{a}
        \input knuth
        \rangestop{a}
        \rangestart{b}
    \section{C}
        \input knuth
        \rangestop{b}
\stoptext
  • 1
    Rather than a global mytemplate it is better to use usersdata.range_template so as not to pollute the global name space – Aditya Jul 15 '13 at 0:56
  • 1
    I had hoped to get away with laziness, but the devil makes work for idle hands. :-P Corrected. – Esteis Jul 15 '13 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.