获取文档结束前的 .idx 文件快照

获取文档结束前的 .idx 文件快照

对于索引和词汇表之间交叉引用的一些技巧,我想获取.idx大型文档(书籍)中某个特定点的文件快照 - 准确地说,是在正文之后,但在词汇表之前。我尝试了几种可能的解决方案,但都没有成功。

  1. 打开第二个.idx文件(例如\jobname.idx2)并修补\@wrindex以写入两个文件。我尝试使用xpatch\pretocmd但它抱怨:

-

analyzing '\@wrindex'
++ control sequence is defined
++ control sequence is a macro
++ control sequence is a macro with parameters
-- nested patching command and parameters in patch
-> the patching command seems to be nested in the
   argument to some other command
-> the patch text seems to contain # characters
-> either avoid nesting or use # characters with
   category code 12 in the patch text
-> simply doubling the # characters will not work

更新:这是 MWE

\documentclass{book}

\usepackage{lipsum,makeidx,xpatch,hyperref}

\makeatletter
\newwrite\@dblindexfile
\immediate\openout\@dblindexfile=\jobname.dbx

\newcommand{\writedbx}[1]{\protected@write\@dblindexfile{}{\string\indexentry{#1}}}

\tracingpatches
\AtBeginDocument{\xpretocmd{\@wrindex}{\protected@write\@dblindexfile{}{\string\indexentry{#1}}}{}{}}
%\xpretocmd{\@wrindex}{\protected@write\@dblindexfile{}{\string\indexentry{#1}}}{}{}

\makeatother

\begin{document}

\chapter{Were the moon landings faked?}

\lipsum[1]

\end{document}

但事实证明,如果我这样做xpretocmd 外部,它\AtBeginDocument确实有效:

[debug] tracing \pretocmd on input line 13
[debug] analyzing '\@wrindex'
[debug] ++ control sequence is defined
[debug] ++ control sequence is a macro
[debug] ++ control sequence is a macro with parameters
[debug] ++ macro can be retokenized cleanly
[debug] == retokenizing macro now

并且文件确实被写入,因此修补的宏似乎可以工作。我曾使用 来\AtBeginDocument确保修补程序将应用于 重新定义的宏hyperref,但我当然可以手动确保这一点。

因此,感谢您提出制作 MWE 的建议。

答案1

暂时将#类别代码设为12,建议如下:

\documentclass{book}

\usepackage{lipsum,makeidx,xpatch,hyperref}
\makeindex
\makeatletter
\newwrite\@dblindexfile
\immediate\openout\@dblindexfile=\jobname.dbx

\tracingpatches
\catcode`#=12
\AtBeginDocument{\xpretocmd{\@wrindex}{\protected@write\@dblindexfile{}{\string\indexentry{#1}}}{}{}}
\catcode`#=6
%\xpretocmd{\@wrindex}{\protected@write\@dblindexfile{}{\string\indexentry{#1}}}{}{}

\makeatother

\begin{document}

\chapter{Were the moon landings faked?}

\lipsum[1]

\index{aaa}

\end{document}

相关内容