答案1
正在寻找这样的东西:
这是使用定制enumitem
环境实现的,该环境劫持了\item
命令并将其替换为\colouredItem
。该\colouredItem
命令接受两个参数:
- 可选参数,用于给出线条和数字的颜色。在下面的代码中,
#1
可以是 之一g
,表示灰色、r
红色、b
蓝色或g
绿色。在底层,此语法\str_case:nn
使用LaTeX3
。 - 第二个强制参数给出了要定义的术语。这是使用 排版的
\textbf{...}
。
有了这个,上面的图像是使用以下方法创建的:
\begin{colouredenum}
\item{Definition 1} description
\item{Definition 2} description
\item[r]{Definition 3} description
\item[b]{Definition 4} description
\end{colouredenum}
此代码仅在每行定义适合一行物理行时才有效。如果定义超过多行,则将变得混乱。可以修改代码以应对这种情况,但这会稍微复杂一些。
以下是完整的 MWE:
\documentclass{article}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{xparse}
\newlist{colouredenum}{enumerate}{1}
\setlist[colouredenum]{
before=\let\item\colouredItem,
labelindent=0pt,
labelsep=0pt,
labelwidth=0pt,
leftmargin=0pt,
}
\let\realItem\item
\ExplSyntaxOn
\tl_new:N \l_linecolour_tl
\newcommand\colouredline[1]{
\str_case:nn {#1} {
{g} {\tl_set:Nn \l_linecolour_tl {gray} }
{r} {\tl_set:Nn \l_linecolour_tl {red} }
{b} {\tl_set:Nn \l_linecolour_tl {blue} }
{g} {\tl_set:Nn \l_linecolour_tl {green} }
}
\realItem[] % we want to underline the number so we fake it
\hspace*{0.1mm} % need something here to start the line
\rlap{\textcolor{\tl_use:N\l_linecolour_tl}{\rule[-3pt]{\linewidth}{1pt}}}%
\refstepcounter{enumi} % insert item number now that the line is drawn
\textcolor{\tl_use:N\l_linecolour_tl}{\theenumi.}\space
}
\ExplSyntaxOff
\NewDocumentCommand\colouredItem { O{g} m }{
\colouredline{#1}
\textbf{#2}\space
}
\begin{document}
\begin{colouredenum}
\item{Definition 1} description
\item{Definition 2} description
\item[r]{Definition 3} description
\item[b]{Definition 4} description
\end{colouredenum}
\end{document}
编辑
正如我所提到的,这个想法的一个变体适用于多行定义。这个想法是在下一个定义的开头绘制前一个定义的彩色线\item
。在下面的代码中,这是使用宏完成的 \lastColouredLine
。修订后的宏使用完全相同的语法产生以下输出:
您可以通过改变宏定义中的\rule[0.8em]
和来调整行距。\vspace*{-1.5em}
\l_lastcolouredline
以下是新代码:
\documentclass{article}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{xparse}
\newlist{colouredenum}{enumerate}{1}
\setlist[colouredenum]{
before=\let\item\colouredItem\let\lastColouredLine\relax,
after=\lastColouredLine,
labelindent=0pt,
labelsep=0pt,
labelwidth=0pt,
leftmargin=0pt,
}
\let\realItem\item
\ExplSyntaxOn
\tl_new:N \l_linecolour_tl
\newcommand\l_lastcolouredline{
\leavevmode\newline
\textcolor{\tl_use:N\l_linecolour_tl}{\rule[0.8em]{\linewidth}{1pt}}
\vspace*{-1.5em}
}
\newcommand\colouredline[1]{
\lastColouredLine % insert the last coloured line
\str_case:nn {#1} {
{g} {\tl_set:Nn \l_linecolour_tl {gray} }
{r} {\tl_set:Nn \l_linecolour_tl {red} }
{b} {\tl_set:Nn \l_linecolour_tl {blue} }
{g} {\tl_set:Nn \l_linecolour_tl {green} }
}
\realItem[] % we want to underline the number so we fake it
\let\lastColouredLine\l_lastcolouredline
\refstepcounter{enumi} % insert item number now that the line is drawn
\textcolor{\tl_use:N\l_linecolour_tl}{\theenumi.}\space
}
\ExplSyntaxOff
\NewDocumentCommand\colouredItem { O{g} m }{%
\colouredline{#1}%
\textbf{#2}\space
}
\begin{document}
\begin{colouredenum}
\item{Definition 1} here is a longer description that goes on and on
and on and on and on and on and on and on
\item{Definition 2} description
\item[r]{Definition 3} description here is a longer description that
goes on and on and on and on and on and on and on and on
\item[b]{Definition 4} description
\end{colouredenum}
\end{document}