Always resumed list does not always resume if invoked from within an environment

The resume option is always local, i.e. only within the current group. Using a list within some environment and trying to resume the list in another environment later on will fail in the sense that the information is lost.

The remedy is resume*, which must be specified locally, however and can't be given as option to the \setlist macro.

\documentclass{article}
\usepackage{enumitem}
\usepackage{xcolor}



\newlist{MyList}{enumerate}{2}
\setlist[MyList,1]{label={\arabic*.},resume}% ALWAYS resumed
\setlist[MyList,2]{label={\alph*)}}%           NOT resumed

\newenvironment{MyColoredList}[2][]{%
  \color{#2}%
  \MyList[#1]
  }{%
  \endMyList%
}%


\begin{document}
    \begin{MyColoredList}{red}
        \item First Item
    \end{MyColoredList}
    Some text
    \begin{MyColoredList}[resume*]{orange}% <--- This should be number 2
    \item First Item
    \end{MyColoredList}
    Some text
    \begin{MyList}% <--- This should be number 3
        \item Second Item
    \end{MyList}
    Some text
    \begin{MyList}
        \item Third Item
    \end{MyList}
    Some text
    \begin{MyColoredList}{blue}
        \item Fourth Item (this works!!)
          \begin{MyList}[resume*]
            \item Inner Item
            \end{MyList}
    \end{MyColoredList}
    \begin{MyColoredList}{violet}
        \item Fifth Item (this works!!)
          \begin{MyList}[resume*]
            \item Inner Item
            \end{MyList}
    \end{MyColoredList}

\end{document}

A better way can be achieved with the series= key:

\documentclass{article}
\usepackage{enumitem}
\usepackage{xcolor}


\newlist{MyList}{enumerate}{2}
\setlist[MyList,1]{label={\arabic*.},resume=foo}% ALWAYS resumed
\setlist[MyList,2]{label={\alph*)}}%           NOT resumed

\newenvironment{MyColoredList}[2][]{%
  \color{#2}%
  \begin{MyList}[#1]
  }{%
  \end{MyList}%
}%

\begin{document}
\begin{MyColoredList}[series=foo]{red}
\item First Item
    \end{MyColoredList}
    Some text
    \begin{MyColoredList}{orange}% <--- This should be number 2
    \item First Item
    \end{MyColoredList}
    Some text
    \begin{MyList}%  <--- This should be number 3
        \item Second Item
    \end{MyList}
    Some text
    \begin{MyList}
        \item Third Item
    \end{MyList}
    Some text
    \begin{MyColoredList}{blue}
        \item Fourth Item (this works!!)
          \begin{MyList}[resume*]
            \item Inner Item
            \end{MyList}
    \end{MyColoredList}
    \begin{MyColoredList}{violet}
        \item Fifth Item (this works!!)
          \begin{MyList}[resume*]
            \item Inner Item
            \end{MyList}
    \end{MyColoredList}

\end{document}

enter image description here


You can do it the hard way:

\documentclass{article}
\usepackage{enumitem}
\usepackage{xcolor}

\newlist{MyList}{enumerate}{2}
\setlist[MyList,1]{
  label=\arabic*.,
  before=\setcounter{MyListi}{\value{MyList}},
  after=\setcounter{MyList}{\value{MyListi}},
}% ALWAYS resumed
\setlist[MyList,2]{label=\alph*)}%           NOT resumed

\newcounter{MyList}
\newenvironment{MyColoredList}[2][]{%
    \color{#2}\begin{MyList}[#1]
}{%
    \end{MyList}%
}%


\begin{document}
    \begin{MyColoredList}{red}
        \item First Item
    \end{MyColoredList}
    Some text
    \begin{MyColoredList}{orange}% <--- This should be number 2
        \item First Item
    \end{MyColoredList}
    Some text
    \begin{MyList}% <--- This should be number 3
        \item Second Item
    \end{MyList}
    Some text
    \begin{MyList}
        \item Third Item
    \end{MyList}
    Some text
    \begin{MyColoredList}{blue}
        \item Fourth Item (this works!!)
    \end{MyColoredList}
\end{document}

enter image description here


You can try this

\documentclass{article}
\usepackage{enumitem}
\usepackage{xcolor}

\makeatletter
\newlist{MyList}{enumerate}{2}
\setlist[MyList,1]{label={\arabic*.},
        after={\xdef\enit@resume@MyList{\noexpand\c@MyListi=\the\c@MyListi}},resume}% ALWAYS resumed
\setlist[MyList,2]{label={\alph*)}}%           NOT resumed

\makeatother
\newenvironment{MyColoredList}[2][]{%
    \color{#2}%
    \begin{MyList}[#1]
}{%
    \end{MyList}%
}%

\begin{document}

    \begin{MyColoredList}{red}
        \item First Item
    \end{MyColoredList}
    Some text
    \begin{MyColoredList}{orange}% <--- This should be number 2
        \item First Item
    \end{MyColoredList}
    Some text
    \begin{MyList}% <--- This should be number 3
        \item Second Item
    \end{MyList}
    Some text
    \begin{MyList}
        \item Third Item
    \end{MyList}
    Some text
    \begin{MyColoredList}{blue}
        \item Fourth Item (this works!!)
    \end{MyColoredList}
\end{document}

enter image description here

Tags:

Lists

Enumitem