删除索引中的虚假分页符?

删除索引中的虚假分页符?

编辑:澄清我的问题:我不是在寻找一个没有分页符就开始的索引;相反,我想要一个我可以手动处理分页符的索引。

假设您插入了大量\clearpages,以确保“下一页”从第 37 页开始。现在您想将索引“插入”到第 37 页 - 因此您\printindex[names]在那里输入 - 令人惊讶的是 - 索引现在从第 38 页开始,因为索引决定再输入一个,\clearpage :/这意味着现在您必须返回并重新输入\clearpages 的数量,以便它们与第 N-1 页匹配;这样,与额外的分页符一起,索引将从第 N 页开始。我认为这是一种不必要的复杂化。

其次,假设您不想使用默认的章节标题\printindex;您宁愿使用\section*。最容易想到的(不知道有什么更好的)是隐藏默认的索引标题(在这种情况下,由于示例是针对\printindex来自index包的,因此它将在\newindex命令中):

\newindex{names}{ndx}{nnd}{}  % instead of: {names}{ndx}{nnd}{Name Index}

...(这确实会抑制默认索引标题) - 然后,而不是该标题,人们会输入后面\section*{Name Index}跟着的\printindex[names]。显然,如果\printindex插入自己的分页符,那么这种方法将不起作用,因为部分标题和索引的第一页将位于不同的页面上 - 这正是这个问题中的 MWE 所展示的......


该死,我真的开始讨厌 Latex:/ 了……:)

看一下以下 MWE:

\documentclass[a4paper,twoside,11pt]{book}


% must load index before hyperref, else no hyperlinks!
\usepackage{index}
\usepackage{hyperref}
\usepackage{trace}

% declare new index files
\newindex{names}{ndx}{nnd}{Name Index}
\newindex{titles}{tdx}{tnd}{Title Index}

\usepackage{forloop}
\usepackage{trace}

% expandable modulo operation
\def\truncdiv#1#2{((#1-(#2-1)/2)/#2)}
\def\moduloop#1#2{(#1-\truncdiv{#1}{#2}*#2)}
\def\modulo#1#2{\number\numexpr\moduloop{#1}{#2}\relax}


% needed to generate index files
\newcounter{ct}
\def\modme{10}

% generate names (.nnd) index file
\newwrite\tempnnd
\immediate\openout\tempnnd=\jobname.nnd
\immediate\write\tempnnd{\protect\noexpand\begin{theindex}}

\forLoop[1]{0}{255}{ct}{
    \immediate\write\tempnnd{%
      \protect\noexpand\item Surname\thect, Name\thect, %
      %\protect\noexpand\hyperpage{\thect}
      % hyperlink all to page 1, 
      % to avoid Latex complaining:
      \protect\noexpand\hyperpage{1} %
    }
    \ifnum\modulo\thect\modme=0
      \immediate\write\tempnnd{}
      \immediate\write\tempnnd{\protect\noexpand\indexspace}
      \immediate\write\tempnnd{}
    \else
    \fi
}
\immediate\write\tempnnd{\protect\noexpand\end{theindex}}
\immediate\closeout\tempnnd

% generate titles (.tnd) index file
\newwrite\temptnd
\immediate\openout\temptnd=\jobname.tnd
\immediate\write\temptnd{\protect\noexpand\begin{theindex}}

\forLoop{0}{255}{ct}{
    \immediate\write\temptnd{%
      \protect\noexpand\item \noexpand\emph {Title of Work\thect}, %
      %\protect\noexpand\hyperpage{\thect}
      % hyperlink all to page 1, 
      % to avoid Latex complaining:
      \protect\noexpand\hyperpage{1} %
    }
    \ifnum\modulo\thect\modme=0
      \immediate\write\temptnd{}
      \immediate\write\temptnd{\protect\noexpand\indexspace}
      \immediate\write\temptnd{}
    \else
    \fi
}
\immediate\write\temptnd{\protect\noexpand\end{theindex}}
\immediate\closeout\temptnd


\begin{document}

\clearpage

\section*{Test 1}
\addcontentsline{toc}{section}{Test 1}

\phantomsection % NOTE NO PAGEBREAK PRESENT AROUND HERE!
\addcontentsline{toc}{chapter}{Name Index} \traceon
\printindex[names] \traceoff

\phantomsection
\addcontentsline{toc}{chapter}{Title Index} % master TOC and PDF bookmark
\printindex[titles] % the title index

\end{document}

输出的前两页是:

测试.png

生成于:

montage -bordercolor LimeGreen -border 1 -density 100 test.pdf[0] test.pdf[1] -geometry +2+2 -tile 2x1 test.png

我的问题是:

有人能向我解释为什么在名称索引部分之前有一个分页符吗?以及我该如何删除它?

 

正如在 MWE 中看到的,我甚至尝试对其进行追踪:

$ grep -C10 'Surname0' b.log
{into \Hy@temp=macro:->}
{\ifx}
{true}
{\else}
{blank space  }
{\relax}

\item ->\par \hangindent 40\p@                 <= \item
{\par}                                         <= \par (?)
@firstpass
[]\OT1/cmr/m/n/10.95 Surname0, Name0, []0[]    <= typeset Surname0
@\par via @@0 b=0 p=-10000 d=100
@@1: line 1.2- t=100 -> @@0

{changing \hangindent=40.0pt}
{into \hangindent=0.0pt}
%% goal height=476.5225, max depth=5.5         <= pagebreak?!
% t=0.0 g=476.5225 b=10000 p=0 c=100000#       <=    WHERE FROM??
{vertical mode: \hangindent}
{changing \hangindent=0.0pt}
{into \hangindent=40.0pt}

...但我只是不明白哪个命令会导致新页面第一个名字排版好了?!如果分页符是在Surname0排版之后出现的,那为什么不在Surname0第一页?!

答案1

这个问题看起来相当抽象,因为索引开始时没有分页符的情况非常少见。然而,这是一种可能性。由于您不想要分页符,因此有必要避免索引生成中的任何自动化。

\documentclass[a4paper,twoside,11pt]{book}

\usepackage[noautomatic,nonewpage]{imakeidx}
\usepackage{hyperref}

% declare new index files
\makeindex[name=names,title=Name Index,intoc]
\makeindex[name=titles,title=Title Index,intoc]
\indexsetup{level=\section*} % no page break with sections

\begin{document}

\tableofcontents

% generate fake index entries
\count255=0
\loop\ifnum\count255<200
  \index[names]{Surname\number\count255, Name\number\count255}
  \index[titles]{Title of Work\number\count255}
  \advance\count255 1
\repeat

\clearpage

\phantomsection
\section*{Test 1}
\addcontentsline{toc}{section}{Test 1}

\printindex[names]
\printindex[titles]

\end{document}

答案2

写入序言:

\documentclass[a4paper,twoside,11pt]{book}

\usepackage{index}
\usepackage{multicol}
\makeatletter
\renewenvironment{theindex}
  {{\huge\indexname}\par%
   \@mkboth{\MakeUppercase\indexname}{\MakeUppercase\indexname}%
   \parindent\z@
   \parskip\z@ \@plus .3\p@\relax
   \let\item\@idxitem
   \begin{multicols}{2}}
  {\end{multicols}}
\makeatother
...

答案3

非常感谢@Werner(在 OP 评论中)提供的最重要的指点!

不幸的是,我没有查看book.clslatex.ltx,而是试着查看index.sty;但这并没有显示任何内容,除了几个\clearpages(应该运行,而不是在索引排版之前)。

theindex和的定义\twocolumn可以通过 轻松确认texdef

$ texdef -t latex -c book -p index \theindex -f
\theindex first defined in "book.cls".

\theindex:
\long macro:->\edef \indexname {\the \@nameuse {idxtitle@\@indextype }}\if@twocolumn \@restonecolfalse \else \@restonecoltrue \fi \columnseprule \z@ \columnsep 35\p@ \twocolumn [\@makeschapterhead {\indexname }\ifx \index@prologue \@empty \else \index@prologue \bigskip \fi ]\@mkboth {\MakeUppercase \indexname }{\MakeUppercase \indexname }\thispagestyle {plain}\parindent \z@ \parskip \z@ \@plus .3\p@ \relax \let \item \@idxitem 

2$ texdef -t latex -c book -p index \twocolumn -f
\twocolumn is defined by (La)TeX.

\twocolumn:
macro:->\clearpage \global \columnwidth \textwidth \global \advance \columnwidth -\columnsep \global \divide \columnwidth \tw@ \global \hsize \columnwidth \global \linewidth \columnwidth \global \@twocolumntrue \global \@firstcolumntrue \col@number \tw@ \@ifnextchar [\@topnewpage \@floatplacement 

注:我盯着看了\twocolumn几分钟才意识到这\clearpage是第一个标记:)

因此,我们最终可以在打印索引之前插入这个重新定义:

....
\makeatletter
\def \twocolumn {%
  %\clearpage
  \global\columnwidth\textwidth
  \global\advance\columnwidth-\columnsep
  \global\divide\columnwidth\tw@
  \global\hsize\columnwidth
  \global\linewidth\columnwidth
  \global\@twocolumntrue
  \global\@firstcolumntrue
  \col@number \tw@
  \@ifnextchar [\@topnewpage\@floatplacement
}
\makeatother

\phantomsection % NOTE NO PAGEBREAK PRESENT AROUND HERE!
\addcontentsline{toc}{chapter}{Name Index} %\traceon
\printindex[names] %\traceoff

\phantomsection
\addcontentsline{toc}{chapter}{Title Index} % 
\printindex[titles] % 

\end{document}

... 确实如此 - 分页符终于消失了!但是,现在还有另一个问题 - 即使你像这样隐藏标题:

\newindex{names}{ndx}{nnd}{}

... 索引的布局仍然会变得混乱 - 鉴于索引是以双列模式排版的 - 看起来,“ \section*{Test 1}”会将左列“推”下来 - 所以看起来不太好看:

测试.png

...但至少分页符消失了:)

相关内容