I am using imakeidx. How do I group entries in the (person) index in a way that they do not get split across page boundaries? My goal here is to have groups of surnames "Smith" and a group of "Jones", plus 1-12 first names each, in the index, and keep those groups on the same page.
I have the feeling that some application of \nopagebreak might solve it, but where?
Here is a pdflatex MWE and below an image of the output, with a big red rectangle as annotation to show where the surname in index gets separated from the several first names. All the "Wilson" names should be on same page.
The MWE is pdflatex file:
\documentclass{article} \usepackage{imakeidx} \makeindex[name=person, title={Index},options=-s example.ist, columns=1] % Use .ist for dotfill \makeindex \begin{document} Page1 \index[person]{{Smith}!{John1 , something}} \index[person]{{Smith}!{John2 , something}} \index[person]{{Smith}!{John3 , something}} \index[person]{{Johnson}!{John1 , something}} \index[person]{{Johnson}!{John2 , something}} \index[person]{{Johnson}!{John3 , something}} \index[person]{{Williams}!{John1 , something}} \index[person]{{Williams}!{John2 , something}} \index[person]{{Williams}!{John3 , something}} \index[person]{{Brown}!{John1 , something}} \index[person]{{Brown}!{John2 , something}} \index[person]{{Brown}!{John3 , something}} \index[person]{{Jones}!{John1 , something}} \index[person]{{Jones}!{John2 , something}} \index[person]{{Jones}!{John3 , something}} \index[person]{{Miller}!{John1 , something}} \index[person]{{Miller}!{John2 , something}} \index[person]{{Miller}!{John3 , something}} \index[person]{{Davis}!{John1 , something}} \index[person]{{Davis}!{John2 , something}} \index[person]{{Davis}!{John3 , something}} \index[person]{{Wilson}!{John1 , something}} \index[person]{{Wilson}!{John2 , something}} \index[person]{{Wilson}!{John3 , something}} \index[person]{{Taylor}!{John1 , something}} \index[person]{{Taylor}!{John2 , something}} \index[person]{{Taylor}!{John3 , something}} \index[person]{{Martinez}!{John1 , something}} \index[person]{{Martinez}!{John2 , something}} \index[person]{{Martinez}!{John3 , something}} \index[person]{{Thompson}!{John1 , something}} \index[person]{{Thompson}!{John2 , something}} %\index[person]{{Thompson}!{John3 , something}} \index[person]{{Lee}!{John1 , something}} \index[person]{{Lee}!{John2 , something}} \index[person]{{Lee}!{John3 , something}} \printindex[person] \end{document} which uses the one line style example.ist
delim_1 "\\nobreak\\dotfill " All surnames and first names grouped nicely, good. However, the name "Wilson" (and its three first names) is split across two pages. How do I that?
I tried in the .ist style to add \nopagebreak but nothing has the desired effect. For example
% group_skip "\n\n \\nopagebreak\\indexspace\n" % item_1 "\\par\\nopagebreak" % item_0 "\\nopagebreak" % prefix "\\par\\nopagebreak" % delim_1 "\\nobreak\\nopagebreak\\dotfill " % delim_1 "\\nobreak\\par\\nopagebreak\\dotfill " % delim_1 "\\nopagebreak\\dotfill " % delim_1 "\\nolinebreak\\dotfill " How to massage the .ist style to keep the groups of surnames plus 1-12 first names on the same page? In this case, I would expect an extra empty line of page 2 and "Wilson" plus 3 first names on page as a group.
PS: While I look at the initial comments, I realize that the example above concentrates on 1-12 sub-items (first names), the full latex file has a few instances with almost 100 sub-items (first names) spanning more than a page which leads to additional challenges. Ideally, a solution would handle the whole range.
Just a wild guess, but if we choose a threshold N and can have all groups of N together and split larger ones, would that work? Maybe something simple like
if there are less than N==5 sub-items/names then keep them together else do nothing and break as usual might work? But how to code that?
PS2: Thank you for all your help so far. In the first answer, a solution is suggested that works it seems, but not for wrapped lines in index. Am adding here a second MWE to demonstrate and a second image to visualize the issue.
\documentclass{article} \usepackage{imakeidx} % ---- \makeatletter \newcommand{\nobreakafteritem}{} \newcommand{\setnobreakafteritem}{\renewcommand{\nobreakafteritem}{\par\penalty10000\relax \renewcommand{\nobreakafteritem}{}}} \usepackage{etoolbox}\pretocmd\subitem{\nobreakafteritem}{}{\fail} \usepackage{etoolbox}\pretocmd\@idxitem{\setnobreakafteritem}{}{\fail} \makeatother %---------------- \makeindex[name=person, title={Index},options=-s example.ist, columns=1] % Use .ist for dotfill \makeindex \begin{document} Page1 \index[person]{{Smith}!{John1 , something}} \index[person]{{Smith}!{John2 , something}} \index[person]{{Smith}!{John3 , something}} \index[person]{{Johnson}!{John1 , something}} \index[person]{{Johnson}!{John2 , something}} \index[person]{{Johnson}!{John3 , something}} \index[person]{{Williams}!{John1 , something}} \index[person]{{Williams}!{John2 , something}} %\index[person]{{Williams}!{John3 , something}} %ORG \index[person]{{Williams}!{John3 , something very long wrap the line extra long and longer and longer and longer}} \index[person]{{Brown}!{John1 , something}} \index[person]{{Brown}!{John2 , something}} \index[person]{{Brown}!{John3 , something}} \index[person]{{Jones}!{John1 , something}} \index[person]{{Jones}!{John2 , something}} \index[person]{{Jones}!{John3 , something}} \index[person]{{Miller}!{John1 , something}} \index[person]{{Miller}!{John2 , something}} \index[person]{{Miller}!{John3 , something}} \index[person]{{Davis}!{John1 , something}} \index[person]{{Davis}!{John2 , something}} \index[person]{{Davis}!{John3 , something}} \index[person]{{Wilson}!{John1 , something}} \index[person]{{Wilson}!{John2 , something}} %\index[person]{{Wilson}!{John3 , something}} \index[person]{{Taylor}!{John1 , something}} \index[person]{{Taylor}!{John2 , something}} \index[person]{{Taylor}!{John3 , something}} \index[person]{{Martinez}!{John1 , something}} \index[person]{{Martinez}!{John2 , something}} \index[person]{{Martinez}!{John3 , something}} \index[person]{{Thompson}!{John1 , something}} \index[person]{{Thompson}!{John2 , something}} \index[person]{{Thompson}!{John3 , something}} % \index[person]{{Lee}!{John1 , something}} \index[person]{{Lee}!{John2 , something}} \index[person]{{Lee}!{John3 , something}} \printindex[person] \end{document} 

\usepackage{etoolbox}\pretocmd\subitem{\par\nobreak}{}{\fail}could work.\usepackage{etoolbox}\pretocmd\subitem{\par\widowpenalty 10000\raggedbottom}{}{\fail}but I do not know how to set the penalties such that groups of 1 to 10 stay to together and larger groups can be broken up.