\dotfill 和长索引条目产生的断行问题

\dotfill 和长索引条目产生的断行问题

首先,MWE 及其索引页如下。

\documentclass[12pt]{article}

%%%index setting%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{imakeidx}
\usepackage[colorlinks,linkcolor=blue]{hyperref}
\usepackage[columns=2,columnsep=2em,%rule=0pt,
initsep=1\baselineskip plus 0.2ex minus 0.1ex,%
hangindent=1em,subindent=1em,hangindent=1em,subsubindent=2em]
{idxlayout}%must be after imakeidx

%Index format-----------------------------------------
\usepackage{filecontents}
\begin{filecontents}[overwrite]{\jobname.mst}
headings_flag 1
heading_prefix "{\\bfseries "
    heading_suffix "}\\nopagebreak\n"
delim_0 "\\dotfill "
delim_1 "\\dotfill "
delim_2 "\\dotfill "
delim_n "\\idxBdelim"%%change comma before 'see also' for semicolon
\end{filecontents}
\makeindex
%change comma before 'see also' for semicolon---------------
\makeatletter
\def\idxBdelim{\@ifnextchar{\hyperindexformat}{; }{, }}
\def\@commahyperpage#1{\@@commahyperpage#1\idxBdelim{}\idxBdelim{}\\}
\def\@@commahyperpage#1\idxBdelim#2\idxBdelim#3\\{%
  \ifx\\#2\\%
    \HyInd@pagelink{#1}%
  \else
    \HyInd@pagelink{#1}\idxBdelim\HyInd@pagelink{#2}%
  \fi
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{document}

some text\index{red apple} \index{alpple!red \~{}} \index{yellow!apple}\index{alpple!yellow \~{}} \index{green!apple} \index{alpple!green \~{}}with some index entries\index{white pear} \index{pear!white \~{}}

index{yellow!pear} \index{pear!yellow \~{}} \index{green!pear} \index{pear!green \~{}}
\index{yellow!many many many many many many alpple}
\index{alpple!yellow many many many many many many \~{}}

\newpage
and some\index{yellow!pear} \index{pear!yellow \~{}} \index{green!pear} \index{pear!green \~{}}\emph{see also} entries as well.

\index{red apple} \index{alpple!red \~{}} \index{yellow!apple}\index{alpple!yellow \~{}} \index{green!apple} \index{alpple!green \~{}} \index{white pear} \index{pear!white \~{}}



\newpage
\null
\index{alpple!red \~{}|seealso{red apple}}
\index{alpple!yellow \~{}|seealso{yellow apple}}
\index{alpple!yellow many many many many many many \~{}|seealso{yellow many many many many many many apple}}
\index{alpple!green \~{}|seealso{green apple}}
\index{pear!white \~{}|seealso{white pear}}
\index{pear!yellow \~{}|seealso{yellow pear}}
\index{pear!green \~{}|seealso{green pear}}

\printindex
\end{document}

在此处输入图片描述

如你所见,索引条目的词条布局很好,除了下面两个 apple 子条目。如果没有“另见”说明但页码很多,问题也存在。

在此处输入图片描述

那么有没有办法让这么长的条目输出如下,即每行条目的左右两侧都对齐?顺便说一句,我知道索引条目上的这种对齐问题非常困难,因此,无论是否能解决这个问题,任何努力都是值得欢迎和赞赏的。提前致谢!

在此处输入图片描述

答案1

我觉得这是不可能的。相反,我有这个想法让它看起来更好一些。

\documentclass[12pt]{article}

%%%index setting%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{imakeidx}
\usepackage[colorlinks,linkcolor=blue]{hyperref}
\usepackage[columns=2,columnsep=2em,%rule=0pt,
initsep=1\baselineskip plus 0.2ex minus 0.1ex,%
hangindent=1em,subindent=1em,hangindent=1em,subsubindent=2em]
{idxlayout}%must be after imakeidx

%Index format-----------------------------------------
\begin{filecontents}[overwrite]{\jobname.mst}
headings_flag 1
heading_prefix "{\\bfseries "
    heading_suffix "}\\nopagebreak\n"
delim_0 "\\dotfill "
delim_1 "\\dotfill "
delim_2 "\\dotfill "
delim_n "\\idxBdelim"%%change comma before 'see also' for semicolon
delim_t ""
\end{filecontents}
\makeindex
%change comma before 'see also' for semicolon---------------
\makeatletter
\def\idxBdelim{\@ifnextchar{\hyperindexformat}{\newline\null\hfill}{, }}
\def\@commahyperpage#1{\@@commahyperpage#1\idxBdelim{}\idxBdelim{}\\}
\def\@@commahyperpage#1\idxBdelim#2\idxBdelim#3\\{%
  \ifx\\#2\\%
    \HyInd@pagelink{#1}%
  \else
    \HyInd@pagelink{#1}\idxBdelim\HyInd@pagelink{#2}%
  \fi
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{document}

some text\index{red apple} \index{alpple!red \~{}} \index{yellow!apple}\index{alpple!yellow \~{}} \index{green!apple} \index{alpple!green \~{}}with some index entries\index{white pear} \index{pear!white \~{}}

index{yellow!pear} \index{pear!yellow \~{}} \index{green!pear} \index{pear!green \~{}}
\index{yellow!many many many many many many alpple}
\index{alpple!yellow many many many many many many \~{}}

\newpage
and some\index{yellow!pear} \index{pear!yellow \~{}} \index{green!pear} \index{pear!green \~{}}\emph{see also} entries as well.

\index{red apple} \index{alpple!red \~{}} \index{yellow!apple}\index{alpple!yellow \~{}} \index{green!apple} \index{alpple!green \~{}} \index{white pear} \index{pear!white \~{}}



\newpage
\null
\index{alpple!red \~{}|seealso{red apple}}
\index{alpple!yellow \~{}|seealso{yellow apple}}
\index{alpple!yellow many many many many many many \~{}|seealso{yellow many many many many many many apple}}
\index{alpple!green \~{}|seealso{green apple}}
\index{pear!white \~{}|seealso{white pear}}
\index{pear!yellow \~{}|seealso{yellow pear}}
\index{pear!green \~{}|seealso{green pear}}

\printindex
\end{document}

在此处输入图片描述

相关内容