Table of Contents Keywords – How to Insert Keywords & Key Phrases into TOC

table of contents

To make the structure of documents (here: my PhD-thesis) comprehensible straight from the ToC, I would like to insert some key words or key phrases in each element of the ToC. I saw this device in a teaching book and I thought it would be a helpful little improvement for longer documents.

My first description of the desired layout lacked of precision, I hope this one is more clear.

Requirements for user-friendlybility:
I am looking for a solution that handles key words or key phrases which belong to one part, section etc. in the following way:

a) puts key words/phrases in rows

b) divides them through hyphens

c) one line under the actual part, section etc.

d) key words/phrases without page number

e) depending on the number of key words/phrases it can exceed one line

It should look like this:

Contents

1 section ………………………..1

key word 1 – key phrase 2 – key word 3

1.1 subsection ………………………….5

key word 1 – key phrase 2 – key word 3 –

key word 4 – key phrase 5 – key word 6

Requirement for writer-friendlybility: To make it easier for the writer of the document one command is hoped-for that adapts to a given section, part etc. without any specification within the command in order to prevent him from adjusting the command every time when he is copying it from one sections to a part, subsection, etc.

In the .tex-file it should look like this:

\begin{document}
\section{section} 
bla bla \command{keyword1} few lines or pages of bla bla \command{keyword2} ....

\subsection{subsection} 
bla bla \command{keyword1} few lines or pages of bla bla \command{key phrase2} ....
\end{document}

Does anyone have an idea how to do it?

PS: To convert the keywords in the toc into hyperref-keywords take a look here: Hyperref-ize the keywords in toc. It works with egregs solution.

Best Answer

\documentclass[a4paper]{article}
\usepackage{etoolbox}

\makeatletter
\newif\ifsection
\newif\ifsubsection
\newtoks\keywordstoks
\preto\section{\flushkeywords\sectiontrue\subsectionfalse}
\preto\subsection{\flushkeywords\sectionfalse\subsectiontrue}
\preto\enddocument{\flushkeywords}

\newcommand{\flushkeywords}{%
  \ifsection\addtocontents{toc}{\formatkwsection{\protect\@gobble\the\keywordstoks\relax}}\fi
  \ifsubsection\addtocontents{toc}{\formatkwsubsection{\protect\@gobble\the\keywordstoks\relax}}\fi
  \keywordstoks={}}

\newcommand{\keyword}[1]{\@bsphack\keywordstoks=\expandafter{\the\keywordstoks\kwsep#1}\@esphack}
\newrobustcmd{\kwsep}{~-- }
\newrobustcmd{\formatkwsection}[1]{#1\par\medskip}
\newrobustcmd{\formatkwsubsection}[1]{{\leftskip=2.2em\relax#1\par}\smallskip}
\makeatother

\begin{document}

\tableofcontents

\section{Section}

Abc \keyword{key1} def \keyword{key2}

\subsection{Subsection}

Ciao \keyword{key3} ciao \keyword{key4}

\subsection{Another}

x

\end{document}

I'll leave to the OP the task of redefining \formatkwsection and \formatkwsubsection to suit his needs.

Related Question