# 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
Name: John Doe
It needs to be completed by July 2012.

Name: James F.
...

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}
It needs to be done by ...


Gives me a basic result:

Index

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.

-
en.wikibooks.org/wiki/LaTeX/Indexing –  Seamus May 9 '12 at 10:20
What are you asking? Is this a question about how to make an index? In which case the above link should give you your answers. Or is this a more specific question about how to format the index in the above way? Could you make this clearer? –  Seamus May 9 '12 at 10:23
Thanks. This is a specific question about how to format the index in the presented way. However, I never worked with indexing, so I'll take a look in the link you had provided me, but I would appreciate if you could could briefly give me the answer for the complete solution. –  Filip May 9 '12 at 10:42
Okay, I read the resource, and I indexed all the names in the document and compiled an index. However, now I have the usual index: name, page number. How do I format it to the above table? And also print the (sub)section in which the index appears? –  Filip May 9 '12 at 12:53
Maybe you should edit your question to make it clearer what you are asking, and what you have done so far. Perhaps include a small example that demonstrates what you want. –  Seamus May 9 '12 at 14:07

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!).

\documentclass{article}
\usepackage{etoolbox}
\usepackage{filecontents}

\begin{filecontents*}{luaFunctions.lua}

--global table
names = {}

--create a new name item
item = {}
item.Name = name
item.Section = section
item.Page = page

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

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

--print the latex table
tex.print(string.format("\\begin{tabular}{c|c|c|c}"))
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))
else
tex.print(string.format("{%s} & {%s} & {%s} & {%s}\\\\",p.Name, p.Section, p.Task, p.Page))
end

nameTemp = p.Name
end

tex.print(string.format("\\end{tabular}"))
end

\end{filecontents*}

% read the external lua file to declare the defined function,
% but without execute the Lua function
\directlua{dofile("luaFunctions.lua")}

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

% command for the references
\def\myRef#1{%
#1\store{#1}{\thesubsection}{\sectioningtitle}{\thepage}%
}

%this is used to get the current sectionname
\newcommand*{\sectioningtitle}{}
\makeatletter
\apptocmd{\@sect}{\renewcommand*{\sectioningtitle}{#8}}{}{}
\makeatother

\begin{document}

\section{ Basics}

Name: \myRef{John Doe}\\
It needs to be completed by July 2012.

Name:  \myRef{James F.}

Name: \myRef{John Doe}
%
\\[1cm]
\printTable
\end{document} enter code here


-
Thanks! Perfect. –  Filip May 11 '12 at 6:35

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.)

\documentclass{article}

\usepackage{makeidx}
\makeindex

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

\usepackage{etoolbox}

\newcommand*{\sectioningtitle}{}

\makeatletter
\apptocmd{\@sect}{\renewcommand*{\sectioningtitle}{#8}}{}{}
\makeatother

\begin{document}

\section{Basics}