如何使用词汇表包定义索引和多个词汇表?

如何使用词汇表包定义索引和多个词汇表?

问题陈述

我想使用glossaries包制作索引。但是,我在使用时收到一条消息makeglossaries

警告:文件“document.idx”为空。您是否使用了词汇表“index”中定义的任何条目?

我已经通过索引类型定义了索引列表。虽然词汇表列表是成功的(来源问题是针对每章为基础的,而我改编为针对每部分为基础),但我无法对索引列表执行此操作。

平均能量损失

 \documentclass[12pt,a4paper,headinclude,openright,chapterprefix=on,numbers=noenddot]{scrbook}
 \usepackage[xindy,order=letter,nonumberlist,toc,nopostdot,nomain,nohypertypes={index}]{glossaries}
 \renewcommand*{\glspostdescription}{}
 \newglossary{glossary1}{glossarypt01}{glossPt01}{Glossary for Part I} % Note: No front zeros
 \newcommand{\addgloss}[2]{\newglossaryentry{#1}{type=glossary\arabic{part},name=#1,description={#2}}}
 \newcommand{\addglossmath}[3]{\newglossaryentry{#3}{type=glossary\arabic{part},name=\ensuremath{#1},description={#2}}}
 % Code amdended from Ludovic [email protected] (http://tex.stackexchange.com/questions/123824/list-of-symbols-for-each-chapter)
 \newcommand{\printpartglossary}{\glsaddall%
 \printglossary[type=glossary\arabic{part}]}
 \newglossary[ilg]{index}{ind}{idx}{\indexname}
 \newcommand{\newidx}[2]{\newglossaryentry{#1}[type=index,name=#1,description=\nopostdesc]}
 \makeglossaries

 \begin{document}
 \part{Start}
 \chapter{Dummy Chapter}
 \addgloss{complex number}{A type of numbers in the form of $x\;+\;iy$.}
 \section{Complex (Number) Set is the Largest Numerical Set}

 Textbooks when introducing \addgloss{complex number} \newidx{complex number}, do list a set of equations and specify the largest set could not solve such equation. Hence we ought for a complex number. 

 \cleardoublepage
 \printpartglossary

 \printglossary[type=index]
 \end{document}

答案1

主要问题在于的定义\newidx

 \newcommand{\newidx}[2]{\newglossaryentry{#1}[type=index,name=#1,description=\nopostdesc]}

首先,这使用了错误的语法\newglossaryentry。key=value 列表应该用大括号括起来,{}而不是方括号。第二个问题是,\newidx这里定义的 有两个参数,但您只用了一个参数。

这里的另一个问题是,您只定义条目,但没有对它们进行索引。这意味着每次执行时\newidx{complex number},您只是定义条目(存储以供以后使用)。因此,正确的定义应该是:

\newcommand{\newidx}[1]{%
  \newglossaryentry{#1}{type=index,name=#1,description=\nopostdesc}% define it
  \glsadd{#1}% index it
}

\addgloss其他命令(如和)也会出现类似的问题\addglossmath。每个标签( 的第一个参数\newglossaryentry)都应该是唯一的,但您遇到了标签冲突,因为两个不同的条目都想使用标签complex number。但是,由于您在文档中而不是序言中定义条目,因此防止此冲突的保护措施已被禁用,因此标签complex number始终引用使用该标签定义的第一个条目。可以通过在自定义命令的定义中强制使用前缀来解决此冲突。例如:

\newcommand{\addgloss}[2]{%
 \newglossaryentry
 {gls\arabic{part}:#1}% label
 {type=glossary\arabic{part},name=#1,description={#2}}%
 \glsadd{gls\arabic{part}:#1}%
}

\newcommand{\newidx}[1]{%
 \newglossaryentry{idx:#1}{type=index,name={#1},description=\nopostdesc}%
 \glsadd{idx:#1}}

另一个问题是,它\addgloss被定义为接受两个参数,但是您在以下代码中只使用了一个参数:

\addgloss{complex number} \newidx{complex number}

这意味着它将\newidx作为第二个参数。由于complex number已经用 定义\addgloss,因此这不会执行任何操作,您只剩下{complex number},这就是为什么 看起来似乎\addgloss在工作,但实际上却没有。事实上,最好定义:

\newcommand{\addgloss}[2]{%
 \newglossaryentry
 {gls\arabic{part}:#1}% label
 {type=glossary\arabic{part},name=#1,description={#2}}%
}

\newcommand*{\refgloss}[1]{\gls{gls\arabic{part}:#1}}

然后使用\refgloss来引用该条目。像这样:

\documentclass{scrbook}
\usepackage[xindy,order=letter,nonumberlist,toc,nopostdot,
 nomain,nohypertypes={index},
]{glossaries}

\newglossary{glossary1}{glossarypt01}{glossPt01}{Glossary for Part I}
\newglossary[ilg]{index}{ind}{idx}{\indexname}

\newcommand{\printpartglossary}{\glsaddall%
\printglossary[type=glossary\arabic{part}]}

\makeglossaries

\newcommand{\addgloss}[2]{%
 \newglossaryentry
 {gls\arabic{part}:#1}% label
 {type=glossary\arabic{part},name=#1,description={#2}}%
}

\newcommand*{\refgloss}[1]{\gls{gls\arabic{part}:#1}}

\newcommand{\addglossmath}[3]{%
 \newglossaryentry{#3}{type=glossary\arabic{part},name=\ensuremath{#1},description={#2}}}

\newcommand{\newidx}[1]{%
 \newglossaryentry{idx:#1}{type=index,name={#1},description=\nopostdesc}%
 \glsadd{idx:#1}}


\begin{document}
\part{Start}
\chapter{Dummy Chapter}
\addgloss{complex number}{A type of numbers in the form of $x\;+\;iy$.}
\section{Complex (Number) Set is the Largest Numerical Set}

Textbooks when introducing \refgloss{complex number}\newidx{complex number}, do list a set of equations and specify the largest set could not solve such equation. Hence we ought for a complex number. 

\cleardoublepage
\printpartglossary

\printglossary[type=index]
\end{document}

背后的原理glossaries是,您首先定义条目,然后在文档中引用它们。(可以将其视为参考书目,将数据存储在单独的文件中,然后在文档中引用它。)文档中的动态定义可能会导致问题,但可以通过扩展包glossariesglossaries-extra使用docdef=restricted包选项来减少这些问题。例如:

\documentclass[12pt,a4paper,headinclude,openright,chapterprefix=on,numbers=noenddot]{scrbook}
\usepackage[xindy,order=letter,nonumberlist,
 nomain,nohypertypes={index},
 docdef=restricted
]{glossaries-extra}

\newglossary{glossary1}{glossarypt01}{glossPt01}{Glossary for Part I} % Note: No front zeros
\newglossary[ilg]{index}{ind}{idx}{\indexname}

\newcommand{\printpartglossary}{\glsaddall%
\printglossary[type=glossary\arabic{part}]}

\makeglossaries

\newcommand{\addgloss}[2]{%
 \newglossaryentry
 {gls\arabic{part}:#1}% label
 {type=glossary\arabic{part},name=#1,description={#2}}%
}

\newcommand*{\refgloss}[1]{\gls{gls\arabic{part}:#1}}

\newcommand{\addglossmath}[3]{%
 \newglossaryentry{#3}{type=glossary\arabic{part},name=\ensuremath{#1},description={#2}}}

\newcommand{\newidx}[1]{%
 \ifglsentryexists{idx:#1}%
 {}% already defined
 {%
   \newglossaryentry{idx:#1}{type=index,name={#1},description=\nopostdesc}%
 }%
 \glsadd{idx:#1}}


\begin{document}
\part{Start}
\chapter{Dummy Chapter}
\addgloss{complex number}{A type of numbers in the form of $x\;+\;iy$.}
\section{Complex (Number) Set is the Largest Numerical Set}

Textbooks when introducing \refgloss{complex number}\newidx{complex number}, do list a set of equations and specify the largest set could not solve such equation. Hence we ought for a complex number. 

\cleardoublepage
\printpartglossary

\printglossary[type=index]
\end{document}

编辑:这里不需要\cleardoublepageas \printglossary(在 的定义中使用\printpartglossary)已经发出了明确的双页,因为它在\chapter*与 类一起使用时以 开头scrbook。此外,\glsaddallwithin\printpartglossary将索引全部此时定义的条目(包括前面部分中定义的条目)。对书的每个部分都这样做是多余的,并且可能会导致一些奇怪的行为。

顺便说一句,您可能希望打开索引的数字列表,因为没有它,索引似乎没有什么意义。您可能还希望更改为更适合索引的样式(例如mcolindex),并隐藏索引条目的粗体字体。

修正后的 MWE:

\documentclass[12pt,a4paper,headinclude,openright,chapterprefix=on,numbers=noenddot]{scrbook}
\usepackage[xindy,order=letter,
 stylemods={mcols},% load glossary-mcols.sty
 nomain,nohypertypes={index},
 docdef=restricted
]{glossaries-extra}

\newglossary{glossary1}{glossarypt01}{glossPt01}{Glossary for Part I}
\newglossary[ilg]{index}{ind}{idx}{\indexname}

% Just suppress numberlists for the "part" glossaries
\newcommand{\printpartglossary}{%
  \printglossary[nonumberlist,type=glossary\arabic{part}]}

\makeglossaries

\newcommand{\addgloss}[2]{%
 \newglossaryentry
 {gls\arabic{part}:#1}% label
 {type=glossary\arabic{part},name=#1,description={#2}}%
}

\newcommand*{\refgloss}[1]{\gls{gls\arabic{part}:#1}}

\newcommand{\addglossmath}[3]{%
 \newglossaryentry{#3}{type=glossary\arabic{part},name=\ensuremath{#1},description={#2}}}

\newcommand{\newidx}[1]{%
 \ifglsentryexists{idx:#1}%
 {}% already defined
 {%
   \newglossaryentry{idx:#1}{type=index,name={#1},description=\nopostdesc}%
 }%
 \glsadd{idx:#1}}


\begin{document}
\part{Start}
\chapter{Dummy Chapter}
\addgloss{complex number}{A type of numbers in the form of $x\;+\;iy$.}
\section{Complex (Number) Set is the Largest Numerical Set}

Textbooks when introducing \refgloss{complex number}\newidx{complex number}, do list a set of equations and specify the largest set could not solve such equation. Hence we ought for a complex number. 

\printpartglossary

\renewcommand*{\glstreenamefmt}[1]{#1}% remove bold font in mcolindex style
\printglossary[type=index,style=mcolindex]
\end{document}

相关内容