參考:

參考:

MyList是一個始終恢復的enumitem清單:

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

但如果第一的使用MyList來自另一個環境(橙色文字)。在 MWE 中,此環境會為清單新增顏色,然後呼叫\begin{MyList}

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

但是,使用相同的環境直接使用MyList似乎很好(藍色文字):

在此輸入影像描述

筆記:

  • 第二級沒有恢復,沒有問題。因此我刪除了測試該部分的程式碼。
  • 我的問題不是如何為列表著色,而是如何resume在從環境中調用它時對其進行編號。

參考:

代碼:

\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}%
    \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}

答案1

你可以用困難的方式做到這一點:

\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}

在此輸入影像描述

答案2

resume選項始終是本地的,即僅在目前群組內。在某個環境中使用清單並稍後嘗試在另一個環境中恢復該清單將會失敗,因為資訊會遺失。

補救措施是resume*,它必須在本地指定,但是不能作為\setlist巨集的選項給出。

\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}

更好的方法可以用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}

在此輸入影像描述

答案3

你可以試試這個

\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}

在此輸入影像描述

相關內容