![对索引进行排序时忽略连字符](https://linux22.com/image/383191/%E5%AF%B9%E7%B4%A2%E5%BC%95%E8%BF%9B%E8%A1%8C%E6%8E%92%E5%BA%8F%E6%97%B6%E5%BF%BD%E7%95%A5%E8%BF%9E%E5%AD%97%E7%AC%A6.png)
答案1
如果你想摆脱全部连字符进行排序,您可以这样做,\index{firstlove@first-love}
当输入时,它基本上会自动执行\Index{first-love}
。
\documentclass{article}
\usepackage{imakeidx}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\Index}{om}
{
\str_set:Nn \l_tmpa_str { #2 }
\str_if_in:NnTF \l_tmpa_str { - }
{
\str_set_eq:NN \l_tmpb_str \l_tmpa_str
\str_remove_all:Nn \l_tmpb_str { - }
\use:x
{
\exp_not:N \index
\IfValueT { #1 } { [ #1 ] }
{ \str_use:N \l_tmpb_str @ \str_use:N \l_tmpa_str }
}
}
{
\use:x
{
\exp_not:N \index
\IfValueT { #1 } { [ #1 ] }
{ \str_use:N \l_tmpa_str }
}
}
}
\ExplSyntaxOff
\makeindex[columns=1]
\begin{document}
Test
\Index{firstaid}
\Index{first-love}
\printindex
\end{document}
如果你想保留\index
:
\documentclass{article}
\usepackage{imakeidx}
\usepackage{xparse}
\AtBeginDocument{%
\let\originalindex\index
\let\index\newindex
}
\ExplSyntaxOn
\NewDocumentCommand{\newindex}{om}
{
\str_set:Nn \l_tmpa_str { #2 }
\str_if_in:NnTF \l_tmpa_str { - }
{
\str_set_eq:NN \l_tmpb_str \l_tmpa_str
\str_remove_all:Nn \l_tmpb_str { - }
\use:x
{
\exp_not:N \originalindex
\IfValueT { #1 } { [ #1 ] }
{ \str_use:N \l_tmpb_str @ \str_use:N \l_tmpa_str }
}
}
{
\use:x
{
\exp_not:N \originalindex
\IfValueT { #1 } { [ #1 ] }
{ \str_use:N \l_tmpa_str }
}
}
}
\ExplSyntaxOff
\makeindex[columns=1]
\begin{document}
Test
\index{firstaid}
\index{first-love}
\printindex
\end{document}