为每个部分制作索引关键字列表

为每个部分制作索引关键字列表

我使用 makeidx 包和命令在文档中创建了一个索引\index{}。现在我想总结每个部分前面的所有索引条目(以括号内且没有页码的关键字集合的形式),这些条目在该部分中使用。可以吗?

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{makeidx}
\makeindex

\begin{document}

\section{Section 1}
% Display here all indexes used in Section 1
\index{First index}
\index{Second index}

\section{Section 2}
% Display here all indexes used in Section 2
\index{Third index}

\printindex

\end{document}

答案1

这是一个初步版本,使用imakeidx及其多索引功能。

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{imakeidx}
\usepackage{totcount}
\usepackage{assoccnt}%
\usepackage{forloop}
\usepackage{blindtext}

\newtotcounter{totalsections}
\newcounter{loopcounter}

\makeindex[name=indexsec1,title={Index of section 1}]
\makeindex[name=indexsec2,title={Index of section 2}]

\DeclareAssociatedCounters{section}{totalsections}

\begin{document}

\section{Section 1}
% Display here all indexes used in Section 1
\index[indexsec\number\value{totalsections}]{First index}


\index[indexsec\number\value{totalsections}]{Second index}

\printindex[indexsec\number\value{totalsections}]

\section{Section 2}
% Display here all indexes used in Section 2
\index[indexsec\number\value{totalsections}]{Third index}

\printindex[indexsec\number\value{totalsections}]



\end{document}

改进版本,自动使用index[indexsec#]

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{imakeidx}
\usepackage{totcount}
\usepackage{assoccnt}%
\usepackage{forloop}
\usepackage{blindtext}

\usepackage{xpatch}

\newcounter{totalsections}

\DeclareAssociatedCounters{section}{totalsections}%

\newcounter{loopcounter}

\newcommand{\indexsecname}{indexsec}

\newcommand{\sectionindexprefix}[1]{%
\indexsecname\number\value{#1}%
}%


\newcounter{maxindexsections}
\setcounter{maxindexsections}{10}


\forloop{loopcounter}{1}{\value{loopcounter} <  \numexpr\value{maxindexsections}+1}{%
  \makeindex[name=\sectionindexprefix{loopcounter}]
}%



\let\indexoriginal\index

\renewcommand{\index}[2][\sectionindexprefix{totalsections}]{%
  \indexoriginal[#1]{#2}
}%


\makeatletter
\AtEndDocument{%
  \ifnumgreater{\value{totalsections}}{0}{\IfFileExists{\sectionindexprefix{totalsections}.idx}{\printindex[\sectionindexprefix{totalsections}]}{}}{}%
%  \immediate\write\@auxout{\string\setcounter{numberofsections}{\number\totvalue{totalsections}}}
}%

\xpretocmd{\@sect}{\ifnumgreater{\value{totalsections}}{0}{\IfFileExists{\sectionindexprefix{totalsections}.idx}{\printindex[\sectionindexprefix{totalsections}]}{}}{}}{}{}
\makeatletter

\begin{document}

\section{Section 1}
% Display here all indexes used in Section 1
\blindtext[10]

\index{First index}


\index{Second index}

\index{Intermediate index of first section}


\section{Section 2}
% Display here all indexes used in Section 2
\index{Third index}

\section{Section 3}
\blindtext[10]
\index{Fourth index}



\end{document}

相关内容