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.



   \label{#1} %
    Annex \arabic{ctannex} %


   \label{#1} %
   Activity \arabic{ctactivity}\\ %
   Description: #3\\ %
   Receipt: \ref{#2}



 \item \activity{actLabel}{annexLabel}{nothing, really}

\item \annex{annexLabel}


Ideally I would have the command called just as

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

The following code provide the functionality you're after.

enter image description here

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

\newcounter{activityctr}% \renewcommand{\theactivityctr}{\arabic{activityctr}}%
\newcounter{annexctr}% \renewcommand{\theannexctr}{\arabic{annexctr}}%
  \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


  \newactivity{activity1}{tiger}{nothing, really}
  \newactivity{activity4}{tiger}{Mooaaarrr Rooooaaarrr!}


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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.