我想在枚举列表中每个项目的第一行周围创建一个彩色框,其中该行还包含一个\hfill
。未修改的代码如下所示:
\documentclass[ 12pt,
a4paper,
twoside,
openright
]{report}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}[label=\textbf{\arabic*}.]
\item \textbf{Item 1} \hfill Text 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\item \textbf{Item 2} \hfill Text 2
Donec laoreet ultricies volutpat. Proin bibendum fringilla consequat.
\item \textbf{Item 3} \hfill Text 3
\end{enumerate}
\end{document}
输出结果
我希望彩色框有圆角,宽度填满整个\linewidth
,并且可以设置框的高度。
我环顾四周,发现有两种方法几乎可以满足我的要求,但不完全符合我的要求。第一的,此解决方案可以添加具有圆角和指定高度的彩色框,但我无法修改它以使其填满整个\linewidth
并遵守\hfill
命令。
例如:
\documentclass[ 12pt,
a4paper,
twoside,
openright
]{report}
\usepackage{enumitem}
\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds,fit,positioning}
\begin{document}
\tikzset{
remark/.style= {
rounded corners,fill=blue!15,anchor=base, inner sep=1.3mm,
}
}
\def\remarkedoriginal#1#2{% name, contents
\tikz[baseline]{
\node[remark] (#1) {#2};
}}
\begin{enumerate}[label=\textbf{\arabic*}.]
\item \remarkedoriginal{}{\textbf{Item 1} \hfill Text 1}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\item \remarkedoriginal{}{\textbf{Item 2} \hfill Text 2}
Donec laoreet ultricies volutpat. Proin bibendum fringilla consequat.
\item \remarkedoriginal{}{\textbf{Item 3} \hfill Text 3}
\end{enumerate}
\end{document}
产生输出:
第二,此解决方案可以创建一个填充整个\linewidth
并尊重的框\hfill
,但它似乎无法在同一个列表中创建多个框,而且我无法修改它以获得圆角或指定的高度。另外,我不喜欢使用 minipage 环境。
\documentclass[ 12pt,
a4paper,
twoside,
openright
]{report}
\usepackage{enumitem}
\usepackage{xparse}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\makeatletter
\NewDocumentCommand{\DrawBox}{s O{}}{%
\tikz[overlay,remember picture]{
\IfBooleanTF{#1}{%
\coordinate (RightPoint) at ($(left |- right)+(\linewidth-\labelsep-\labelwidth,0.0)$);
}{%
\coordinate (RightPoint) at (right.east);
}%
\draw[red,#2]
($(left)+(-0.2em,0.9em)$) rectangle
($(RightPoint)+(0.2em,-0.3em)$);}
}
\NewDocumentCommand{\DrawBoxWide}{s O{}}{%
\tikz[overlay,remember picture]{
\IfBooleanTF{#1}{%
\coordinate (RightPoint) at ($(left |- right)+(\linewidth-\labelsep-\labelwidth,0.0)$);
}{%
\coordinate (RightPoint) at (right.east);
}%
\draw[red,#2]
($(left)+(-\labelwidth,0.9em)$) rectangle
($(RightPoint)+(0.2em,-0.3em)$);}
}
\makeatother
\begin{document}
\begin{minipage}{1.0\linewidth}
\begin{enumerate}[label=\textbf{\arabic*}.]
\item \tikzmark{left}\textbf{Item 1} \hfill Text 1\tikzmark{right}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\item \tikzmark{left}\textbf{Item 2} \hfill Text 2\tikzmark{right}
Donec laoreet ultricies volutpat. Proin bibendum fringilla consequat.
\item \tikzmark{left}\textbf{Item 3} \hfill Text 3\tikzmark{right}
\end{enumerate}
\DrawBoxWide*[thick, blue, fill=yellow, fill opacity=0.2]
\end{minipage}
\end{document}
产生输出:
有人能帮助我实现期望的输出吗?
答案1
从您的问题中,我尝试使用 enumitem 包的选项修改枚举环境,然后我编写了一个命令,可以根据您的意愿制作颜色框(我想......)
经过一些修改,您可以使用枚举环境。它还会尊重示例的段落缩进,无论是枚举环境内部还是外部。
与线宽 hfill 相同宽度的框在\new@item
命令中运行良好,并且第一个参数使您能够使用 tiKz 语法选择颜色。
\usepackage{tikz}
\usepackage{enumitem}
\begin{document}
\makeatletter\def\new@item#1#2{
\item[]
\tikz[baseline,anchor=base]
\node[rounded corners,fill=#1,text width=\linewidth-4pt]{
\textbf{\theenumi.\quad\stepcounter{enumi}}#2};\setlength{\parindent}{15pt}}
\noindent$<$\hfill linewidth \hfill$>$
{\setlength{\parindent}{0pt}
\begin{enumerate}[left= 0pt .. \parindent]\stepcounter{enumi}
\new@item{blue!10}{hello\hfill text1}
foo
\new@item{red!10}{hello\hfill text2}
bar
\end{enumerate}}
some text with par indentation
\end{document}
答案2
如果不使用 tikz,则此代码:
\documentclass[ 12pt,
a4paper,
twoside,
openright
]{report}
\usepackage{enumitem}
{\lccode`\?=`\p \lccode`\!=`\t \lowercase{\gdef\ignorept#1?!{#1}}}
\edef\wbx/{\expandafter\ignorept\the\hsize}
\def\crbox{\pdfliteral{q 8 w 1 j .5 .5 1 rg .5 .5 1 RG 0 -3 \wbx/ 14 re B Q}}
\begin{document}
\begin{enumerate}[label=\textbf{\crbox\arabic*}.]
\item \textbf{Item 1} \hfill Text 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\item \textbf{Item 2} \hfill Text 2
Donec laoreet ultricies volutpat. Proin bibendum fringilla consequat.
\item \textbf{Item 3} \hfill Text 3
\end{enumerate}
\end{document}
如果您使用 XeTeX,那么您必须添加以下声明:
\def\pdfliteral#1{\special{pdf:literal #1}}