0

I am trying to come up with a new enumerated list environment "stepslist" such that the items are marked with "Step k of n", where n is the total number of items in one stepslist environment, and k is the kth item in it. Thus e.g. the code

\begin{stepslist}

\item A

\item B

\item C

\end{stepslist}

ought to render as:

Step 1 of 3: A

Step 2 of 3: B

Step 3 of 3: C

and

\begin{stepslist}

\item A

\item B

\item C

\item D

\end{stepslist}

ought to render as:

Step 1 of 4: A

Step 2 of 4: B

Step 3 of 4: C

Step 4 of 4: D

I don't want to define the variable n myself; I want its value to be determined automatically by the macro. Conversations with GPT4 suggest a "two-pass approach" (which makes sense, since as I understand it LaTeX does not know at the beginning of a list environment how many items there are in it).

I am unaware if this can even be done, but if it can then I would appreciate a method that would work in Overleaf.

Ingmar
  • 6,690
  • 5
  • 26
  • 47
Alp Uzman
  • 284

1 Answers1

3

You can set a label which counts the items:

\documentclass{article}

\usepackage{enumitem} % \newlist{stepslist}{enumerate}{1} \setlist[stepslist,1]{label={Step \arabic* of \ref{steplist:\themylistcnt}:},ref=\arabic{stepslisti}}

\newcounter{mylistcnt} % for a unique label \AddToHook{env/stepslist/begin}{\stepcounter{mylistcnt}} \AddToHook{env/stepslist/end}{\label{steplist:\themylistcnt}}

\begin{document}

\begin{stepslist} \item A \item B \item C \item D \end{stepslist}

\begin{stepslist} \item A \item B \item C \end{stepslist}

\end{document}

enter image description here

Ulrike Fischer
  • 327,261
  • Thank you. May I ask a followup question? First, I am using hyperref also, so to not generate links I replaced your \ref with \ref*. Second, in my document the indentation of the stepslist items are way off the margin on the left. I can fix this by adding the option "left=0pt" inside \setlist, but then Overleaf is giving yellow errors, e.g. "Reference `steplist:0' on page 0 undefined on input line 66.". Is there a way to solve this? – Alp Uzman Aug 14 '23 at 20:26
  • no don't ask follow up question in comments, that pings only me. Ask a new question (and with a proper example). – Ulrike Fischer Aug 14 '23 at 20:27
  • Thanks, I had meant to only ping you. I don't think the followup question has independent interest; it's about a certain implementation of your code (an implementation I explicitly state in my question as what interests me). I can indeed provide a proper example if you are willing to humor me further. – Alp Uzman Aug 14 '23 at 21:05