我想让每个除以 3 余数为 1 的项自动变为红色。手动包装项3n-2
将\textcolor{}
是一个荒谬的想法,尤其是在当前的人工智能时代。
平均能量损失
\documentclass[12pt]{article}
\usepackage{enumitem}
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{xcolor}
\begin{document}
\begin{multicols}{2}
\begin{enumerate}
\item \textcolor{red}{$3xy^2-5xy$}
\item $-2x^2y^3+14x^2y^2$
\item $-2a^2b-4ab^2+8ab$
\item \textcolor{red}{$2x^2+10xy-3xy-15y^2$}
\item $m^2-mn-mn+n^2$
\item $2\alpha^2+2\alpha\beta-3\alpha\beta-3\beta^2$
\item \textcolor{red}{$-2 x^2+13 x y-15 y^2$}
\item $-5 m^2-7 m n+6 n^2$
\item $-14 p^2+29 p q+15 q^2$
\item $-2 a^2-5 a b+12 b^2$
\item $8 r^2+14 r s-15 s^2$
\item $a^2-b^2$
\item $x^2-y^2$
\item $x^2-5x+6$
\item $x^3-5x^2+6x$
\item $-10x^2+17x-3$
\item $15x^2+23x-28$
\item $-30x^2-52x+42$
\item $30x^2-52x-42$
\item $-20x^2-52x+63$
\item $20x^2-52x-63$
\item $-30x^2+12x+42$
\item $30x^2+12x-42$
\item $-20x^2+12x+63$
\item $20x^2+12x-63$
\item $30x^2-73x+42$
\item $-30x^2-73x-42$
\item $20x^2-73x+63$
\item $-20x^2-73x-63$
\item $30x^2+71x+42$
\item $-30x^2+71x-42$
\item $20x^2+71x+63$
\item $-20x^2+71x-63$
\end{enumerate}
\end{multicols}
\end{document}
答案1
看起来该enumitem
包没有项目挂钩,所以我手动修补了\@item
。
当前设置只会对 的第一级中使用的项进行着色enumerate
。也就是说,嵌套列表环境的项不会被着色。
\documentclass[12pt]{article}
\usepackage{enumitem}
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{xcolor}
\usepackage{etoolbox}
\makeatletter
\ExplSyntaxOn
\setlist[enumerate, 1]{
before*={
\let\@item@orig\@item
\pretocmd\@item
{\normalcolor}
{}{\fail}
\ExplSyntaxOn
\apptocmd\@item
{
\int_compare:nNnT { \int_mod:nn{\use:c{c@\@listctr}}{3} } = {1}
{ \color{red} }
}
{}{\fail}
\ExplSyntaxOff
}
}
\setlist[2]{
before*=\normalcolor\let\@item\@item@orig
}
\ExplSyntaxOff
\makeatother
\begin{document}
\begin{multicols}{2}
\begin{enumerate}
\item $3xy^2-5xy$
\begin{enumerate}
\item a
\item a
\item a
\end{enumerate}
\item $-2x^2y^3+14x^2y^2$
\begin{itemize}
\item a
\item a
\item a
\end{itemize}
\item $-2a^2b-4ab^2+8ab$
\item $2x^2+10xy-3xy-15y^2$
\item $m^2-mn-mn+n^2$
\item $2\alpha^2+2\alpha\beta-3\alpha\beta-3\beta^2$
\item $-2 x^2+13 x y-15 y^2$
\item $-5 m^2-7 m n+6 n^2$
\item $-14 p^2+29 p q+15 q^2$
\item $-2 a^2-5 a b+12 b^2$
\item $8 r^2+14 r s-15 s^2$
\item $a^2-b^2$
\item $x^2-y^2$
\item $x^2-5x+6$
\item $x^3-5x^2+6x$
\item $-10x^2+17x-3$
\item $15x^2+23x-28$
\item $-30x^2-52x+42$
\item $30x^2-52x-42$
\item $-20x^2-52x+63$
\item $20x^2-52x-63$
\item $-30x^2+12x+42$
\item $30x^2+12x-42$
\item $-20x^2+12x+63$
\item $20x^2+12x-63$
\item $30x^2-73x+42$
\item $-30x^2-73x-42$
\item $20x^2-73x+63$
\item $-20x^2-73x-63$
\item $30x^2+71x+42$
\item $-30x^2+71x-42$
\item $20x^2+71x+63$
\item $-20x^2+71x-63$
\end{enumerate}
\end{multicols}
\end{document}
更新
(\int_case:nn
记录在LaTeX3 接口,第 XI.5 节,整数表达式条件),您将获得更大的灵活性。
\apptocmd\@item
{
\int_case:nn { \int_mod:nn{\use:c{c@\@listctr}}{3} }
{
{0}{ \color{red} }
{1}{ \color{blue} }
{2}{ \color{green} }
}
}
{}{\fail}
答案2
下面的操作可以满足您的需要,但是您不应该在枚举中使用嵌套任何环境\autocoloredlist
!
\autocoloredlist
有一个可选参数,您可以使用它来选择所需的模数(默认情况下使用3
)。
\documentclass[12pt]{article}
\usepackage{enumitem}
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{xcolor}
\usepackage{xpatch}
\makeatletter
\newif\if@autocoloredlist@group
\newcommand*\autocoloredlist[1][3]
{%
\xapptocmd\@item
{%
\ifnum\numexpr\value{enumi}-(\value{enumi}/#1)*#1=1
\begingroup\color{red}\@autocoloredlist@grouptrue
\fi
\ignorespaces
}%
{}{}%
\preto\item{\if@autocoloredlist@group\endgroup\fi}{}{}%
\preto\end{\if@autocoloredlist@group\endgroup\fi}{}{}%
}
\makeatother
\begin{document}
\begin{multicols}{2}
\begin{enumerate}[first=\autocoloredlist]
\item $3xy^2-5xy$
\item $-2x^2y^3+14x^2y^2$
\item $-2a^2b-4ab^2+8ab$
\item $2x^2+10xy-3xy-15y^2$
\item $m^2-mn-mn+n^2$
\item $2\alpha^2+2\alpha\beta-3\alpha\beta-3\beta^2$
\item $-2 x^2+13 x y-15 y^2$
\item $-5 m^2-7 m n+6 n^2$
\item $-14 p^2+29 p q+15 q^2$
\item $-2 a^2-5 a b+12 b^2$
\item $8 r^2+14 r s-15 s^2$
\item $a^2-b^2$
\item $x^2-y^2$
\item $x^2-5x+6$
\item $x^3-5x^2+6x$
\item $-10x^2+17x-3$
\item $15x^2+23x-28$
\item $-30x^2-52x+42$
\item $30x^2-52x-42$
\item $-20x^2-52x+63$
\item $20x^2-52x-63$
\item $-30x^2+12x+42$
\item $30x^2+12x-42$
\item $-20x^2+12x+63$
\item $20x^2+12x-63$
\item $30x^2-73x+42$
\item $-30x^2-73x-42$
\item $20x^2-73x+63$
\item $-20x^2-73x-63$
\item $30x^2+71x+42$
\item $-30x^2+71x-42$
\item $20x^2+71x+63$
\item $-20x^2+71x-63$
\end{enumerate}
\end{multicols}
\end{document}