7

Can I achieve something like this with automatic numbering?

Examples:
  1 of 93: Example one
  2 of 93: Example two
  ...
  92 of 93: Example ninety-two
  93 of 93: Example ninety-three

I can change the label of enumerate, but I can't reference the number of items.

2
  • Do you need any \label command in this enumerated list?
    – egreg
    Oct 25, 2013 at 20:55
  • @egreg: good question, I'm maybe thinking of the same solution as you are. :) Honestly I don't know yet. It would be better to have the \label intact (but I can live with the loss).
    – masu
    Oct 25, 2013 at 21:00

2 Answers 2

8

An “automatic” version (inspired by Werner's). The “of n” might be wrong after some LaTeX runs, because the \label-\ref mechanism is used. But you'll get the warning Label(s) may have changed. in this case.

A \label inside the environment will refer to the item number (without the “of n”).

\documentclass{article}
\usepackage{enumitem,calc}

\makeatletter
\newcounter{enumerateof} % to create unique labels
\newenvironment{enumerateof}
  {\stepcounter{enumerateof}
   \begin{enumerate}[
     label=\arabic* of \protect\ref{enof@\arabic{enumerateof}}:,
     ref=\arabic*,
     labelwidth=\widthof{\ref{enof@\arabic{enumerateof}} of \ref{enof@\arabic{enumerateof}}:},
     leftmargin=!]}
  {\addtocounter{\@enumctr}{-1}%
   \refstepcounter{\@enumctr}%
   \label{enof@\arabic{enumerateof}}%
   \end{enumerate}}
\makeatother

\begin{document}

\noindent X\dotfill X

\begin{enumerateof}
\item\label{a} a
\item b
\item c
\item d
\item e
\item f
\item g
\item h
\item i
\item j
\item k
\end{enumerateof}

Here's \ref{a}

\end{document}

enter image description here

7

The following is similar in solution to How to make enumerate items align at left margin? and requires a label to capture the last item in the list. It could be automated if needed, I'm sure:

enter image description here

\documentclass{article}
\usepackage{enumitem,calc}% http://ctan.org/pkg/{enumitem,calc}
\begin{document}
\noindent Here is some text.
\begin{enumerate}[
    labelindent=0pt,
    labelwidth=\widthof{\ref{last-item} of \ref{last-item}:},
    label={\arabic* of \ref{last-item}:},
    ref={\arabic*},
    itemindent=1em,
    leftmargin=!]
  \item An item
  \item An item
  \item An item \addtocounter{enumi}{9997}
  \item An item \label{last-item}
\end{enumerate}
Here is some more text.
\end{document}

If you wish to have the alignment different, you could add the align=left key to your enumeration. For left-aligned enumeration but horizontally-aligned :, you could use label={\arabic* of \ref{last-item}\hfill:} since the label is set inside of box:

enter image description here

\documentclass{article}
\usepackage{enumitem,calc}% http://ctan.org/pkg/{enumitem,calc}
\begin{document}
\noindent Here is some text.
\begin{enumerate}[
    labelindent=0pt,
    labelwidth=\widthof{\ref{last-item} of \ref{last-item}:},
    label={\arabic* of \ref{last-item}\hfill:},
    ref={\arabic*},
    itemindent=1em,
    leftmargin=!,align=left]
  \item An item
  \item An item
  \item An item \addtocounter{enumi}{9997}
  \item An item \label{last-item}
\end{enumerate}
Here is some more text.
\end{document}
2
  • I know, a comment comes after 7 years! But, hey, people are still reading this answer and finding it useful. Could you please explain why the \addtocounter{enumi}{9997} before the last item?
    – Andyc
    Oct 1, 2020 at 16:35
  • 1
    @Andyc: Just to have a wide numbering to show the alignment.
    – Werner
    Oct 3, 2020 at 4:42

You must log in to answer this question.

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