在我的教科书中,我想同时包含索引和词汇表。对于词汇表(我目前正在使用该glossaries
软件包),我更希望指针(和超链接)不会直接指向正文中的一个或多个出现位置,而是指向单个索引条目(通常针对同一个术语)。
也许需要画一张草图。
常规场景:
Index
Combustion engine -> page 23, page 56, ...
Glossary
Combustion engine: an endangered species -> page 23, page 56, ...
我想做的是:
Index
Combustion engine -> page 23, page 56, ...
Glossary
Combustion engine: an endangered species. -> (page of) index entry
on combustion engine
在我们这样做的同时,我们还不如生成一个指向索引项中的词汇表条目的指针。
动机:
不要让索引中已经维护的长页码引用列表扰乱词汇表。
我通常会在词汇表条目中给出相当广泛的解释,而正文通常不会对这些解释添加太多内容;因此,我不想让读者仔细阅读正文来寻找不存在的细节,从而令读者感到沮丧。
在写作时,我不想考虑这样的问题:这个事件应该只放在索引中,还是只放在词汇表中,还是两者都放?考虑在索引中包含什么内容已经足够让人分心了。
附言:在评论上述示例文本之前,请注意本书既不是关于发动机也不是关于濒危物种的。
答案1
这是我的想法:我放弃了glossaries
包,而是直接基于 实现了几个宏hyperref
。索引照常由 生成makeindex
,但我使用了一些技巧,以便让它生成超链接和 -targets 以匹配词汇表中创建的超链接和 -targets。词汇表可以简单地写成一个description
环境(尽管在现实生活中我会使用enumitem
包来定制它)。
% arara: pdflatex
% arara: makeindex
% arara: pdflatex
% arara: pdflatex
% we need two pdflatex runs after makeindex for this to work.
% arara is great for this kind of per-document build configuration.
\documentclass[12pt]{article}
\usepackage[margin=1in,papersize={6in,8in}]{geometry}
% create a custom style file for makeindex that wraps the standard
% index item commands such that we can get hold of their text contents
%
% e.g:
%
% \inx{drugs}
% \subinx{lithium}\hyperpage{2}, \hyperindexformat{\glsitem}{3}
% \subinx{molecular size}\hyperpage{1}
\begin{filecontents*}{\jobname.mst}
line_max 2048
item_0 "\n\\inx{"
item_1 "\n \\subinx{"
item_01 "\n \\subinx{"
item_x1 "}\n \\subinx{"
item_2 "\n \\subsubinx{"
item_12 "\n \\subsubinx{"
item_x2 "}\n \\subsubinx{"
delim_0 "}"
delim_1 "}"
delim_2 "}"
\end{filecontents*}
% load and run makeindex
\usepackage{makeidx}
\makeindex
% we use hyperref to implement the reciprocal links between glossary and index
\usepackage[colorlinks,linkcolor=blue]{hyperref}
% implement the commands
% raise hypertargets by one line to get them into the viewport
\newcommand*{\raisedtarget}[1]{\raisebox{\ht\strutbox}{\hypertarget{#1}{}}}
% a glossary entry. Mandatory arguments are term and definition.
% The optional argument becomes a prefix for the index key.
\newcommand{\glsentry}[3][]{%
\edef\glskey{#1#2|glsitem}%
\expandafter\index\expandafter{\glskey}%
\item [#2] \raisedtarget{gls-#2} #3 \hyperlink{inx-#2}{\pageref*{inx-#2}\emph{i}}
}
% wrap the index \item and \subitem commands to store the current index term
\newcommand*{\inx}[1]{%
\global\edef\currentinx{#1}%
\item #1
}
\newcommand*{\subinx}[1]{%
\global\edef\currentinx{#1}%
\subitem #1
}
\newcommand*{\subsubinx}[1]{%
\global\edef\currentinx{#1}%
\subsubitem #1
}
% format the index page references generated by glossary entries
\newcommand{\glsitem}[1]{%
\label{inx-\currentinx}% we need the label to typeset the index page in the glossary ...
\raisedtarget{inx-\currentinx}% ... and the explicit hypertarget to make the link work
\hyperlink{gls-\currentinx}{#1\emph{g}}%
}
\begin{document}
\section{Introduction to pharmacology}
... document text snipped ...
\section*{Glossary}
\begin{description}
% the [drugs!] prefix makes the link go to a subitem in the index
\glsentry[drugs!]{lithium}{An alkali metal used in batteries that moonlights as an antidepressant}
% no prefix will point the link to a main index item
\glsentry{Paracelsus}{A famous physician from the 16th century who investigated the relationship of drugs and poisons by way of self-experimentation}
\end{description}
\printindex
\end{document}
词汇表如下:
索引如下: