![多卷书的索引](https://linux22.com/image/437068/%E5%A4%9A%E5%8D%B7%E4%B9%A6%E7%9A%84%E7%B4%A2%E5%BC%95.png)
我想为多卷书创建索引。我尝试了这个:如何从多个文档生成组合索引? 有一些初步结果,但是排序错误。下面是一个简短的例子:
第一卷:
\documentclass{article}
\usepackage{makeidx}
\makeindex
\newcommand\docname[1]{#1}
\let\LATEXindex\index % save old definition to prevent recursion
\renewcommand\index[1]{\LATEXindex{#1|docname{I}}}
\begin{document}
\index{word1}word1
\index{word2}word2
\index{word3}word3
\index{word4}
\newpage
\setcounter{page}{3}
\index{word1}word1
\newpage
\setcounter{page}{4}
\index{word1}word1
\index{word2}word2
\newpage
\setcounter{page}{5}
\index{word1}word1
\index{word2}word2
\printindex
\end{document}
第二卷:
\documentclass{article}
\usepackage{makeidx}
\makeindex
\newcommand\docname[1]{#1}
\let\LATEXindex\index % save old definition to prevent recursion
\renewcommand\index[1]{\LATEXindex{#1|docname{II}}}
\begin{document}
\index{word1}word1
\index{word3}word3
\index{word4}word4
\newpage
\setcounter{page}{3}
\index{word1}word1
\index{word2}word2
\index{word3}word3
\newpage
\setcounter{page}{4}
\index{word1}word1
\index{word3}word3
\newpage
\setcounter{page}{5}
\index{word2}word2
\index{word4}word4
\printindex
\end{document}
指數量:
\documentclass{article}
\usepackage{makeidx}
\newcommand\docname[2]{#1: #2}
\begin{document}
\printindex
\end{document}
正如上面的链接所写,两个 .idx 文档合并为一个,我这样做:
复制/b *.idx volume_ind.idx
之后,对 volume_ind.idx 执行 makeindex 和 latex。它起作用了,但是两个卷混在一起,排序错误。它看起来像这样:
有人知道如何正确排列索引条目吗?最好的方法是这样的:
单词1 I:1,3-4,II:1,3-4
万分感谢!!
答案1
我自己找到了答案。现在只需要一个文档和一个 perl 脚本。
\documentclass{article}
\usepackage{makeidx}
\usepackage{xpatch}
\makeindex
\makeatletter
\patchcmd{\@wrindex}{\thepage}{\volumenr-\thepage}{}{}
\makeatother
\begin{document}
\newcommand{\volumenr}{I}
\index{word1}word1
\index{word2}word2
\index{word3}word3
\index{word4}
\newpage
\setcounter{page}{3}
\index{word1}word1
\newpage
\index{word1}word1
\index{word2}word2
\newpage
\index{word1}word1
\index{word2}word2
\renewcommand{\volumenr}{II}
\index{word1}word1
\index{word3}word3
\index{word4}word4
\newpage
\setcounter{page}{3}
\index{word1}word1
\index{word2}word2
\index{word3}word3
\newpage
\index{word1}word1
\index{word3}word3
\newpage
\index{word2}word2
\index{word4}word4
\printindex
\end{document}
perl 脚本消除了 *.ind 文件中多余的罗马数字,因此我在一行中只保留一个“I-”或“II-”:
#!/usr/bin/perl
use strict;
my $src = shift @ARGV;
my $des = shift @ARGV;
open(SRC, '<', $src) or die $!;
open(DES,'>',$des) or die $!;
while (my $row = <SRC>) {
$row =~ s/II-/II: /; # search first "II-" in each row, replace with "II: "
$row =~ s/(?<!%)\II-//g; # search all "II-" in each row, delete
$row =~ s/I-/I: /; # search first "I-" in each row, replace with "I: "
$row =~ s/(?<!%)\I-//g; # search all "I-" in each row, delete
print DES "$row";
}
close(SRC);
close(DES);
我在 makeindex 之后和第二次 latex 调用之前调用了 perl 脚本。
我的索引现在如下所示:
答案2
为此,我建议使用glossaries-extra
。bib2gls
例如,如果您有以下三个文档:
我的文档1.tex
\documentclass{article}
\usepackage[record,index,postdot]{glossaries-extra}
\usepackage{glossary-bookindex}
\setglossarystyle{bookindex}
%number of columns
\renewcommand{\glsxtrbookindexcols}{2}
%names for glossaries, there can be more than one
\newglossary{symbols}{lds}{lsi}{Symbols List}
\newglossary{index}{ing}{ige}{General Index}
%setting external documents and prefixes
\newcommand{\setfileprefix}[2]{\csdef{prefix@#1}{#2}}
\newcommand{\usefileprefix}[1]{\csuse{prefix@#1}}
\setfileprefix{MyDoc2.pdf}{II-}
\setfileprefix{MyDoc3.pdf}{III-}
\renewcommand*\glsxtrdisplaysupploc[5]{%
\setentrycounter[#1]{#2}%
\usefileprefix{#4}\glsxtrmultisupplocation{#5}{#4}{#3}%
}
%style of symbols list
\newglossarystyle{listsimb}{%
\glossarystyle{super}%
\renewenvironment{theglossary}%
{\tablehead{}\tabletail{}%
\begin{supertabular}{@{}lp{20cm}}}%
{\end{supertabular}}%
\renewcommand{\glsgroupskip}{}%
\renewcommand*{\glossaryentryfield}[5]{%
\glsentryitem{##1}\glstarget{##1}{##2} & ##3\glspostdescription\space ##5\\[2pt]}%
}
%font
\renewcommand*{\glsnamefont}[1]{\textmd{#1}}
%loading supplemental files
\GlsXtrLoadResources[
src=index,
type=index,
supplemental-locations={MyDoc2, MyDoc3},% you can have more than one!
supplemental-category={supplemental},
supplemental-selection={all}
]
%and you can define all kinds of glossaries
\GlsXtrLoadResources[
src=symbols,
type=symbols,
supplemental-locations={MyDoc2, MyDoc3},
supplemental-category={supplemental},
supplemental-selection={all}
]
\begin{document}
A test \gls{ind1} and another time with different output \glsuseri{ind1}. Finally a constant \gls{symb1}.
\printunsrtglossary[type=index, style=bookindex]
\printunsrtglossary[type=symbols, style=listsimb]
\end{document}
MyDoc2.tex
\documentclass{article}
\usepackage[record,index,postdot]{glossaries-extra}
\usepackage{glossary-bookindex}
\setglossarystyle{bookindex}
%number of columns
\renewcommand{\glsxtrbookindexcols}{2}
%names for glossaries, there can be more than one
\newglossary{symbols}{lds}{lsi}{Symbols List}
\newglossary{index}{ing}{ige}{General Index}
%setting external documents and prefixes
\newcommand{\setfileprefix}[2]{\csdef{prefix@#1}{#2}}
\newcommand{\usefileprefix}[1]{\csuse{prefix@#1}}
\setfileprefix{MyDoc1.pdf}{I-}
\setfileprefix{MyDoc3.pdf}{III-}
\renewcommand*\glsxtrdisplaysupploc[5]{%
\setentrycounter[#1]{#2}%
\usefileprefix{#4}\glsxtrmultisupplocation{#5}{#4}{#3}%
}
%style of symbols list
\newglossarystyle{listsimb}{%
\glossarystyle{super}%
\renewenvironment{theglossary}%
{\tablehead{}\tabletail{}%
\begin{supertabular}{@{}lp{20cm}}}%
{\end{supertabular}}%
\renewcommand{\glsgroupskip}{}%
\renewcommand*{\glossaryentryfield}[5]{%
\glsentryitem{##1}\glstarget{##1}{##2} & ##3\glspostdescription\space ##5\\[2pt]}%
}
%font
\renewcommand*{\glsnamefont}[1]{\textmd{#1}}
%loading supplemental files
\GlsXtrLoadResources[
src=index,
type=index,
supplemental-locations={MyDoc1, MyDoc3},% you can have more than one!
supplemental-category={supplemental},
supplemental-selection={all}
]
%and you can define all kinds of glossaries
\GlsXtrLoadResources[
src=symbols,
type=symbols,
supplemental-locations={MyDoc1, MyDoc3},
supplemental-category={supplemental},
supplemental-selection={all}
]
\begin{document}
A test in this document \gls{ind2} and a constant \gls{symb1}.
\printunsrtglossary[type=index, style=bookindex]
\printunsrtglossary[type=symbols, style=listsimb]
\end{document}
和 MyDoc3.tex
\documentclass{article}
\usepackage[record,index,postdot]{glossaries-extra}
\usepackage{glossary-bookindex}
\setglossarystyle{bookindex}
%number of columns
\renewcommand{\glsxtrbookindexcols}{2}
%names for glossaries, there can be more than one
\newglossary{symbols}{lds}{lsi}{Symbols List}
\newglossary{index}{ing}{ige}{General Index}
%setting external documents and prefixes
\newcommand{\setfileprefix}[2]{\csdef{prefix@#1}{#2}}
\newcommand{\usefileprefix}[1]{\csuse{prefix@#1}}
\setfileprefix{MyDoc1.pdf}{II-}
\setfileprefix{MyDoc2.pdf}{III-}
\renewcommand*\glsxtrdisplaysupploc[5]{%
\setentrycounter[#1]{#2}%
\usefileprefix{#4}\glsxtrmultisupplocation{#5}{#4}{#3}%
}
%style of symbols list
\newglossarystyle{listsimb}{%
\glossarystyle{super}%
\renewenvironment{theglossary}%
{\tablehead{}\tabletail{}%
\begin{supertabular}{@{}lp{20cm}}}%
{\end{supertabular}}%
\renewcommand{\glsgroupskip}{}%
\renewcommand*{\glossaryentryfield}[5]{%
\glsentryitem{##1}\glstarget{##1}{##2} & ##3\glspostdescription\space ##5\\[2pt]}%
}
%font
\renewcommand*{\glsnamefont}[1]{\textmd{#1}}
%loading supplemental files
\GlsXtrLoadResources[
src=index,
type=index,
supplemental-locations={MyDoc1, MyDoc2},% you can have more than one!
supplemental-category={supplemental},
supplemental-selection={all}
]
%and you can define all kinds of glossaries
\GlsXtrLoadResources[
src=symbols,
type=symbols,
supplemental-locations={MyDoc1, MyDoc2},
supplemental-category={supplemental},
supplemental-selection={all}
]
\begin{document}
A test \gls{ind2} and \gls{symb2}.
\printunsrtglossary[type=index, style=bookindex]
\printunsrtglossary[type=symbols, style=listsimb]
\end{document}
您还需要此示例中的索引和符号文件 index.bib
@index{ind1, %id
name = {Something}, %as is going to appear in the index
user1 = {some-thing} %if you want something different in the text
}
@index{ind2,
name = {another thing},
parent = {ind1}
}
和symbols.bib
@entry{symb1,
name = {\ensuremath{c}},
description = {an important constant},
sort = {constant}
}
@entry{symb2,
name = {\ensuremath{\binom{n}{k}}},
description = {binomial coeficient},
sort = {binomial}
}
他们所有人都可以拥有一把sort
钥匙,但前提是index
它是正确的。
然后,使用以下链进行编译:
pdflatex MyDoc1
pdflatex MyDoc2
pdflatex MyDoc3
bib2gls MyDoc1
bib2gls MyDoc2
bib2gls MyDoc3
pdflatex MyDoc1
pdflatex MyDoc2
pdflatex MyDoc3
您将在不同的文件中得到以下内容