# How to split with chapter, section, … Titles in the index part?

Maybe the title isn't clear.

In fact, I'm taking note during my course, and sometimes, teachers said that a thing is a pipe (tip, stuff to know) for the examination. Since the tex have a few of them, and more then 1000 lines when finished, I'm using \usepackage[makeindex]{imakeidx} to be able to have multiple index, and one only with the pipe (but there some others index with other content made the same way).

Works fine, but the problem is, I don't have the part-chapter-section-subsection-subsubsection title reference (and having paragraph and sub title reference could be nice too).

Is there a way to have it ? By adding a column, putting the entire title (chapter etc.) before the indexed sentence ?

Here, an exemple of what I'd like it look like :

Or, if not possible, like if titles (of the part, chapter, etc.) was add before (or after, I don't really care) the page number :

I've put the page number in bold to more clear, but it's not need.

The aim here, is to be able to know immediately, without having to look where it is with the page number (same with the number of the part/chapter/section... Since it could change very easly), where is the indexed text in the document, and so about what is talking about.

-

The following solution uses the same method as the table of contents is generated. The entries for the list of important things are written to the .aux file, then at the end of the document LaTeX writes it to the file \jobname.imp. In the next run this file is read and a table is set.

\documentclass{report}
\usepackage{longtable}

\makeatletter
\newlength\ImpLineWidth
\newtoks\ImpToks
\newcommand*{\printimportant}{%
\clearpage
\section*{\centering Index of important things}%
\setlength{\ImpLineWidth}{\linewidth}%
\begin{longtable}{@{}p{.6\ImpLineWidth}p{.4\ImpLineWidth}@{}}%
\global\ImpToks{}%
\@starttoc{imp}%
\the\ImpToks
\end{longtable}%
}
\newcommand*{\importantentry}[2]{%
\global\ImpToks\expandafter{%
\the\ImpToks
\centering #1&
\centering #2\tabularnewline
}%
\ignorespaces
}
\newcommand*{\important}[1]{%
\protect\importantentry{#1}{%
\ifnum\value{part}=0 %
\else
part~\thepart, %
\fi
\ifnum\value{chapter}=0 %
\else
chapter~\thechapter, %
\ifnum\value{section}=0 %
\else
section~\thesection, %
\fi
\fi
page~\thepage
}%
}%
}
\makeatother
\begin{document}
\part{First part}
\important{Some indexed \dots}
\chapter{First chapter}
\important{Again}
\section{First section}
\important{Idem 1}
\chapter{Second chapter}
\important{Idem 2}
\printimportant
\end{document}


## Index based variant

The following uses the index machinery. For the case that there is already an index I have used package index to get a second one. (There are other packages like multind, …)

The sectioning titles are usually not available. Exception is the latest title that packages like titleref, nameref or zref-titleref remember. For the best control of the numbering and title, the sectioning commands needs to be patched. Here I have used a shortcut via \addcontentsline assuming that only sectioning titles are relevant that also goes into the table of contents. A glitch remains, the large space between the part number and the part title, the standardclass uses the hardcoded \hspace{1em} here. But anyway, you are using a different unknown setup as the word numbering shows. For simplicity it is also assumed that the part title is always present.

The first LaTeX run of test.tex generates test.imx. The final index file test.imp is generated via:

makeindex -o test.imp test.imx


Also a different style file than the standard settings can be used to control the output of the final index file.

\documentclass{report}
\usepackage{index}
\newindex{imp}{imx}{imp}{Index of important things}

\makeatletter
\newlength\ImpLineWidth
\newtoks\ImpToks
\newcommand*{\printimportant}{%
\begingroup
\def\numberline##1{##1:~}%
\renewcommand*{\theindex}{%
\clearpage
\section*{\centering Index of important things}%
\setlength{\parskip}{1ex plus .5ex minus .25ex}%
\setlength{\parindent}{0pt}%
\let\item\@idxitem
}{%
\clearpage
}%
\printindex[imp]%
\endgroup
}

% Remember sectioning titles
\newcommand*{\imp@toc}{toc}
\edef\imp@temp{#1}%
\ifx\imp@temp\imp@toc
\begingroup
\expandafter\protected@xdef\csname imp@current@#2\endcsname{#3}%
\csname imp@cleanup@#2\endcsname
\endgroup
\fi
}%
}
% Clear subordinate sectioning titles
\def\imp@temp#1#2{%
\expandafter\newcommand\csname imp@cleanup@#1\endcsname{%
\global\expandafter\let\csname imp@current@#2\endcsname\@empty
\csname imp@cleanup@#2\endcsname
}%
}
\imp@temp{part}{chapter}
\imp@temp{chapter}{section}
\imp@temp{section}{subsection}

\newcommand*{\important}[2][]{%
\def\imp@temp{#1}%
\protected@edef\imp@temp{%
\noexpand\index[imp]{%
\ifx\imp@temp\@empty
\else
#1@%
\fi
#2; %
% assuming the part is always present.
part~\csname imp@current@part\endcsname
\ifx\imp@current@chapter\@empty
\else
, chapter~\csname imp@current@chapter\endcsname
% section, ...
\fi
|textbf%
}%
}%
\imp@temp
}
\makeatother
\begin{document}
\tableofcontents
\part{First part}
\important{Some indexed \dots}
\chapter{First chapter}
\important{Again}
\section{First section}
\important{Idem 1}
\chapter{Second chapter}
\important{Idem 2}
\printimportant
\end{document}


-
Impressive, but not exactly my goal. Here, you're doing your own kind of index. Clearly, I could use it. However, it seems to only put the number of the part/chapter, etc. And not the name, which is the most important for me. And, it doesn't use index, so the data aren't sort. Finally, I've forgot to mention it, but I would prefer the 1st solution rather than the 2nd. What you did, so quickly is great, but maybe for a in case of solution (because pretty time-eater to put in place). Thanks a lot. – Julien Oct 2 '12 at 22:53
In the first example the sorting order is document order, the second example does not specify any order. Also the examples do not show any chapter/section/... titles. Therefore the question should be refined. – Heiko Oberdiek Oct 2 '12 at 23:11
Indeed, sorry. I've realized it a bit after reading your solution. Is it more clear now ? By the way, your solution is quite cool, for both the result, but also the means. – Julien Oct 2 '12 at 23:46
Sorry, it's not too clear for me. The second example is too short and without explicit page numbers to show the data sorting. And do you really want to sort the entries inside part/chapter, but keeping document order for parts/chapters? – Heiko Oberdiek Oct 3 '12 at 0:40
Impressive ! But, 5 questions remains : what does ~ do ? Why are you using \section*{\centering Index of important things}% line ? Since "Index of important things" is your one of you're index title, you shouldn't have to do it, no ? Why the text is only in the index ? Why make a new command in stead of using the \printindex one ? And, would it work with \usepackage[makeindex]{imakeidx} ? Anyway, thanks a lot. – Julien Oct 3 '12 at 15:41