Listing occurrences of a name through the document in one table

I have a document (article) presenting a list and description of several tasks. Each task is written in either a section or subsection, beside the title and description it includes the name of the person in charge, just in plain text in the beginning of the (sub)section. The name can be present in more than one (sub)section, as one person may have multiple tasks.

The end document looks something like this:

1. Basics
1.1 Task A
Name: John Doe
The task is about ...
It needs to be completed by July 2012.

1.2. Task B
Name: James F.

2. Task C
Name: John Doe

and so on.

In the beginning of the document I would like to generate a table (or index) of the involved people and the tasks for which they are responsible (i.e. where all the occurrences of the names are referenced in one place), in order to have an organised view in which I could see all assigned tasks for a person, and it should be in the following format:

Name     | Task         | Section, page

John Doe | Task A       | 1.1, p. 2
         | Task C       | 2, p. 5
James F. | Task B       | 1.2, p.3

Is there an easy solution for that?

The solution should facilitate that each person could be easily switched to another task, so that the index is automatically regenerated.

I don’t think I can really use labels here, as one name can appear multiple times.

Indexing, like this:

\subsection{Task A}
Name: John Doe \index{John Doe}
The task is about ...
It needs to be done by ...

Gives me a basic result:


John Doe, 2, 5

It might be a good place to start, but as far as I know, changing the layout of the index in a table (which also includes section names) is a pain.

Solutions Collecting From Web of "Listing occurrences of a name through the document in one table"

Here is an approach with LuaLaTeX without any LaTeX indexing. The main principle is to store all needed informations in a Lua table during the LaTeX run and print the (sorted) informations at the end of the document. To get the section name I used the \sectioningtitle macro of lockstep (Thank you!).


--global table
names = {}

function storeItems(name, section, task, page)
    --create a new name item
    item = {}
    item.Name = name
    item.Section = section
    item.Task = task
    item.Page = page

    --add the new name item to the table
    table.insert(names, item)

function printTable()
    --sort the table (Name order)
    table.sort (names,
           function (item1, item2)
             return item1.Name < item2.Name

    --print the latex table
    tex.print(string.format(" Name & Section & Task & Page\\\\\\hline"))

    local nameTemp = ""

    for i,p in ipairs(names) do
        --print the name only if it is a new one
        if p.Name == nameTemp then
            tex.print(string.format("     & {%s} & {%s} & {%s}\\\\",p.Section, p.Task, p.Page))
            tex.print(string.format("{%s} & {%s} & {%s} & {%s}\\\\",p.Name, p.Section, p.Task, p.Page))

        nameTemp = p.Name


% read the external lua file to declare the defined function,
% but without execute the Lua function

% latex command to execute the lua function
\def\store#1#2#3#4{\directlua{storeItems("#1", "#2", "#3", "#4")}}

% command for the references

%this is used to get the current sectionname


\section{ Basics}

\subsection{Task A}
Name: \myRef{John Doe}\\
The task is about ...\\
It needs to be completed by July 2012.

\subsection{Task B}
Name:  \myRef{James F.}

\subsection{Task C}
Name: \myRef{John Doe}
\end{document} `enter code here`

enter image description here

Here’s a partial solution — I didn’t manage to add sectioning numbers plus a “table-like” header to the index, but I managed to create an index of persons and the tasks they’re responsible for.

  • Define a new \sectioningtitle macro which is empty at the start.

  • Patch the internal \@sect macro so that every time you start a section/subsection, its argument also becomes the new meaning of \sectioningtitle.

  • Define a new \taskindex macro which

    • typesets “Name: ” plus its argument (this is just for convenience);

    • adds an index entry with its argument as main item and the current meaning of \sectioningtitle as subitem. (Note: In the following MWE, I assume that MakeIndex is run using the standard ! as item/subitem separator.)



\usepackage[unbalanced]{idxlayout}% no page break before `article` index




\newcommand*{\taskindex}[1]{Name: #1\index{#1!\sectioningtitle}}



\subsection{Task A}

\taskindex{Doe, John}

\subsection{Task B}

\taskindex{F., James}

\section{Task C}

\taskindex{Doe, John}



enter image description here