如何才能使规则的宽度等于每个项目段落的文本?有什么建议吗?
\documentclass[french,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{kpfonts}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes,calc,positioning}
\usepackage{babel}
\usepackage{enumitem}
\usepackage{lipsum}
%-----------------------------------------------------
%macro puce
%-----------------------------------------------------
\newcommand{\mapuce}[2]{%
\begin{tikzpicture}[baseline=(a.base),overlay]
\node[rectangle,inner sep=5pt,text centered,text=white,fill=#1,font=\bfseries](a){#2};
\draw[#1,line width=1pt](a.north west)--(a.north east)--++(4em,0em);
\end{tikzpicture}%
}
\setenumerate[1]{label=\protect\mapuce{red}{\arabic*},labelsep=1em,itemsep=1.1\baselineskip}
\begin{document}
\section{}
\lipsum[1]
\begin{enumerate}[label=\protect\mapuce{cyan}{\arabic*}]
\item Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.
\item Curabitur dictum gravida mauris.
\item eget sem vel leo ultrices bibendum. Aenean
\end{enumerate}
\end{document}
答案1
以下是一个 MWE 示例,其中介绍了您建议的更改。我还添加了一些有关文本整体宽度和当前文本的更改。
\documentclass[french,12pt]{article}
\usepackage[showframe]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{kpfonts}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{shapes,calc,positioning}
\usepackage{babel}
\usepackage{enumitem}
\usepackage{lipsum}
%-----------------------------------------------------
%macro puce
%-----------------------------------------------------
\newlength\mapucetextwidth %defines length of the current line
\newcommand{\mapuceitem}[1]{%
\setlength{\mapucetextwidth}{\widthof{#1}}% Saves the current line width
\item {#1}%
}
\newcommand{\mapuce}[2]{%
\begin{tikzpicture}[baseline=(a.base),overlay]
\node[rectangle,inner sep=5pt,text centered,text=white,fill=#1,font=\bfseries](a){#2};
\draw[#1,line width=1pt](a.north west)--(a.north east)--++(\mapucetextwidth+3.5pt,0em); % implements the current line width
\end{tikzpicture}%
}
\setenumerate[1]{label=\protect\mapuce{red}{\arabic*},labelsep=1em,itemsep=1.1\baselineskip}
\begin{document}
\section{}
\lipsum[1]
\begin{enumerate}[label=\protect\mapuce{cyan}{\arabic*}]
\mapuceitem{Lorem ipsum dolor}
\mapuceitem{Curabitur dictum gravida mauris.}
\mapuceitem{eget sem vel leo ultrices bibendum. Aenean}
\end{enumerate}
\end{document}
请注意,如果您希望textwidth
考虑所有内容,您只需在代码中替换以下行即可。
\draw[#1,line width=1pt](a.north west)--(a.north east)--++(\linewidth+3.5pt,0em);
注意,\linewidth
计算线的当前宽度并且3.5pt
需要补偿矩形的宽度和您定义的线条粗细。
答案2
这应该可以满足您的要求:在命令中\draw
,在 tikzpicture 的末尾,我将其替换4em
为\linewidth
(即,此处,列表中的文本宽度)。
\documentclass[french,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{kpfonts}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes,calc,positioning}
\usepackage{babel}
\usepackage{enumitem}
\usepackage{lipsum}
%-----------------------------------------------------
%macro puce
%-----------------------------------------------------
\newcommand{\Mapuce}[2]{%
\begin{tikzpicture}[baseline=(a.base),overlay]
\node[rectangle,inner sep=5pt,text centered,text=white,fill=#1,font=\bfseries](a){#2};
\draw[#1,line width=1pt](a.north west)--(a.north east)--++(\linewidth,0em);
\end{tikzpicture}%
}
\setlist[enumerate, 1]{label=\protect\Mapuce{red}{\arabic*}, labelsep=1em, itemsep=1.1\baselineskip}
\begin{document}
\section{}
\lipsum[1]
\begin{enumerate}[label=\protect\Mapuce{cyan}{\arabic*}]%
\item Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.
\item Curabitur dictum gravida mauris.
\item eget sem vel leo ultrices bibendum. Aenean
\end{enumerate}
\end{document}