用空格填充数字以达到正确的排序索引

用空格填充数字以达到正确的排序索引

makeindex(或 imakeidx)可以很好地按数字进行索引,除非您添加文本强调(粗体或斜体),如图所示。为了强制对第二组进行正确的排序,我想用额外的空格填充数字,假设数字只有 3 位数字,例如行中的宏变量 \padthreedigits

\index[test]{{\bfseries{#3}}!\textit{\padthreedigits{#2}}: #1}

以下是示例(参数#2是填充的目标)

\documentclass{article}
\usepackage{imakeidx}

\newcommand{\addreftoindex}[3]{
    % ** This is the offending line
    \index[test]{{\bfseries{#3}}!\textit{#2}: #1}
}
\makeindex
\makeindex[title=My Index, columnseprule, name=test]

\begin{document}

    Lorem Ipsum is \addreftoindex{book 1}{5}{Author 15} simply dummy text of the printing and typesetting industry. 

    To show that getvalue works, \addreftoindex{book 1}{9}{Author 15}I want the bookname I cited here: \addreftoindex{book 1}{11}{Author 15}

    Lorem Ipsum has been the industry's standard dummy text ever since the~1500s,  
    when an unknown printer took a galley of type and scrambled it to make a type 
    specimen book. 

    It \addreftoindex{book 9}{9}{Author 01}has survived not only \addreftoindex{book 9}{1}{Author 01}five centuries, but \addreftoindex{book 9}{11}{Author 01}also the leap into 
    electronic typesetting, remaining essentially unchanged. Now add the reference 

    \printindex[test]   
\end{document}

对数字进行索引排序

答案1

假设\indexdigits“999”的长度\padthreedigits可以定义为

\newcommand\padthreedigits[1]{\hbox to \indexdigits{\hfill#1}}

以你的例子来说:

\documentclass{article}
\usepackage{imakeidx}

\newdimen\indexdigits
\setbox0\hbox{999}
\indexdigits\wd0
\newcommand\padthreedigits[1]{\hbox to \indexdigits{\hfill#1}}
\newcommand{\addreftoindex}[3]{
    \index[test]{{\bfseries{#3}}!\textit{\padthreedigits{#2}}: #1}
}
\makeindex
\makeindex[title=My Index, columnseprule, name=test]

\begin{document}

    Lorem Ipsum is \addreftoindex{book 1}{5}{Author 15} simply dummy text of the printing and typesetting industry. 

    To show that getvalue works, \addreftoindex{book 1}{9}{Author 15}I want the bookname I cited here: \addreftoindex{book 1}{11}{Author 15}

    Lorem Ipsum has been the industry's standard dummy text ever since the~1500s,  
    when an unknown printer took a galley of type and scrambled it to make a type 
    specimen book. 

    It \addreftoindex{book 9}{9}{Author 01}has survived not only \addreftoindex{book 9}{1}{Author 01}five centuries, but \addreftoindex{book 9}{11}{Author 01}also the leap into 
    electronic typesetting, remaining essentially unchanged. Now add the reference 

    \printindex[test]   
\end{document}

最后,关于索引的排序,你会说

\newcommand{\addreftoindex}[3]{
  \index[test]{{\bfseries{#3}}!#2@\textit{\padthreedigits{#2}}: #1}
}

输出

答案2

<sort by>@<entry>如果可能包含奇怪的东西,请考虑使用符号<entry>,而应按以下方式排序<sort by>

在此处输入图片描述

\documentclass{article}

\usepackage{imakeidx}

\newcommand{\addreftoindex}[3]{
  % ** This is the offending line
  \index[test]{{\bfseries{#3}}!#2@\makebox[0pt][r]{\textit{#2}}: #1}
}

\makeindex[title=My Index, columnseprule, name=test]

\begin{document}

Lorem Ipsum is \addreftoindex{book 1}{5}{Author 15} simply dummy text of the printing and typesetting industry. 

To show that getvalue works, \addreftoindex{book 1}{9}{Author 15}I want the bookname I cited here: \addreftoindex{book 1}{11}{Author 15}

Lorem Ipsum has been the industry's standard dummy text ever since the~1500s,  
when an unknown printer took a galley of type and scrambled it to make a type 
specimen book. 

It \addreftoindex{book 9}{9}{Author 01}has survived not only \addreftoindex{book 9}{1}{Author 01}five centuries, 
but \addreftoindex{book 9}{11}{Author 01}also the leap into 
electronic typesetting, remaining essentially unchanged. Now add the reference 

\printindex[test]

\end{document}

元素的格式已设置r为在零宽度框内右对齐。您可以将 改为0pt更大的尺寸,以将项目进一步移至右侧。

答案3

想法是使用@;我们还希望宏不会不合时宜地扩展。

\documentclass{article}

\usepackage{etoolbox}
\usepackage{imakeidx}
\makeindex[title=My Index, columnseprule, name=test]

\newrobustcmd{\addreftoindex}[3]{%
  \index[test]{\formatauthor{#3}!#2@\padnumber{#2}: #1}%
}
\newrobustcmd{\formatauthor}[1]{\textbf{#1}}
\newrobustcmd{\padnumber}[1]{%
  \textit{\ifnum#1<10 \hphantom{00}\else\ifnum#1<100 \hphantom{0}\fi\fi#1}%
}


\begin{document}

Lorem Ipsum is \addreftoindex{book 1}{5}{Author 15} simply dummy text 
of the printing and typesetting industry. 

To show that getvalue works, \addreftoindex{book 1}{9}{Author 15}I want 
the bookname I cited here: \addreftoindex{book 1}{11}{Author 15}

Lorem Ipsum has been the industry's standard dummy text ever since the~1500s,  
when an unknown printer took a galley of type and scrambled it to make a type 
specimen book. 

It \addreftoindex{book 9}{9}{Author 01}has survived not only 
\addreftoindex{book 9}{1}{Author 01}five centuries, 
but \addreftoindex{book 9}{11}{Author 01}also the leap into 
electronic typesetting, remaining essentially unchanged. Now add the reference.

\printindex[test]

\end{document}

test.idx以下是(原始索引文件)的内容:

\indexentry{\formatauthor {Author 15}!5@\padnumber {5}: book 1}{1}
\indexentry{\formatauthor {Author 15}!9@\padnumber {9}: book 1}{1}
\indexentry{\formatauthor {Author 15}!11@\padnumber {11}: book 1}{1}
\indexentry{\formatauthor {Author 01}!9@\padnumber {9}: book 9}{1}
\indexentry{\formatauthor {Author 01}!1@\padnumber {1}: book 9}{1}
\indexentry{\formatauthor {Author 01}!11@\padnumber {11}: book 9}{1}

以下是test.ind已排序并处理过的文件:

\begin{theindex}

  \item \formatauthor {Author 01}
    \subitem \padnumber {1}: book 9, 1
    \subitem \padnumber {9}: book 9, 1
    \subitem \padnumber {11}: book 9, 1
  \item \formatauthor {Author 15}
    \subitem \padnumber {5}: book 1, 1
    \subitem \padnumber {9}: book 1, 1
    \subitem \padnumber {11}: book 1, 1

\end{theindex}

在此处输入图片描述

如果想要更小的缩进,可以调整定义\subitem。例如,

\makeatletter
\renewcommand{\subitem}{\@idxitem\hspace*{10pt}}
\makeatother

将缩进减半(在三个数字的空格之前)。

相关内容