
我在整個文件中放置了一個宏,標記了長段落中的關鍵位置。這些與章節標題無關。
_____________ _____________
| | |
| .....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.setdata
/job.datasets.getdata
語法phg 針對不同多遍問題的構造。希望這可以幫助!
\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