4

I have created a counter to count enclosures:

\newcounter{enclosure}{0}

At the bottom of each enclosure I put:

\addtocounter{enclosure}{1}\vfill\hfill\Enclosure \arabic{enclosure}\clearpage

And that works perfectly.

However, at the page before the enclosures I need to put this at the bottom (if there are three enclosures):

ENCLOSURES (3)

I would like to make the value in the parenthesis reflect the last value of the enclosure counter. I've tried \ref and \label but that grabs the section counter, not these counters.

Here is a complete example:

\documentclass[10pt]{article}   
\begin{document}
\newcounter{enclosure}
\noindent
Total Enclosures: \\
\stepcounter{enclosure} Enclosure \arabic{enclosure}\\
\stepcounter{enclosure} Enclosure \arabic{enclosure}\\
\stepcounter{enclosure} Enclosure \arabic{enclosure}\\
\stepcounter{enclosure} Enclosure \arabic{enclosure}\\
Total Enclosures: \arabic{enclosure}
\end{document}

I want the Total Enclosures to be the same at the top as the bottom.

Thanks.

3
  • Can you make a minimal working example (MWE)?
    – egreg
    Mar 2, 2013 at 15:29
  • You'll need to write the information to the .aux file, and then read it. Once implemented, it'll need two runs to get it right. You can see examples of this in a few different packages, for example the lastpage package
    – cmhughes
    Mar 2, 2013 at 15:53
  • Right, but is there an easy way to do it? I'd rather just use \ref and \label...
    – vy32
    Mar 2, 2013 at 15:55

2 Answers 2

5
\addtocounter{enclosure}{1}

or

\stepcounter{enclosure}

which is the same thing, just increment the counter, not make it available to \ref.

Instead use

\refstepcounter{enclosure}

then the counter is incremented in the same way but its value becomes the current reference number so \label will pick it up and you can reference it with \ref.

0
2

You can add a \label command at end document:

\documentclass[10pt]{article}

\newcounter{enclosure}
\newcommand{\totalenclosures}{Total Enclosures:~\ref{tot@lencl@sures}}
\newcommand{\enclosure}{%
  \stepcounter{enclosure}%
  Enclosure~\theenclosure
}

\AtEndDocument{%
  \addtocounter{enclosure}{-1}%
  \refstepcounter{enclosure}\label{tot@lencl@sures}%
}

\begin{document}
\noindent\totalenclosures\\
\enclosure\\
\enclosure\\
\enclosure\\
\enclosure\\
\totalenclosures
\end{document}

The strange \label{tot@lencl@sures} is meant to be a string that you don't use in the document. The counter is stepped down and then "refstepped" up to have the correct value available for \label.

It's a bit more complicated if you want that \totalenclosures shows "No Enclosure" if there's none in the document.

\documentclass[10pt]{article}
\usepackage{refcount} % for \getrefnumber
\newcounter{enclosure}
\newcommand{\totalenclosures}{%
  \ifnum\getrefnumber{tot@lencl@sures}=0
    No Enclosure%
  \else
    Total Enclosures:~\ref{tot@lencl@sures}%
  \fi
}
\AtEndDocument{%
  \addtocounter{enclosure}{-1}%
  \refstepcounter{enclosure}\label{tot@lencl@sures}%
}
\newcommand{\enclosure}{%
  \stepcounter{enclosure}%
  Enclosure~\theenclosure
}

\begin{document}
\noindent\totalenclosures\\
\enclosure\\
\enclosure\\
\enclosure\\
\enclosure\\
\totalenclosures
\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .