0

I am using enumerated lists and sometimes I want to replace the label displayed without affecting the underlying counting and referencing mechanism. I know how to use \addtocounter on a case by case basis to recover the numbering for subsequent items, but this is not enough for references. Also, using \addtocounter on a case by case basis adds a burden and I would to specify the behaviour one and for all. I am using enumitem to define my own enumerate-inspired list.

Is there any way to change the default counting behaviour without tinkering on a case by case using \addtocounter?

\documentclass{article}

\begin{document}
\begin{enumerate}
\item A
\item[x] B
\item C (The label should be 3, but displays 2)
\end{enumerate}

\begin{enumerate}
\item A
\addtocounter{enumi}{1}
\item[x] \label{two} B \ref{two} (The ref should be 2 but displays 1)
\item \label{three} C \ref{three} (The label and the ref are 3 as desired)
\end{enumerate}
\end{document}
1
  • You should use \refstepcounter – DG' Jun 1 '20 at 10:58
2

Try using \refstepcounter instead of \addtocounter and create a new command, e.g. \myitem, to automate the step:

\documentclass{article}

\newcommand\myitem[1][]{\item[#1]\refstepcounter{enumi}}

\begin{document}
\begin{enumerate}
    \item    A
    \item[x] B
    \item    C (The label should be 3, but displays 2)
\end{enumerate}

\begin{enumerate}
    \item      A 
    \myitem[x] \label{two} B \ref{two} (The ref should be 2 but displays 1)
    \item      \label{three} C \ref{three} (The label and the ref are 3 as desired)
\end{enumerate}
\end{document}

enter image description here

7
  • Ah yes, nice. Is there a way to set up my list environment such that refstepcounter is called after each item regardless of whether it receives an argument in []? So that I can set it up once in the preamble and don't need to remember to call \refstepcounter. – Abdallah Jun 1 '20 at 12:04
  • You can redefine \item. Maybe look at the documentation of enumitem, search this site or ask a new question. – DG' Jun 1 '20 at 12:59
  • Yes, I will search the site again. But I don't yet see what sort of different question I should ask as the current one specifically asks for a way to address the counting on global level rather than case by case. – Abdallah Jun 1 '20 at 13:46
  • 2
    I added a new command \myitem that does the job. You shouldn't redefine \itemglobally, since it could cause problems in many unexpected places. – DG' Jun 2 '20 at 8:24
  • Yes, that helps a bit, bit there is still the local difference between calling \item and \myitem. Since \item[x] will not cause an error, one can easily forget to call \myitem and the referencing will be wrong. Maybe there's a way to adjust your definition of myitem to test if the optional argument is provided or not. If it is, then the stepcounter needs to be incremented, if the optional argument isn't provided then the stepcounter doesn't need to be incremented. – Abdallah Jun 2 '20 at 15:18

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.