答案1
看附录以供 OP 后续跟进。
已编辑以创建一个单独的环境,benumerate
。
这里,在benumerate
环境中,可选参数\item
表示难度级别。
这里是对新的/修订的宏的作用的简要说明,因为如果字体发生变化,它们可能需要进行一些修改,正如 OP 所希望的那样。
\difbox
:在项目编号下创建“难度框”。灰色框的大小和颜色由\rule
和\color
宏提供。它调用来\makeballs
设置灰色框中适当的圆圈数量。
\makeballs
\bullet
:将一组连续的s 作为框中的圆圈放置的递归宏。
benumerate
:通过重新定义一些事物并调用来完成所有这一切的环境enumerate
。
\item
:里面的benumerate
,\item
被重新定义为将难度级别作为可选参数(默认1
),它在调用通常的之前保存该参数\item
。
\labelenumi
:里面的标签重新定义benumerate
,它将 堆叠在\difbox
1pt
枚举标签下方(已设置为粗体,无改变颜色的字体)。
\smash
已编辑\labelenumi
,因为低垂的框影响了项目内容的行距。
\documentclass{article}
\usepackage{stackengine,xcolor,lipsum}
\let\svitem\item
\newcommand\difbox[1]{\stackengine{0pt}{\color{gray!30}\rule{5ex}{1.15ex}}{%
\color{cyan!60!black}$\mkern1mu\makeballs{#1}$}{O}{c}{F}{F}{L}}
\def\makeballs#1{\ifnum#1>0\relax{\bullet}%
\expandafter\makeballs\the\numexpr#1-1\relax\fi}
\newenvironment{benumerate}
{\renewcommand\item[1][1]{\def\difficulty{##1}\svitem}%
\def\labelenumi{\smash{\stackunder[1pt]{\color{cyan!60!black}%
\bfseries\sffamily\theenumi}{\difbox{\difficulty}}}}%
\enumerate}{\endenumerate}
\begin{document}
\begin{benumerate}
\setcounter{enumi}{32}
\item[2] \lipsum[4]
\item[3] \lipsum[4]
\item[4] \lipsum[3]
\end{benumerate}
\end{document}
附录
\makeballs{#1}
要处理原作者的后续扩展请求,解决方案很简单。在宏中的调用后difbox
,添加\textcolor{red}{\makeballs{\the\numexpr4-#1\relax}}
。
\documentclass{article}
\usepackage{stackengine,xcolor,lipsum}
\let\svitem\item
\newcommand\difbox[1]{\stackengine{0pt}{\color{gray!30}\rule{5ex}{1.15ex}}{%
\color{cyan!60!black}$\mkern1mu\makeballs{#1}%
\textcolor{red}{\makeballs{\the\numexpr4-#1\relax}}$}{O}{c}{F}{F}{L}}
\def\makeballs#1{\ifnum#1>0\relax{\bullet}%
\expandafter\makeballs\the\numexpr#1-1\relax\fi}
\newenvironment{benumerate}
{\renewcommand\item[1][1]{\def\difficulty{##1}\svitem}%
\def\labelenumi{\smash{\stackunder[1pt]{\color{cyan!60!black}%
\bfseries\sffamily\theenumi}{\difbox{\difficulty}}}}%
\enumerate}{\endenumerate}
\begin{document}
\begin{benumerate}
\setcounter{enumi}{32}
\item[2] \lipsum[4]
\item[3] \lipsum[4]
\item[4] \lipsum[3]
\end{benumerate}
\end{document}
答案2
另一种实现是。将使用新环境,tcolorbox
而不是环境\item
内部的。此环境使用强制参数(难度 1 到 4)和可选参数来更改一个或多个问题中的特定格式选项。enumerate
problem
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\newtcolorbox[auto counter]{problem}[2][]{%
breakable,
blankest,
enhanced,
attach boxed title to top left={yshift=-\tcboxedtitleheight},
left=\tcboxedtitlewidth+1mm,
top=0pt,
colback=white,
fonttitle=\bfseries\sffamily,
coltitle={cyan!60!black},
colbacktitle=white,
colframe=white,
title=\thetcbcounter,
overlay unbroken and first={%
\node[fill=gray!30, inner sep=1pt, minimum width=\tcboxedtitlewidth,
outer sep=0pt, text=cyan!60!black, anchor=north] at (title.south)
{\foreach \i in {1,...,#2}{\if#2gt1{\,}\fi$\bullet$}};},
#1}
\begin{document}
\lipsum[1]
\begin{problem}{1}
\lipsum[2]
\end{problem}
\begin{problem}{4}
\lipsum[3]
\end{problem}
\begin{problem}{2}
\lipsum[4]
\end{problem}
\end{document}
附录
在这种情况下,标题下方放置了四个球,并且它们的颜色由条件决定。
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\newtcolorbox[auto counter]{problem}[2][]{%
breakable,
blankest,
enhanced,
attach boxed title to top left={yshift=-\tcboxedtitleheight},
left=\tcboxedtitlewidth+2mm,
top=0pt,
colback=white,
fonttitle=\bfseries\sffamily,
coltitle={cyan!60!black},
colbacktitle=white,
colframe=white,
title=\thetcbcounter,
overlay unbroken and first={%
\node[fill=gray!30, inner sep=1pt,
minimum width=\tcboxedtitlewidth,
outer sep=0pt,
anchor=north] at (title.south) {%
\foreach \i in {1,...,4}{%
\ifnum\i>#2%
\,\textcolor{cyan!60!black}{$\bullet$}%
\else
\,\textcolor{red}{$\bullet$}%
\fi}};},
#1}
\begin{document}
\lipsum[2]
\begin{problem}{1}
\lipsum[2]
\end{problem}
\begin{problem}{2}
\lipsum[2]
\end{problem}
\begin{problem}{3}
\lipsum[2]
\end{problem}
\begin{problem}{4}
\lipsum[2]
\end{problem}
\end{document}
答案3
这个想法是创建一个新的enumerate
环境,我将其称为questions
,然后使用一个排版“难度点”的新命令before=\fakeitem
替换此环境中的标准命令。然后使用调用中的构造\item
绘制“难度点”。pics
\tikz
以下是代码:
\documentclass{article}
\usepackage{enumitem}
\usepackage{tikz}
\usepackage{lipsum}
\tikzset{pics/question/.style args={#1,#2}{
code={
\node[teal] at (0.5,0.35) {\textsf{#1}};
\fill[gray!30] (0.1,0) rectangle (0.9,0.2);
\foreach \ball [evaluate=\ball as \x using {0.5+(\ball-0.5-#2/2)*0.2)}] in {1,...,#2} {
\fill[teal] (\x,0.1) circle (2.5pt);
}
}
}
}
\let\realitem\item% save a copy of the \item command
\newcommand\fakeitem[1][\relax]{%
\refstepcounter{questionsi}% increment the counter
\realitem[\smash{\tikz[baseline]{\draw(0,-0.2) pic{question={\thequestionsi,#1}};}}]%
}
\newlist{questions}{enumerate}{1}% define a new enumerate environment
\setlist[questions]{ % set properties of the questions environment
label=\arabic*., % label format
leftmargin=*, % indent labels
before=\let\item\fakeitem, % change the \item command
resume, % continuous numbering
}
\begin{document}
\begin{questions}[start=32]
\item[2] \lipsum[4]
\item[3] \lipsum[4]
\item[4] \lipsum[3]
\end{questions}
\end{document}
输出如下:
附录
我对宏进行了小幅调整,\fakeitem
回答了帖子中的后续问题:
\documentclass{article}
\usepackage{enumitem}
\usepackage{tikz}
\usepackage{lipsum}
\tikzset{
pics/question/max/.initial = 5, % maximum number of balls
pics/question/.style args={#1,#2}{
code={
\node[teal] at (0.5,0.35) {\textsf{#1}};
\fill[gray!30] (0.1,0) rectangle (0.9,0.2);
\def\qmax{\pgfkeysvalueof{/tikz/pics/question/max}}
\foreach \ball [evaluate=\ball as \x using {0.1+(\ball-0.5)*0.8/\qmax}] in {1,...,\qmax} {
\fill[\ifnum\ball>#2 teal\else red\fi] (\x,0.1) circle (2.5pt);
}
}
}
}
\let\realitem\item% save a copy of the \item command
\newcommand\fakeitem[1][\relax]{%
\refstepcounter{questionsi}% increment the counter
\realitem[\smash{\tikz[baseline]{\draw(0,-0.2) pic{question={\thequestionsi,#1}};}}]%
}
\newlist{questions}{enumerate}{1}% define a new enumerate environment
\setlist[questions]{ % set properties of the questions environment
label=\arabic*., % label format
leftmargin=*, % indent labels
before=\let\item\fakeitem, % change the \item command
resume, % continuous numbering
}
\begin{document}
\begin{questions}[start=32]
\item[2] \lipsum[4]
\item[3] \lipsum[4]
\item[4] \lipsum[3]
\end{questions}
\end{document}
默认情况下,每个问题打印 5 个点。可以使用以下方法更改此设置,例如\tikzset{pics/question/max=4}
将其设置为 4 个点。理论上这可以是任意的,但实际上它应该在 2-5 范围内。以下是新输出的示例: