Make list of indexed keywords for each section [closed]

I made an index in my document with the makeidx package and \index{} command. Now I would like to summarize all indiced entries in front of each section (in form of a set of the keywords inside brackets and without page numbers) which are used in the section. Is that possible?

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{makeidx}
\makeindex

\begin{document}

\section{Section 1}
% Display here all indexes used in Section 1
\index{First index}
\index{Second index}

\section{Section 2}
% Display here all indexes used in Section 2
\index{Third index}

\printindex

\end{document}

• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Dec 27 '14 at 7:55

This is a preliminary version, using imakeidx and its multiple-index-feature.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{imakeidx}
\usepackage{totcount}
\usepackage{assoccnt}%
\usepackage{forloop}
\usepackage{blindtext}

\newtotcounter{totalsections}
\newcounter{loopcounter}

\makeindex[name=indexsec1,title={Index of section 1}]
\makeindex[name=indexsec2,title={Index of section 2}]

\DeclareAssociatedCounters{section}{totalsections}

\begin{document}

\section{Section 1}
% Display here all indexes used in Section 1
\index[indexsec\number\value{totalsections}]{First index}

\index[indexsec\number\value{totalsections}]{Second index}

\printindex[indexsec\number\value{totalsections}]

\section{Section 2}
% Display here all indexes used in Section 2
\index[indexsec\number\value{totalsections}]{Third index}

\printindex[indexsec\number\value{totalsections}]

\end{document}


Improved version with automatic usage of index[indexsec#]

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{imakeidx}
\usepackage{totcount}
\usepackage{assoccnt}%
\usepackage{forloop}
\usepackage{blindtext}

\usepackage{xpatch}

\newcounter{totalsections}

\DeclareAssociatedCounters{section}{totalsections}%

\newcounter{loopcounter}

\newcommand{\indexsecname}{indexsec}

\newcommand{\sectionindexprefix}[1]{%
\indexsecname\number\value{#1}%
}%

\newcounter{maxindexsections}
\setcounter{maxindexsections}{10}

\forloop{loopcounter}{1}{\value{loopcounter} <  \numexpr\value{maxindexsections}+1}{%
\makeindex[name=\sectionindexprefix{loopcounter}]
}%

\let\indexoriginal\index

\renewcommand{\index}[2][\sectionindexprefix{totalsections}]{%
\indexoriginal[#1]{#2}
}%

\makeatletter
\AtEndDocument{%
\ifnumgreater{\value{totalsections}}{0}{\IfFileExists{\sectionindexprefix{totalsections}.idx}{\printindex[\sectionindexprefix{totalsections}]}{}}{}%
%  \immediate\write\@auxout{\string\setcounter{numberofsections}{\number\totvalue{totalsections}}}
}%

\xpretocmd{\@sect}{\ifnumgreater{\value{totalsections}}{0}{\IfFileExists{\sectionindexprefix{totalsections}.idx}{\printindex[\sectionindexprefix{totalsections}]}{}}{}}{}{}
\makeatletter

\begin{document}

\section{Section 1}
% Display here all indexes used in Section 1
\blindtext[10]

\index{First index}

\index{Second index}

\index{Intermediate index of first section}

\section{Section 2}
% Display here all indexes used in Section 2
\index{Third index}

\section{Section 3}
\blindtext[10]
\index{Fourth index}

\end{document}

• In the first example i had to add \makeindex to run it properly, but then it worked when producing a "miniindex" at the end of the section. Is it possible to do this right after the section title and more just listening the keywords in a row (no need for page numbers)? I.e. output: Section 2 (MV frontier, correlation, tangency portfolio) – Heikkisorsa Dec 27 '14 at 11:48
• @SebastianBüchler: You are asking another question not contained in your OP. In principle, my initial solution should work, if shell-escape is enabled, no additional need to use \makeindex – user31729 Dec 27 '14 at 14:17
• Actually the positioning part is visible in the MWE (display before any definition of indexes). The other part regarding how it should be displayed was just not specified enough by 'summarize all entries'. – Heikkisorsa Dec 27 '14 at 15:42
• @SebastianBüchler: You edited the post away from the initial question, such that my solution is effectively useless – user31729 Dec 27 '14 at 15:56
• @ChristanHupfer: This is not true, I precised it due to confusion. You interpreted it just the way you wanted it and now you're stubborn on that. I think we are on the right way, but need further development on the answer. – Heikkisorsa Dec 28 '14 at 11:45