Проблема с повторным продлением команды

Проблема с повторным продлением команды

Я хочу сделать список, пронумерованный цветными числами. Способ, который я нашел, заключается в обновлении команды \labelenumi, используемой средой enumerate для маркировки элементов. Однако я хочу иметь возможность вернуться к настройкам по умолчанию и добился всего, используя этот фрагмент кода с помощью пакета circledsteps.

\newcommand{\colorlabel}{
    \let\oldlabelenumi\labelenumi
    \renewcommand{\labelenumi}{
    \Circled[inner color=white,fill color=mycolor,outer color=mycolor]{\textsf{\arabic{enumi}}}
    }
}
\newcommand{\defaultlabel}{
    \renewcommand{\labelenumi}{\oldlabelenumi}
}

Проблема в том, что в первый раз, когда я использую \colorlabel и \defaultlabel, это работает. Во второй раз, когда я переключаюсь на цветные метки, это тоже работает, но возвращение к значениям по умолчанию — нет (если используется внутри группы enumerate, \defaultlabel ничего не делает, а если используется вне ее, код не компилируется). Я могу исправить это, вручную пометив все, что не окрашено, и никогда не переключаясь обратно на значения по умолчанию, но хотелось бы избежать этого решения.

Редактировать:

Вот компилируемая версия кода. Если раскомментировать последнюю часть, код не скомпилируется.

\documentclass[a4paper,11 pt]{article}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{circledsteps}
\newcommand{\colorlabel}{
    \let\oldlabelenumi\labelenumi
    \renewcommand{\labelenumi}{
    \Circled[inner color=white,fill color=mycolor,outer color=mycolor]{\textsf{\arabic{enumi}}}
    }
}
\newcommand{\defaultlabel}{
    \renewcommand{\labelenumi}{\oldlabelenumi}
}

\begin{document}

\definecolor{hardex}{RGB}{204,51,0}
\definecolor{mediumex}{RGB}{204,153,0}
\definecolor{easyex}{RGB}{0,153,51}
\colorlet{mycolor}{easyex}
\newcommand{\ritem}{\colorlet{mycolor}{hardex} \item}
\newcommand{\yitem}{\colorlet{mycolor}{mediumex} \item}
\newcommand{\gitem}{\colorlet{mycolor}{easyex} \item}

\colorlabel
\begin{enumerate}
    \gitem green
    \yitem yellow
    \ritem red
    \item red again
\end{enumerate}
\defaultlabel

\begin{enumerate}
    \item one
    \item two
\end{enumerate}

\colorlabel
\begin{enumerate}
    \gitem green
\end{enumerate}
\defaultlabel

%\begin{enumerate}
%    \item ?
%\end{enumerate}

\end{document}

решение1

Вам необходимо инициализировать \oldlabelenumi, в противном случае отсутствие инициала \colorlabelприведет к ошибке.

\documentclass[a4paper,11 pt]{article}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{circledsteps}

\let\oldlabelenumi\labelenumi
\newcommand{\colorlabel}{%
  \renewcommand{\labelenumi}{%
    \Circled[inner color=white,fill color=mycolor,outer color=mycolor]{\textsf{\arabic{enumi}}}
  }%
}
\newcommand{\defaultlabel}{%
  \let\labelenumi\oldlabelenumi
}

\definecolor{hardex}{RGB}{204,51,0}
\definecolor{mediumex}{RGB}{204,153,0}
\definecolor{easyex}{RGB}{0,153,51}
\colorlet{mycolor}{easyex}

\newcommand{\ritem}{\colorlet{mycolor}{hardex} \item}
\newcommand{\yitem}{\colorlet{mycolor}{mediumex} \item}
\newcommand{\gitem}{\colorlet{mycolor}{easyex} \item}

\begin{document}

\colorlabel
\begin{enumerate}
    \gitem green
    \yitem yellow
    \ritem red
    \item red again
\end{enumerate}
\defaultlabel

\begin{enumerate}
    \item one
    \item two
\end{enumerate}

\colorlabel
\begin{enumerate}
    \gitem green
\end{enumerate}
\defaultlabel

\begin{enumerate}
    \item one
    \item two
\end{enumerate}

\end{document}

С другой стороны, я бы предпочел другую стратегию, а именно определение colorenumerateсреды.

\documentclass[a4paper,11 pt]{article}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{circledsteps}

\newenvironment{colorenumerate}[1][]{%
  \begin{enumerate}[#1]
  \renewcommand{\labelenumi}{%
    \Circled[inner color=white,fill color=mycolor,outer color=mycolor]{\textsf{\arabic{enumi}}}
  }%
}{\end{enumerate}}

\definecolor{hardex}{RGB}{204,51,0}
\definecolor{mediumex}{RGB}{204,153,0}
\definecolor{easyex}{RGB}{0,153,51}
\colorlet{mycolor}{easyex}

\newcommand{\ritem}{\colorlet{mycolor}{hardex}\item}
\newcommand{\yitem}{\colorlet{mycolor}{mediumex}\item}
\newcommand{\gitem}{\colorlet{mycolor}{easyex}\item}

\begin{document}

\begin{colorenumerate}
  \gitem green
  \yitem yellow
  \ritem red
  \item red again
\end{colorenumerate}

\begin{enumerate}
  \item one
  \item two
\end{enumerate}

\begin{colorenumerate}
  \gitem green
\end{colorenumerate}

\begin{enumerate}
  \item one
  \item two
\end{enumerate}

\end{document}

введите описание изображения здесь

Связанный контент