\inputlineno 坏了?

\inputlineno 坏了?

以下代码表明这\inputlineno是错误的。它没有为宏\组提供正确的行号,它将宏视为单行。我明白为什么可以理解这一点,因为 TeX 文件被视为标记流,宏的参数可能会也可能不会输出。

因此这\inputlineno是一种“扩展”的行号。

\documentclass[12pt,oneside]{book}

\usepackage{xcolor}
\usepackage{everypage}


\AddEverypageHook{\typeout{Page = \thepage, Line = \the\inputlineno}}


\usepackage{lipsum}
\begin{document}
\textcolor{blue}{\lipsum}
\newpage


\textcolor{red}{This is a test cache1
\newpage
\lipsum
\newpage
\lipsum}


\newpage
\lipsum
\newpage
test
\newpage
xxxxxxxxxxxxxxxx
\end{document}

因此,这可能是预期的行为,但这并不是通常所说的 inputlineno 是“源代码行值”。

有什么方法可以解决这个问题或者在扩展之前获取实际的源代码行。

上述输出

Page = 1, Line = 13
[1]
Page = 2, Line = 14
[2]
Page = 3, Line = 21 % note that texcolor ends here and spans 4 pages
[3]
Page = 4, Line = 21
[4]
Page = 5, Line = 21 
[5]
Page = 6, Line = 21
[6]
Page = 7, Line = 24
[7]
Page = 8, Line = 26
[8]
Page = 9, Line = 26
[9]
Page = 10, Line = 28
[10]
Page = 11, Line = 30

虽然我想要

Page = 1, Line = 13
[1]
Page = 2, Line = 13
[2]
Page = 3, Line = 17
[3]
Page = 4, Line = 19
[4]
Page = 5, Line = 19
[5]
Page = 6, Line = 21 
[6]
Page = 7, Line = 21
[7]
Page = 8, Line = 25 
[8]
Page = 9, Line = 25 
[9]
Page = 10, Line = 27
[10]
Page = 11, Line = 29

(可能存在差异,因为\inputlineno页面似乎出现并且页面末尾和源代码行实际上看起来位于视觉下方)

基本上我需要的就是在进行任何扩展之前获取与所在页面匹配的实际源代码行。因此,如果某个宏在内部添加了 100 页垃圾内容,但只使用一行简短的源代码,那么这就是我想要的。

它与 syntex 类似,但我想避免外部处理。

我知道宏可以做任何它们想做的事情,但我并不关心这些。我只关心将页码映射到实际源代码行。如果没有页码映射到某些行,那么它不会影响我所要做的事情。如果有行没有映射到页面,那么它不会困扰我。但对于以某种方式出现在页面上的每一行源代码,我都需要该页码。

内部生成内容的宏(不属于实际源代码的一部分)会将其跨越的所有页面映射到其行上。

上面的宏用法\textcolor证明了这一点。当它的参数显然出现在不同的页面上,并且存在从页面到行的映射时,我们应该忽略宏中的所有内容并将其视为单行(第一个输出证明了这一点),这不合逻辑,这比将宏视为原子单位更有信息量。

答案1

您对错误原因的假设是有缺陷的。宏在这里确实有一些影响,但基本问题与宏无关。下面是一个文档,在文档开始和文档结束之间没有任何宏,但几乎每一页都报告为在同一行,因为整个段落被处理为换行单位。

AAA:Page = 1, Line = 15
Page = 1, Line = 85
[1{/usr/local/texlive/2016/texmf-var/fonts/map/pdftex/updmap/pdftex.map}
BBB:Page = 1, Line = 85
]
Page = 2, Line = 85
[2]
Page = 3, Line = 85
[3]
Page = 4, Line = 85
[4]
Page = 5, Line = 85
[5]
Page = 6, Line = 85
[6]
Page = 7, Line = 87
[7]

注意\typeout{AAA 之内该段落报告第 15 行,随着该行的扩展,\typeout您在页面钩子中所拥有的基本上是由\par第 85 行的空白行执行的。

上述日志由

\documentclass[12pt,oneside]{book}

\usepackage{everypage}

\setlength\textwidth{5cm}
\setlength\textheight{\dimexpr\topskip+20\baselineskip}

\AddEverypageHook{\typeout{Page = \thepage, Line = \the\inputlineno}}

\raggedright
\begin{document}

One two three four five six seven eight nine ten.
Red yellow blue green purple black white brown.
Apple orange banana grape pear lemon.\typeout{AAA:Page = \thepage, Line = \the\inputlineno}
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.


\end{document}

相关内容