1

I have a numbers of sections and subsections that have continuous enumeration (i.e. if the first section stops at 4, the second section starts counting at 5). As given by the following:

\section{Section 1 (Number of Items in section: 3)}
    \begin{enumerate}[resume]
        \item Item 1
        \item Item 2
        \item Item 3
    \end{enumerate}
\section{Section 2 (Number of Items in section: 2)}
    \begin{enumerate}[resume]
        \item Item 4
        \item Item 5
    \end{enumerate}

What I want to do is automate the part that says "Number of Items in section: x". Right now, I'm manually counting up the items, but is there a way to get the number of items in the following section and update it automatically?

Thanks!

3

This is quite an easy task with xcntperchap package (I am the author of this package, ok -- shameless promotion ;-))

The main purpose of this package is to count how many Y (here enumi) per track level counter X (here section) occur.

The basic usage is \RegisterCounters{section}{enumi,...}, the 1st argument is designed to hold the track level, i.e. the 'structure' level and the second argument holds a comma separated list of counter names, that should be tracked, here enumi (since this is the enumerate first level counter)

Multiple track levels are possible, just use \RegisterCounters for each of them, even the tracked counters can be used in multiple track levels, the 'book-keeping' is done behind and different track levels do not interfere!

The whole information is stored in a \jobname.cpc file and the information can be retrieved during the 2nd compilation run with

\ObtainTrackedValue[counter value]{section}{enumi}

The counter value specifies which section number should be tracked. Omitting it, the current value of the track level counter (here section) is used, which is quite convenient if one does not want to bother with the current section value.

The package is rather new and the successor of cntperchap, it's available on CTAN xcntperchap

The resume feature of enumitem does have a negative side - effect, since xcntperchap uses xassoccnt in the background.

\documentclass{article}

\usepackage{xcntperchap}
\usepackage{enumitem}

\RegisterCounters{section}{enumi}

\begin{document}


\section{Section 1 -- with \ObtainTrackedValue{section}{enumi} items}
    \begin{enumerate}[resume]
        \item Item 1
        \item Item 2
        \item Item 3
    \end{enumerate}

\section{Section 2 -- with \ObtainTrackedValue{section}{enumi} items}
    \begin{enumerate}[resume]
        \item Item 4
        \item Item 5
    \end{enumerate}
\end{document}

enter image description here

  • Wow, thanks for pointing me to this package (and for writing it too!). This is exactly what I was looking for, and I wasn't expecting it to be so easy to implement. Thank you so much! – pomegranate Jun 6 '16 at 17:13
  • @pomegranate: Well, 'easy' in frontend, not so easy in the backend ;-) Happy TeXing! – user31729 Jun 6 '16 at 17:15
  • Haha, of course! :) I appreciate your hard work on the backend! Quick question -- is there a way I can include these counted values in the Table of Contents? Right now, when I use \tableofcontents with headers similar to the ones you show above, the entries appear as "Section 1 - with () items" etc. – pomegranate Jun 6 '16 at 17:40
  • @pomegranate: No, not in this version. \ObtainTrackedValue isn't expandable, i.e. it does not survive the write-to-ToC process. I'll think, I can provide a workaround. – user31729 Jun 6 '16 at 17:52
  • @pomegranate: I've got a solution, but it requires a new package version. I try to upload tomorrow. – user31729 Jun 7 '16 at 0:00

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.