How to programmatically create sections in LaTeX

In the simplest form of the problem I am facing, I need to define a command that accepts two parameters, uses the first to put some contents in the document exactly where the command is invoked, and save the second parameter to be used later in the document.

To be specific, I am defining a command to help me fill an activities report. The command will accept the description of some activity (first parameter) and the name of a file that proves that the activity was performed (a scanned page or a PDF) (the second parameter). The command must format the first parameter in a nice table and add a \ref to the PDF, which will be automatically included in a section at the end of the document, with the corresponding \label.

Right now I use the label as the second parameter and include the file by hand, as shown in the example below.

\documentclass[]{article}

\newcounter{ctannex}

\DeclareRobustCommand{\annex}[1]{%
\refstepcounter{ctannex}%
\label{#1} %
Annex \arabic{ctannex} %
}

\newcounter{ctactivity}

\DeclareRobustCommand{\activity}[3]{%
\refstepcounter{ctactivity}%
\label{#1} %
Activity \arabic{ctactivity}\\ %
Description: #3\\ %
Receipt: \ref{#2}
}

\usepackage{graphicx}

\begin{document}

\section{Activities}
\begin{itemize}
\item \activity{actLabel}{annexLabel}{nothing, really}
\end{itemize}

\section{Annexes}
\begin{itemize}
\item \annex{annexLabel}
\includegraphics[width=\textwidth]{falta}
\end{itemize}

\end{document}


Ideally I would have the command called just as

\activity{actLabel}{file.png}{nothing, really}

-

The following code provide the functionality you're after.

\documentclass{article}
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{hyperref}% http://ctan.org/pkg/hyperref

\def\PrintAnnex{}
\newcounter{activityctr}% \renewcommand{\theactivityctr}{\arabic{activityctr}}%
\newcounter{annexctr}% \renewcommand{\theannexctr}{\arabic{annexctr}}%
\makeatletter
\newcommand{\newactivity}[3]{%
\item\refstepcounter{activityctr}% Increment and mark activity counter
\label{#1}% Label activity
Activity:~\theactivityctr\\% Print activity #
Description:~#3\\% Print description
Receipt:~\ref{annex-#1}% Reference receipt
\item\refstepcounter{annexctr}% Increment and mark annex counter
\label{annex-#1}% Label annex
Annex:~\theannexctr\par% Print annex #
\includegraphics[width=\linewidth,height=2\baselineskip]{#2}% Insert image
}%
}
\makeatother

\begin{document}

\section{Activities}
\begin{itemize}
\newactivity{activity1}{tiger}{nothing, really}
\newactivity{activity2}{tiger}{Rooooaaarrr!}
\newactivity{activity3}{tiger}{Rooooaaarrr!}
\newactivity{activity4}{tiger}{Mooaaarrr Rooooaaarrr!}
\end{itemize}

\section{Annexes}
\begin{itemize}
\PrintAnnex
\end{itemize}
\end{document}


I assume that you might be interested in referencing the activities in other parts of your document, hence the use of two counters (one for the activities activityctr and one for the annex annexctr).

With each

\newactivity{<label>}{<file>}{<description>}


an \item is created with \label{<label>} and description <description>. The <file> is added as an \includegraphics and appended (using \g@addto@macro) to the macro \PrintAnnex also in the form of an \item. So, you compile your activities using \newactivity, and issue \PrintAnnex within a list to print the list of annexes.

I've added the hyperref package in order to show that hyper-referencing works/is possible, although you might be interested in something different. Also, the demo package option to graphicx is merely for portability reasons, allowing anyone to compile the MWE.

-
This is exactly what I was looking for. Thanks Werner. –  lasaro Mar 2 '12 at 21:58