process_input_buffer 的奇怪双重处理问题

process_input_buffer 的奇怪双重处理问题

我在使用 process_input_buffer 计数行时遇到了问题。似乎\includes被扩展和处理了多次。我认为这是特定于\include

  1. 我使用 process_input_buffer 存储并打印出源代码。

  2. 我有一个宏,它将其参数写入文件,然后用来\include包含该数据(或我尚未添加的 pdf)。

问题:当我使用这个宏时,我最终看到了两次宏的输出。一次是因为它只是输入文件的一部分,另一次是它被宏包含。我实际上从未看到过那行\include,只是结果。

似乎 process_input_buffer 在调用宏后会将其展开,而我之前就需要它,因此我不会重复处理源代码中的任何行(因为我只关心源代码,而不是“输入缓冲区”本身)。现在,它有效地执行了它想要的操作,但我无法按照原样将源写入 pdf 映射器。

如果没有这样的宏,我可以将文档环境中的任何源代码行映射到其 pdf 页面。但我认为这并不可靠,因为其他奇怪的处理事件也会导致类似的问题。

我想要的是process_source_buffer(在进行任何修改之前读取的源代码,并且它与原来的代码没有任何不同)但我想我没有那么幸运。

或者,我想我可能必须手动读取文件并尝试将其缝合起来,process_input buffer但这似乎不是很有效,因为在某些情况下使用重复的行可能会产生匹配问题。即,源文件的每一行在输入缓冲区中至少出现一次,但可能会出现多次,并且一些宏修改可能会触发无效匹配,从而导致行号丢失。

例如,如果宏恰好包含将来在实际源代码中出现的相同行,则匹配将认为它是源代码行并错误地标记其行值。这是我能想到的唯一解决方案,应该适用于许多情况,但不够强大且不可取。我知道 LuaLatex 的功能非常有限,但肯定有办法处理预扩展的 tex?

例如,

Alpha
\includeMe{Beta}
Beta

并将\includeMeBeta 写入文件然后包含它,无法知道第二个 beta 是否是真正的 beta。因为process_input_buffer看到的是

Alpha
\includeMe{Beta}
Beta
Beta

而第二个测试版实际上是从包含文件中包含的测试版,它将触发源代码匹配(因为到那时为止所有内容看起来都与原始内容相同),并且我尝试创建的行号将会关闭。

我从来没有见过这种情况真正发生,但由于有可能,我的方法可能会失败。

有任何想法吗?

(如果 LuaLatex 内置了这些功能,就会容易得多)

答案1

与前面的问题一样,您没有打印任何将输出中的文本与页码联系起来的数字。这里您只是显示扫描行时的页码计数器的值,在所有情况下该值均为 1。

\documentclass[12pt,oneside]{book}

\directlua{require('TeXCache.lua')}

\begin{document}
%__TeXCache_FILESTART__
\setcounter{page}{12}

One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.
One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.

%__TeXCache_FILEEND__
\end{document}

以及

Line = 1, Page = 1, Source = \setcounter{page}{12}
Line = 2, Page = 1, Source = 
Line = 3, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 4, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 5, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 6, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 7, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 8, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 9, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 10, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 11, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 12, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 13, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 14, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 15, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 16, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 17, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 18, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 19, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 20, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 21, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 22, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 23, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 24, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 25, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 26, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 27, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 28, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 29, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 30, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 31, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 32, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 33, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 34, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 35, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 36, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 37, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 38, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 39, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 40, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 41, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 42, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 43, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 44, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 45, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 46, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 47, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 48, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 49, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 50, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 51, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 52, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 53, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 54, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 55, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 56, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 57, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 58, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 59, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 60, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 61, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 62, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 63, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 64, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 65, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 66, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 67, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 68, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 69, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 70, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 71, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 72, Page = 1, Source = One two three four five six seven eight nine ten.
Line = 73, Page = 1, Source = Red yellow blue green purple black white brown.
Line = 74, Page = 1, Source = Apple orange banana grape pear lemon.
Line = 75, Page = 1, Source = 

这只是提供与您从原始 tex 钩子获得的相同信息,即在 tex 读取现在的第 75 行的空白行之前不会输出任何页面。

这是正确的信息,但显然不是您想要的信息。不清楚您想要什么信息。

答案2

如果您想知道源代码和排版页面之间的关联,您需要 \labels。请参见下面的示例。但在您认为这是解决问题的方法之前:在各种情况下,您都会得到错误或至少不清楚的输出,例如

  \label{line:X}\newpage abc

  \label{line:X}\section{abc} % when it starts a new page

  \label{line:X}\begin{figure} blblb\end{figure} % the float can be everywhere

还有许多情况下,如果你在行首插入标签,可能会出现可怕的错误,例如

  \label{line:X}\multicolumn{2}{l}{abc}

所以我坚持我的观点,它不会像你希望的那样发挥作用。

\documentclass[12pt,oneside]{book}

\usepackage{graphicx}
\usepackage[paperheight=6cm]{geometry}

\usepackage{luacode}
\usepackage{listings}
\begin{document}

\label{line:01}One two three four five six seven eight nine ten.
\label{line:02}Red yellow blue green purple black white brown.
\label{line:03}Apple orange banana grape pear lemon.
\label{line:04}One two three four five six seven eight nine ten.
\label{line:05}Red yellow blue green purple black white brown.
\label{line:06}Apple orange banana grape pear lemon.
\label{line:07}One two three four five six seven eight nine ten.
\label{line:08}Red yellow blue green purple black white brown.
\label{line:09}Apple orange banana grape pear lemon.
\label{line:10}One two three four five six seven eight nine ten.
\label{line:11}Red yellow blue green purple black white brown.
\label{line:12}Apple orange banana grape pear lemon.
\label{line:13}One two three four five six seven eight nine ten.
\label{line:14}Red yellow blue green purple black white brown.
\label{line:15}Apple orange banana grape pear lemon.
\label{line:16}One two three four five six seven eight nine ten.
\label{line:17}Red yellow blue green purple black white brown.
\label{line:18}Apple orange banana grape pear lemon.
\label{line:19}One two three four five six seven eight nine ten.
\label{line:20}Red yellow blue green purple black white brown.
\label{line:21}Apple orange banana grape pear lemon.
\label{line:22}One two three four five six seven eight nine ten.
\label{line:23}Red yellow blue green purple black white brown.
\label{line:24}Apple orange banana grape pear lemon.
\label{line:25}One two three four five six seven eight nine ten.
\label{line:26}Red yellow blue green purple black white brown.
\label{line:27}Apple orange banana grape pear lemon.
\label{line:28}One two three four five six seven eight nine ten.
\label{line:29}Red yellow blue green purple black white brown.
\label{line:30}Apple orange banana grape pear lemon.
\label{line:31}One two three four five six seven eight nine ten.
\label{line:32}Red yellow blue green purple black white brown.
\label{line:33}Apple orange banana grape pear lemon.
\label{line:34}One two three four five six seven eight nine ten.
\label{line:35}Red yellow blue green purple black white brown.
\label{line:36}Apple orange banana grape pear lemon.
\label{line:37}One two three four five six seven eight nine ten.
\label{line:38}Red yellow blue green purple black white brown.
\label{line:39}Apple orange banana grape pear lemon.
\label{line:40}One two three four five six seven eight nine ten.
\label{line:41}Red yellow blue green purple black white brown.
\label{line:42}Apple orange banana grape pear lemon.
\label{line:43}One two three four five six seven eight nine ten.
\label{line:44}Red yellow blue green purple black white brown.
\label{line:45}Apple orange banana grape pear lemon.
\label{line:46}One two three four five six seven eight nine ten.
\label{line:47}Red yellow blue green purple black white brown.
\label{line:48}Apple orange banana grape pear lemon.
\label{line:49}One two three four five six seven eight nine ten.
\label{line:50}Red yellow blue green purple black white brown.
\label{line:51}Apple orange banana grape pear lemon.
\label{line:52}One two three four five six seven eight nine ten.
\label{line:53}Red yellow blue green purple black white brown.
\label{line:54}Apple orange banana grape pear lemon.
\label{line:55}One two three four five six seven eight nine ten.
\label{line:56}Red yellow blue green purple black white brown.
\label{line:57}Apple orange banana grape pear lemon.
\label{line:58}One two three four five six seven eight nine ten.
\label{line:59}Red yellow blue green purple black white brown.
\label{line:60}Apple orange banana grape pear lemon.
\label{line:61}One two three four five six seven eight nine ten.
\label{line:62}Red yellow blue green purple black white brown.
\label{line:63}Apple orange banana grape pear lemon.
\label{line:64}One two three four five six seven eight nine ten.
\label{line:65}Red yellow blue green purple black white brown.
\label{line:66}Apple orange banana grape pear lemon.
\label{line:67}One two three four five six seven eight nine ten.
\label{line:68}Red yellow blue green purple black white brown.
\label{line:69}Apple orange banana grape pear lemon.
\label{line:70}One two three four five six seven eight nine ten.
\label{line:71}Red yellow blue green purple black white brown.
\label{line:72}Apple orange banana grape pear lemon.


\begin{luacode*}

for counter = 1, 72 do
 tex.print("line "..counter.." is on page \\pageref{line:"..string.format("%02d",counter).."}\\par")
end
\end{luacode*}
\end{document}

在此处输入图片描述

相关内容