我使用 Latex,特别是使用 Ubuntu 16.04 上的 pdfLatex 处理器
假设我在 Latex 中输入了一个很长的文档(比如《哈姆雷特》)。经过处理后,假设 Latex 生成了一个 300 页的 pdf 文件。
我想知道 pdfLatex 在 .tex 文档中的哪个位置插入分页符并开始新的一页。
更正式地说,是否可以告诉 pdfLatex 输出一种字典,使得对于 pdf 的每一页(“键”),字典指定包含该页内容的原始 .tex 文件的行集(“值”)。
你可能会想出一个笨拙的解决方案,使用pdfgrep但我想知道 pdfLatex 编译器是否会自动生成这些信息。
答案1
您确实需要为此使用 synctex,因为它是通过使用 synctex 标志运行 pdflatex 生成的。这将生成一个.synctex.gz
基本上是 gzip 压缩的文本文件(synctex 文件),或者如果您选择负标志,它只是文本文件。
举个例子来说明这一点:
\documentclass[12pt]{article}
\usepackage{lipsum}
\begin{document}
\lipsum\lipsum
\end{document}
将生成以下 synctex 文件(缩短):
SyncTeX Version:1
Input:1:/path/to/file.tex
Input:2:/usr/local/texlive/2018/texmf-dist/tex/latex/base/article.cls
Input:3:/usr/local/texlive/2018/texmf-dist/tex/latex/base/size12.clo
Input:4:/usr/local/texlive/2018/texmf-dist/tex/latex/lipsum/lipsum.sty
Input:5:/path/to/file.aux
Output:pdf
Magnification:1000
Unit:1
X Offset:0
Y Offset:0
Content:
!410
{1
[1,7:4736286,46187806:28114944,41451520,0
[1,7:7292190,46187806:25559040,40337408,0
[1,7:7292190,6636830:25559040,786432,0
h1,7:7292190,6636830:25559040,0,0
]
[1,7:7292190,44221726:25559040,35946496,0
(1,7:7292190,9061662:25559040,546132,152916
h1,7:7292190,9061662:1155060,0,0
x1,7:10596809,9061662
k1,7:10882169,9061662:285360
x1,7:11523869,9061662
x1,7:12897107,9061662
k1,7:13182468,9061662:285361
x1,7:13995288,9061662
x1,7:14893668,9061662
k1,7:15179028,9061662:285360
x1,7:15996126,9061662
k1,7:16281486,9061662:285360
x1,7:18163806,9061662
k1,7:18456337,9061662:292531
x1,7:19611397,9061662
x1,7:22610275,9061662
k1,7:22895635,9061662:285360
x1,7:24350155,9061662
x1,7:25210033,9061662
x1,7:26236753,9061662
k1,7:26522113,9061662:285360
x1,7:27805513,9061662
k1,7:28233794,9061662:428281
x1,7:29110511,9061662
k1,7:29395872,9061662:285361
x1,7:30251472,9061662
x1,7:31282470,9061662
k1,7:31567830,9061662:285360
x1,7:32851230,9061662
k1,7:32851230,9061662:0
)
... MORE CONTENT HERE ...
]
(1,7:7292190,46187806:25559040,506811,0
k1,7:19879200,46187806:12587010
x1,7:20264220,46187806
k1,7:32851230,46187806:12587010
)
]
]
!27910
}1
!10
... MORE CONTENT HERE ...
{4
[1,8:4736286,46187806:28114944,41451520,0
[1,8:7292190,46187806:25559040,40337408,0
[1,8:7292190,6636830:25559040,786432,0
h1,8:7292190,6636830:25559040,0,0
]
[1,8:7292190,44221726:25559040,35946496,0
(1,8:7292190,9061662:25559040,546132,152916
x1,8:9580920,9061662
x1,8:10928490,9061662
k1,8:11236209,9061662:307719
x1,8:12220149,9061662
k1,8:12540627,9061662:320478
x1,8:13229385,9061662
x1,8:14923473,9061662
k1,8:15231192,9061662:307719
x1,8:17549868,9061662
k1,8:17870346,9061662:320478
x1,8:20308806,9061662
k1,8:20804163,9061662:495357
x1,8:22889552,9061662
k1,8:23197270,9061662:307718
x1,8:23967310,9061662
k1,8:24275029,9061662:307719
x1,8:26375527,9061662
k1,8:26870883,9061662:495356
x1,8:29591691,9061662
k1,8:29899410,9061662:307719
x1,8:30305820,9061662
x1,8:32851230,9061662
k1,8:32851230,9061662:0
)
... MORE CONTENT HERE ...
(1,8:7292190,21415198:25559040,546132,152916
x1,8:7698600,21415198
x1,8:8447250,21415198
g1,8:8703930,21415198
x1,8:11249340,21415198
g1,8:11506020,21415198
x1,8:14483508,21415198
g1,8:14740188,21415198
x1,8:15146598,21415198
x1,8:17478108,21415198
g1,8:17734788,21415198
x1,8:18718728,21415198
g1,8:18975408,21415198
x1,8:20862006,21415198
k1,8:32851230,21415198:11989224
g1,8:32851230,21415198
)
]
(1,8:7292190,46187806:25559040,506811,0
k1,8:19879200,46187806:12587010
x1,8:20264220,46187806
k1,8:32851230,46187806:12587010
)
]
]
!9778
}4
Input:6:/path/to/file.aux
!66
Postamble:
Count:1701
!26
Post scriptum:
有趣的部分是以 开头的行{<int>
,例如{1
,因为这表示新页面(页面由 btw 关闭}<int>
)。您会在那里找到所谓的字节偏移记录。这些是以 开头的数字!
。因此,要获取分页符的确切位置,您只需使用在页面开始之前输出的字节偏移量。
字节偏移量可以通过一个简单的脚本轻松转换为行数,因此我在此不再赘述。但由于您想要获得行数范围,因此您还需要查看第二个字节偏移量记录,即页面末尾之前的记录。