如何在 ConTeXt 中显示段落中标记位置的页面范围?

如何在 ConTeXt 中显示段落中标记位置的页面范围?

我在文档中放置了一个宏,标记了长段落中的关键位置。这些与章节或节标题无关。

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

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

我需要在标题中放置信息,以告诉读者他们当前正在阅读的文本部分的标记的当前页面范围,例如:

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

 _____________ _____________
| e: 2-3      | j: 4-4      |
| ........... | ........... |
| ........... | ........... |
| ........... | .....j..... |
| ........... | ........... |
| ........... | Section.... |
| ........... | ........... |
| ........... | ..........k |
|______3______|______4______|
  • 由于某些页面具有多个范围,因此可以显示页面上出现的最后一个范围。

我如何在文档的标题中放置信息来告诉读者他们正在阅读的当前文本部分的页面范围?

答案1

制作一个\rangestart{rangename}\rangestop{rangename}宏,使它们存储运行中的页码,并制作一个函数来选择和打印给定页码上活​​动的任何范围。

job.datasets.setdatajob.datasets.getdataphg 针对不同的多遍问题的构造。 希望这可以帮助!

\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

相关内容