如何在此枚举环境中自动分配数字

如何在此枚举环境中自动分配数字

考虑

\documentclass{book}
\usepackage{tcolorbox}

\newtcbox{\blackbox}[1][]{nobeforeafter, notitle, %sharpcorners,
     colframe=black,
     colback=black,
     top=4pt,
     left=5pt,
     right=5pt,
     bottom=2pt,
     fontupper=\sffamily\bfseries,
     colupper=white,
     tcbox raise base,
     #1}
\begin{document}
\thispagestyle{empty}
\Large
\begin{enumerate}
\item[\blackbox1] Can the numbers be assigned automatically?
\item[\blackbox2]
\item[\blackbox3]
\end{enumerate}
\end{document}

产生

在此处输入图片描述

我想问的是,对于 n 项枚举描述,除了键入\item[\blackbox1], \item[\blackbox2], ...,\item[\blackboxn]如何才能让 Latex 自动分配框内的数字;类似于标准枚举环境中发生的情况?

谢谢。

答案1

使用enumitem包,\setlist您可以更改文档的所有列表:

\documentclass{book}
\usepackage{tcolorbox}

\newtcbox{\blackbox}[1][]{nobeforeafter, notitle, %sharpcorners,
     colframe=black,
     colback=black,
     top=4pt,
     left=5pt,
     right=5pt,
     bottom=2pt,
     fontupper=\sffamily\bfseries,
     colupper=white,
     tcbox raise base,
     #1}
\usepackage{enumitem}
\setlist{label*={\blackbox{\arabic*}}}
\begin{document}
\thispagestyle{empty}
\Large
\begin{enumerate}
\item Can the numbers be assigned automatically?
\item Another item
\item The third item
\end{enumerate}
\end{document}

在此处输入图片描述

或者,按照以下方式,您可以只更改文档的一个(或一些)列表:

\documentclass{book}
\usepackage{tcolorbox}

\newtcbox{\blackbox}[1][]{nobeforeafter, notitle, %sharpcorners,
     colframe=black,
     colback=black,
     top=4pt,
     left=5pt,
     right=5pt,
     bottom=2pt,
     fontupper=\sffamily\bfseries,
     colupper=white,
     tcbox raise base,
     #1}
     
\usepackage{enumitem}

\begin{document}
\thispagestyle{empty}\Large

... and if only the following list should be changed but not any list of your document:

\begin{enumerate}[label*={\blackbox{\arabic*}}]
\item Can the numbers be assigned automatically?
\item Another item
\item The third item
\end{enumerate}

Ordinary list:

\begin{enumerate}
\item This is an ordinary list
\item Another item
\item The third item
\end{enumerate}

\end{document}

在此处输入图片描述

您还可以使用 定义自己的列表,\newlist并在需要时使用它。下面,我创建了blackboxlist

\documentclass{book}
\usepackage{tcolorbox}

\newtcbox{\blackbox}[1][]{nobeforeafter, notitle, %sharpcorners,
     colframe=black,
     colback=black,
     top=4pt,
     left=5pt,
     right=5pt,
     bottom=2pt,
     fontupper=\sffamily\bfseries,
     colupper=white,
     tcbox raise base,
     #1}
     
\usepackage{enumitem}
\newlist{blackboxlist}{enumerate}{1}
\setlist[blackboxlist]{label*={\blackbox{\arabic*}}}

\begin{document}
\thispagestyle{empty}\Large

Customized list:

\begin{blackboxlist}
\item Can the numbers be assigned automatically?
\item Another item
\item The third item
\end{blackboxlist}

Ordinary list:

\begin{enumerate}
\item This is an ordinary list
\item Another item
\item The third item
\end{enumerate}

\end{document}

在此处输入图片描述

正如 Mico 指出的那样,如果特殊数字不突出到左边距很重要,则可以将该选项添加left=0pt到以下参数中\setlist

\documentclass{book}
\usepackage{tcolorbox}

\newtcbox{\blackbox}[1][]{nobeforeafter, notitle, %sharpcorners,
     colframe=black,
     colback=black,
     top=4pt,
     left=5pt,
     right=5pt,
     bottom=2pt,
     fontupper=\sffamily\bfseries,
     colupper=white,
     tcbox raise base,
     #1}
     
\usepackage{enumitem}
\newlist{blackboxlist}{enumerate}{1}
\setlist[blackboxlist]{label*={\blackbox{\arabic*}},left=0pt}

\usepackage{mwe}% < -- for testing purpose only

\begin{document}
\thispagestyle{empty}\Large

\blindtext% < -- for testing purpose only


\begin{blackboxlist}
\item Can the numbers be assigned automatically?
\item Another item
\item The third item
\end{blackboxlist}

\blindtext% < -- for testing purpose only

\end{document}

在此处输入图片描述

相关内容