答案1
(主要框架是从 Torbjørn 那里偷来的 ;-),但这是实现 的一种方法tcolorbox
,定义总数TCBox
:
\documentclass{article}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\DeclareTotalTCBox[auto counter,number within=section]{\qbox}{+O{}}{
enhanced,
frame hidden,
sharp corners,
nobeforeafter,
colback=gray,
colupper=white,
size=small,
box align=base}{%
\thetcbcounter% Declare the content immediately, there the box number
}
\begin{document}
\section{Foo}
\qbox{}
And then some text and stuff.
\qbox{}
And so on. \qbox\qbox\qbox\qbox\qbox\qbox\qbox
\section{Bar}
Next section, start again with \qbox.
\end{document}
答案2
如果所有箱子都按顺序编号,您可以设置一个计数器来自动获取编号。在下面的示例中,我还在每个 处重置了计数器\section
,以作示范。如果您不想这样做,请删除该\counterwithin
行。
\documentclass{article}
\usepackage{xcolor}
\usepackage{chngcntr}
\newcounter{boxnumber}
% reset counter at every \section:
\counterwithin{boxnumber}{section}
\newcommand\qbox{%
\refstepcounter{boxnumber}%
\colorbox{gray}{\textcolor{white}{\textbf{\arabic{boxnumber}}}}%
}
\begin{document}
\section{Foo}
\qbox{}
And then some text and stuff.
\qbox{}
And so on. \qbox\qbox\qbox\qbox\qbox\qbox\qbox
\section{Bar}
Next section, start again with \qbox.
\end{document}