# How to build a table of sections and certain commands used in a section

I would like to create an list with some tuples (sectionname, property) so I can add them automatically to a table.

Say I have:

\magictablecommand
\section{Test}
\property{Blup}
\section{Plop}
\property{Slup}


Then magictablecommand should expand as:

\begin{table}...
\begin{tabular}...
Test & Blup \\
Plop & Slup \\


I need some command, which on the first pass gather section, property pairs. And on the second pass expands magictablecommand (in the first pass it doesn't expand, I don't care really as long as it doesn't emit errors.).

The biggest trouble I have is in determine how to gather the section, property pairs. The rest I can probably figure out.

I would be quite happy with some kind of pointer.

The Solution

Here is a solution which provides the functionality you want. With a little tweaking, you can fine tune it to your ultimate needs.

\documentclass{article}

\def\magictablecommandopen{%
% Keep the new sections locals and redefine section
\begingroup\let\actualsection\section
\renewcommand{\section}[1]{\actualsection{##1}\def\sectionname{##1}}
% Create output table
\newwrite\tupletable
\immediate\openout\tupletable=\jobname.tpl
\immediate\write\tupletable{\unexpanded{\begin{tabular}{cc}}}
}

\def\magictablecommandclose{%
% Simply close the table, output stream and group
\immediate\write\tupletable{\unexpanded{\end{tabular}}}
\immediate\closeout\tupletable
\endgroup}

\def\property#1{\immediate\write\tupletable{\sectionname\unexpanded{&#1\\}}}

\begin{document}

\section{This Section Will not be in Table}

% We start table creation here
\magictablecommandopen
\section{Test}
\property{Blup}
\section{Plop}
\property{Slup}

\magictablecommandclose

% These sections will not be included in the table

\section{Not in Table}

\IfFileExists{\jobname.tpl}{
\begin{table}[!tbp]
\centering
\input \jobname.tpl
\caption{My Tuples.}
\label{tab:tpl}
\end{table}}

\end{document}


How It Works?

• Actually we have defined two magic commands here. One \magictablecommandopen starts your table. \magictablecommandclose finishes your table. So, texts or sections appearing before \magictablecommandopen or after \magictablecommandclose will not be considered for the tupple table. (With some more work, we can even think of turning on and off the table gather environment a number of times.)
• \magictablecommandopen starts by redefining the \section command so that section name can be saves somewhere (\sectionname). This is done inside a group to keep the changes local. Then it opens an output stream and writes the tabular start commands.
• \magictablecommandclose writes the tabular close commands, closes the output stream and the group.
• \property is rather simple. It simply writes a row in the table. The trick here is to use the section name previously saved when the section started.
• Eventually, you get a \jobname.tpl file which you can use anywhere you want.
• @EdgarKlerks Glad to be of assistance. Nov 19, 2013 at 17:03

The accepted answer is excellent, but as this deals with sections, it is also possible to do it using etoc. This is certainly a bit idiosyncratic, the advantage, if there is one, is that no group is created which encompasses the tagged sections and also that no extra auxiliary file is needed.

Another approach with etoc would add the property information in the section names in a way invisible in the standard TOCs but made visible on command. But here, we have defined a new dummy sectioning called property and played with it with the tools provided by the package. There are some subtleties though, explained in the code comments.

\documentclass{article}
\usepackage{etoc}

\etocsetlevel{property}{6}
\newcommand\property [1]{\etoctoccontentsline{property}{#1}}

\begin{document}\thispagestyle{empty}

\tableofcontents

\etocdepthtag.toc {notintable}

\section{This Section will not be in Magic Table}

first section

\etocdepthtag.toc {magictable}

\section{Test}
\property{Blup}

second section

\section{Plop}
\property{Slup}

third section

% These sections will not be included in the table

\etocdepthtag.toc {notintable}

\section{Not in Magic Table}

fourth section

\begingroup % this group is only if one needs a standard TOC after the magic
% table

\etocsettocstyle{\begin{table}[!tbp]
\centering
% we can not start the tabular here, else
% some macros defined later by etoc will
% get lost at the end
% of the first cell, which closes a group
}
{\caption{Magic Table of my Tuples.}
\label{tab:tpl}
\end{table}}

\etocsetstyle{section}{\begin{tabular}{cc}}{\etocname}{&}{\end{tabular}}

\etocsetlevel{property}{2}% must use numeric level here and we can not set it at
% the same level 1 as section, else \etocsetstyle
% below would also modify the style for sections

% we can not do something like &\etocname, because the & closes a group
% and \etocname then loses its meaning; this is why the & is in the section
% style.
\etocsetstyle{property}{}{\etocname}{}{\\}

% in case there are other sectioning levels we don't want to see them in this
% special TOC
\etocsetlevel{subsection}{6}
\etocsetlevel{subsubsection}{6}
% Material not tagged with magictable has no place here
\etocsettagdepth {notintable}{none}
\etocsettagdepth {magictable}{property} % (or the numeric level 2)

\tableofcontents

\endgroup

\end{document}