在索引中,我的出版商要求更改页码范围,尽可能不超过两位数。例如:
A, 1-23
B, 101-23 %(for 101-123)
C, 101-105 %(not 101-05, because the second number starts with a zero)
D, 101-222 %full range because 101-22 would be wrong
索引结果只是 101-123,而不是 101-23;我该如何修改它?
这个问题类似于这个用于交叉引用的页面范围,但我认为所需的解决方案会有所不同。
例子:
\begin{theindex}
\indexspace
\textbf{Nonsense}
\subitem More nonsense \hspace{6pt} 440
\subitem Some sense \hspace{6pt} 347–350
\subitem Out of order \hspace{6pt} 320-411
\subitem Beyond repair \hspace{6pt} 100-105
\end{theindex}
应变为:
\begin{theindex}
\indexspace
\textbf{Nonsense}
\subitem More nonsense \hspace{6pt} 440
\subitem Some sense \hspace{6pt} 347–50
\subitem Out of order \hspace{6pt} 320-411
\subitem Beyond repair \hspace{6pt} 100-105
\end{theindex}
(关键的是第二场,347-50)
答案1
Makeindex 的样式文件可用于获取索引文件中页码的标记命令。在这种情况下,样式文件被命名\jobname.mst
,因此会自动执行makeindex
。
%%% test.tex %%%
\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.mst}
delim_0 ", \\ipage{"
delim_1 ", \\ipage{"
delim_2 ", \\ipage{"
delim_n "}, \\ipage{"
delim_r "--"
delim_t "}"
encap_infix "{\\ipage{"
encap_suffix "}}"
\end{filecontents*}
\documentclass{article}
\usepackage{makeidx}
\makeindex
\usepackage{intcalc}
\makeatletter
\newcommand*{\ipage}[1]{%
% The trick with \romannumeral removes an optional space
\expandafter\@ipage\romannumeral-`\x#1----\@nil
}
\def\@ipage#1--#2--#3\@nil{
\def\ipage@temp{#2}%
\ifx\ipage@temp\@empty
% case: no page range detected
#1%
\else
% case: page range #1--#2
\ifnum#1<100 %
% case: no abbreviation needed, if first page < 100
#1--#2%
\else
\edef\ipage@div@a{\intcalcDiv{#1}{100}}%
\edef\ipage@div@b{\intcalcDiv{#2}{100}}%
\ifx\ipage@div@a\ipage@div@b
% case: pages differ in last two digits only
\edef\ipage@mod@b{\intcalcMod{#2}{100}}%
\ifnum\ipage@mod@b>9 %
% case: second page is abbreviated and does not start with "0"
#1--\ipage@mod@b
\else
% case: avoid that abbreviated number starts with "0"
#1--#2%
\fi
\else
% case: pages come from different "hundreds"
#1--#2%
\fi
\fi
\fi
}
\makeatother
\newcommand*{\simpage}[1]{%
\newpage
\setcounter{page}{#1}%
\null
}
\begin{document}
\printindex
\simpage{100}
\index{Nonsense!Beyond repair|(}
\index{Nonsense!Some sense|textbf}
\simpage{105}
\index{Nonsense!Beyond repair|)}
\index{Nonsense!More nonsense|(textit}
\simpage{320}
\index{Nonsense!Out of order|(}
\index{Nonsense!More nonsense|)textit}
\simpage{347}
\index{Nonsense!Some sense}
\simpage{348}
\index{Nonsense!Some sense}
\simpage{349}
\index{Nonsense!Some sense}
\simpage{350}
\index{Nonsense!Some sense}
\simpage{411}
\index{Nonsense!Out of order|)}
\simpage{440}
\index{Nonsense!More nonsense}
\end{document}
生成的文件test.mst
:
delim_0 ", \\ipage{"
delim_1 ", \\ipage{"
delim_2 ", \\ipage{"
delim_n "}, \\ipage{"
delim_r "--"
delim_t "}"
encap_infix "{\\ipage{"
encap_suffix "}}"
命令(或pdflatex
代替latex
或...)
latex test
makeindex test
latex test
索引文件test.ind
:
\begin{theindex}
\item Nonsense
\subitem Beyond repair, \ipage{100--105}
\subitem More nonsense, \ipage{\textit{\ipage{105--320}}}, \ipage{
440}
\subitem Out of order, \ipage{320--411}
\subitem Some sense, \ipage{\textbf{\ipage{100}}}, \ipage{347--350}
\end{theindex}