0

How can I get the width of the rule equal to the text for each item paragraph ? Any advice ?

\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}
  • Should the rule go to the right margin, or to the end of the item text? – Bernard Aug 1 at 14:37
  • @Bernard to the right margin for each item text. – F.HA Aug 1 at 14:46
  • Replace this line in your code:\draw[#1,line width=1pt](a.north west)--(a.north east)--++(\linewidth+3.5pt,0em); Note that \linewidth calculates the current width of the line and 3.5pt is necessary to compensate the width of the rectangle and the line thickness that you defined. – azetina Aug 1 at 14:47
  • By the way you had a mistake in your line of code at \begin{enumerate}[label=\protect\mapuce{cyan}{\arabic*}] Make sure you make the correction. – azetina Aug 1 at 14:49
  • @azetina, I want that the length of rule change as the length of text changed for the first line of item. Is it clear ? – F.HA Aug 1 at 14:52
2

Here is a MWE example looking at the changes that you recommend. I have also added a few changes regarding the entire width of the text and current text.

\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}

enter image description here

Note that if you want all the textwidth to be considered you can simply replace the following line in your code.

\draw[#1,line width=1pt](a.north west)--(a.north east)--++(\linewidth+3.5pt,0em);

Note that \linewidth calculates the current width of the line and 3.5pt is necessary to compensate the width of the rectangle and the line thickness that you defined.

  • That'as what want thanks ! – F.HA Aug 1 at 15:32
2

This should do what you want: in the \draw command, at the end of the tikzpicture, I replaced 4em with \linewidth (which is, a this place, the text width in the list).

 \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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.