# Insert key words & key phrases into toc

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.

• Welcome to tex.sx! Usually, we don't put a greeting or a "thank you" in our posts. While it might seem strange at first, it is not a sign of a lack of politeness, but rather part of our trying to keep everything very concise. Upvoting the preferred way here to say "thank you" to users who helped you. – doncherry Jun 3 '11 at 20:59
• you can use the command \addtocontens – Marco Daniel Jun 3 '11 at 20:59
• @Marco: it would be great if you write this as answer, perhaps elaborate a bit. – Stefan Kottwitz Jun 3 '11 at 21:25
• I've updated my answer, adding a new example that satisfies all but the last requirement (the sectional unit must be explicitly declared, but this is easily done using a number). – Gonzalo Medina Jun 4 '11 at 14:19

\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}{%
\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.

The tocloft package provides this kind of functionality for chapters: \cftchapterprecistoc; it's not hard to create a similar command for sections. Here's a small example:

\documentclass{report}
\usepackage{tocloft}

% Define a \cftsectionprecistoc
% based on the existing \cftchapterprecistoc
\makeatletter
{\leftskip \cftsecindent\relax
\rightskip \@tocrmarg\relax
\textit{#1}\protect\par}}}
\makeatother
\begin{document}

\tableofcontents
\chapter{A chapter title}
\cftchapterprecistoc{some keywords}

\section{A section title}
\cftsectionprecistoc{some more keywords}
\subsection{A subsection title}
\end{document}


• & Gonzalo: Both ways dont result in the layout I am looking for. I would prefer to have the key words or key phrases which a) belong to one part, section etc. in rows b) divided through hyphens c) one line under the actual part, section etc. (as both of you created) d) key words/phrases without page number. Like: "key word/phrase - key word/phrase - etc.." In addition, I would like to have ONE command that adapts to a given section, part etc. without any specification within the command, because I am copying from sections to parts, subsections, etc. and dont want to adjust all the time. – Philip Jun 4 '11 at 0:22
• @Philip, I'm not sure that you should treat this site as a personal coding resource. Most solutions on the site are proof of concept, and designed to get you started by showing you how you can do things. I think both of our solutions do this quite adequately, especially since you mentioned none of these other requirements in your question. As for the requirements, nothing prevents you formatting the content of the keyword line with hyphens, either with your own markup or manually. We both gave solutions for (c) and (d). Your last requirement isn't very easy, I don't think. – Alan Munn Jun 4 '11 at 1:56
• thank you for your answer and the time for creating the code. I am knew in the forum world and I realy dont know much about the code of these code worlds (maybe a topic for forum question). So I dont know what can be ask and what cannot be ask. But I have the strong feeling that when there is a question that improves the readability of documents (what seems like one of the main purposes of latex) it should be ask - even when it looks like exploiting this site "as a personal coding resource". I admit: my first question was not precise. I hope I changed it to a better way. – Philip Jun 4 '11 at 9:39

You can use the \addtocontents and/or the \addcontentsline commands; you can control many aspects of the text to be added:

\documentclass{book}
\usepackage{lipsum}

\begin{document}
\tableofcontents

{\protect\lipsum[1]}\hfil\par\smallskip}

\chapter{Test chapter}
\lipsum[1]
\addtocontents{toc}{Here we add some text without page number.\par}

\section{Test section}

\lipsum[1-10]

\addcontentsline{toc}{subsection}{Some text formatted like an unnumbered subsection}

\lipsum[1-5]

\subsection{Test subsection}

\addcontentsline{toc}{chapter}{Some text formatted like a chapter and with page number\protect\numberline{}}

\lipsum[1-6]

\section{Another test section}

\end{document}


EDIT: taking into account the new requirements, I defined a new command \AddToToC with two mandatory arguments: the first one indicates the level of the sectional unit (-1 for parts, 0 for chapters, 1 for sections, etc...) and the second one contains the text that will be added to the ToC (the keywords); here's the definition of the command and and example of its use:

\documentclass{book}
\usepackage{ifthen}

\newlength\UnitIndent
\newlength\UnitHangInd

\ifthenelse{\equal{#1}{-1}}
{\gdef\UnitIndent{2em}\gdef\UnitHangInd{2em}}{}
\ifthenelse{\equal{#1}{0}}
{\gdef\UnitIndent{0em}\gdef\UnitHangInd{1.5em}}{}
\ifthenelse{\equal{#1}{1}}
{\gdef\UnitIndent{2.3em}\gdef\UnitHangInd{3.8em}}{}
\ifthenelse{\equal{#1}{2}}
{\gdef\UnitIndent{5.5em}\gdef\UnitHangInd{7em}}{}
}

\begin{document}
\tableofcontents

\part{Test part}
\AddToToC{-1}{some keywords for the part.}

\chapter{Test chapter one}
\AddToToC{0}{some keywords for the chapter.}

\section{Test section}
\AddToToC{1}{some keywords for the section.}

\subsection{Test subsection}
\AddToToC{2}{some keywords for the subsection.}

\end{document}


The lengths used are those of the book document class; other document classes will require adjusting the lengths. The command doesn't satisfy the last requirement, but all it's required is a simple change to the first argument.

• @Medina: I dont want to bother you, but it is still not the desired result. I hoped that there is a command with an additional feature. Example: \begin{document} \section{Test Part} bla bla \command{keyword1} few pages of bla bla \command{keyword2} .... \end{document} The desired layout in the toc (as in my tried-hard-to-specify-but-failed-version on the top): 1.1 Test Part keyword 1 - keyword 2 - ... I hope you forgive my constant complaints. – Philip Jun 4 '11 at 22:08