是否有一种工具可以读取现有的索引条目,然后在文档中搜索使用索引词/短语的其他位置?
答案1
这是一个开始,但绝不是完整的,甚至可能不是特别好的 Python。它将标记源文件中出现索引术语但后面没有紧跟的任何行\index{term}
。
示例主文件(214458.tex):
\documentclass{report}
\usepackage{makeidx}
\makeindex
\begin{document}
\include{\jobname-1}
\include{\jobname-2}
\printindex
\end{document}
包含的章节(214458-1 和 214458-2.tex):
\chapter{One}
To solve various problems in physics, it can be advantageous
to express any arbitrary piecewise-smooth function as a Fourier Series\index{Fourier Series}
composed of multiples of sine and cosine functions.
和
\chapter{Two}
To solve even more problems\index{problems} in physics\index{physics}, it can be advantageous
to express any arbitrary piecewise-smooth function as a different Fourier Series
composed of multiples of sine and cosine functions.
在尝试以下脚本之前,请确保在文档上运行 latex(否则,没有索引文件可供比较)。
Python脚本(indexwords.py):
#!/usr/bin/env python
### Usage: indexwords.py indexfile sourcefile
import re, sys
indexEntryPattern = '\\indexentry{([A-Za-z ]+)}'
with open(sys.argv[1]) as index:
indexLines = index.readlines()
with open(sys.argv[2]) as source:
sourceLines = source.readlines()
lineDict={}
for indexEntry in indexLines:
match = re.search(indexEntryPattern, indexEntry)
if match:
term = match.groups()[0]
# print "Searching source for unindexed '%s' entries:" % (term)
incompletePattern = "%s" % (term)
completePattern = "%s\\\\index{%s}" % (term, term)
lineNumber=1
for sourceLine in sourceLines:
termFound = re.search(incompletePattern, sourceLine)
termIndexed = re.search(completePattern, sourceLine)
if termFound and not termIndexed:
# print "Line %d: %s" % (lineNumber, sourceLine)
if lineNumber in lineDict:
lineDict[lineNumber].append(term)
else:
lineDict[lineNumber] = [term]
lineNumber = lineNumber+1
for entry in lineDict:
print "On line %d, add entries for %s" % (entry, lineDict[entry])
用法:
安装了 Python 的 Windows:
将 indexwords.py 保存到与 TeX 和索引文件相同的文件夹中。然后打开命令提示符,将目录更改为该文件夹,并运行:
indexwords.py 214458.idx 214458.tex
indexwords.py 214458.idx 214458-1.tex
indexwords.py 214458.idx 214458-2.tex
OS X 或任何其他 Linux/Unix 系统:
将 indexwords.py 保存到与 TeX 和索引文件相同的文件夹中。然后打开命令提示符,将目录更改为该文件夹,并运行:
chmod a+x indexwords.py
一次以使脚本可执行。
然后,在命令提示符下运行:
./indexwords.py 214458.idx 214458.tex
./indexwords.py 214458.idx 214458-1.tex
./indexwords.py 214458.idx 214458-2.tex
示例输出:
E:\Users\mwr\Dropbox\Inbox\TeX.SE>.\indexwords.py 214458.idx 214458.tex
E:\Users\mwr\Dropbox\Inbox\TeX.SE>.\indexwords.py 214458.idx 214458-1.tex
On line 3, add entries for ['problems', 'physics']
E:\Users\mwr\Dropbox\Inbox\TeX.SE>.\indexwords.py 214458.idx 214458-2.tex
On line 4, add entries for ['Fourier Series']
E:\Users\mwr\Dropbox\Inbox\TeX.SE>