4

I'd like to have an enumerated list that ① supports resume and ② uses the notation n of N, e.g. (in ASCII)

1/4. Bla.
2/4. Bla bla.

Some additional bla bla,

3/4. from my balcone.
4/4. Bla bla.

I imagine that I could use a counter and possibly a two pass compilation (but I don't know how exactly do that), but I wonder if that some package already provides the requested features.

3
  • The totcount package should be able to do that. #2, at least.
    – Ingmar
    Jul 3, 2022 at 10:46
  • @user574859 Questions in a written test?
    – gboffi
    Jul 3, 2022 at 13:59
  • Note that the label has to fit into \labelwidth-\labelsep, so 10/11 will probably cause problems. Jul 3, 2022 at 15:35

1 Answer 1

11

With enumitem you can save the final value of the counter.

\documentclass{article}
\usepackage{enumitem}

\newlist{gbenumerate}{enumerate}{1}
\setlist[gbenumerate]{
  wide,
  label=\arabic*/\gbenumall.,
}

\providecommand\gbenumall{0}% initial value

\makeatletter
\AtEndDocument{%
  \write\@auxout{\gdef\string\gbenumall{\the\value{gbenumeratei}}}%
}
\makeatother

\begin{document}

\begin{gbenumerate}
\item this is the first item
\item this is the second item
\end{gbenumerate}

Some text in between

\begin{gbenumerate}[resume]
\item this is the third item
\item this is the fourth item
\end{gbenumerate}

\end{document}

enter image description here

A bit more complex if you want to use gbenumerate several times with reset. In this case you have to say when to reset and to save the last value. I suggest enclosing the parts in a suitable environment.

\documentclass{article}
\usepackage{enumitem}

\newlist{gbenumerate}{enumerate}{1}
\setlist[gbenumerate]{
  wide,
  label=\arabic*/\unexpanded{\gbenum{\thesubgbenumerate}}.,
  resume
}
\newcounter{subgbenumerate}

\makeatletter
\newenvironment{subgbenumerate}
 {%
  \stepcounter{subgbenumerate}%
 }
 {\immediate\write\@auxout{\string\storegbenum{\thesubgbenumerate}{\the\value{gbenumeratei}}}}
\newcommand{\storegbenum}[2]{%
  \expandafter\xdef\csname gbenum@#1\endcsname{#2}%
}
\newcommand{\gbenum}[1]{\ifcsname gbenum@#1\endcsname\csname gbenum@#1\endcsname\else 0\fi}
\makeatother

\AtEndDocument{%
}
\makeatother

\begin{document}

\begin{subgbenumerate}
\begin{gbenumerate}
\item this is the first item
\item this is the second item
\end{gbenumerate}

Some text in between

\begin{gbenumerate}
\item this is the third item
\item this is the fourth item
\end{gbenumerate}
\end{subgbenumerate}

Some text in between.

\begin{subgbenumerate}
\begin{gbenumerate}
\item this is the first item
\item this is the second item
\end{gbenumerate}

Some text in between

\begin{gbenumerate}
\item this is the third item
\item this is the fourth item
\item this is the fifth item
\end{gbenumerate}
\end{subgbenumerate}

\end{document}

enter image description here

6
  • Should the OP realize that what they really want is, using the gbenumerate environment many times in the same document, resetting the counter… should they ask a new question, or they can count on your benevolence?
    – gboffi
    Jul 3, 2022 at 14:14
  • @gboffi I added the code
    – egreg
    Jul 3, 2022 at 14:51
  • Shouldn't enumitem already be saving the last value for use with [resume]? Jul 3, 2022 at 15:41
  • @JohnKormylo But not in the aux file, just in memory.
    – egreg
    Jul 3, 2022 at 15:45
  • @gboffi Depending on your use case, you might be able to embed the subgbenumerate code in another environment you use.
    – egreg
    Jul 3, 2022 at 18:51

You must log in to answer this question.

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