0

I would like to create a colored box around the first row of each item in an enumerated list, where the row also contains an \hfill. The unmodified code looks like:

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

which gives the output

Unmodified output

What I would like to have is something that looks similar to this: Desired output

I would like the colored boxes to have rounded corners, a width that fills the entire \linewidth, and the posibility to set the height of the box.

I looked around and found two approaches that do almost what I want, but not exactly. First, this solution makes it possible to add a colored box with rounded corners and with a specified height, but I have not been able to modify it so that it fills the entire \linewidth and respects the \hfill command.

E.g.:

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

Produce the output:

Example output 1

Second, this solution makes it possible to create a box that fills the entire \linewidth and respects the \hfill, but it does not seem to be able to make multiple boxes in the same list, and I have not been able to modify it to get rounded corners or a specified height. Also, I do not like the use of a minipage environment.

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

Produce the output:

Example output 2

Can anyone help me achieve the desired output?

0

from your problem I tried to modify the enumerate env using options of the enumitem package then I wrote a command that makes a color box as you wish (I think...)

With a few modifications you can use the enumerate env. It also respects the paragraph indentation of your example, inside and outside the enumerate env.

the box as the same width of line width hfill works well in the \new@item command and first arg enable you to choose colors using tiKz syntax.

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

enter image description here Maybe it would help a little.

2
  • Many thanks! This is the kind of solution I was looking for. I only had to modify it slightly to get the text indent on the first and second row of each item to line up properly. Thanks again!
    – Elfendahl
    Dec 11 '20 at 21:33
  • Great ! You're welcome, have fun with those lines and colors now !
    – mxnc baud
    Dec 11 '20 at 23:05
0

Without using tikz, this code:

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

If you are using XeTeX then you have to add following declaration:

\def\pdfliteral#1{\special{pdf:literal #1}}
3
  • This is a very neat solution, but unfortunately it does not work with XeLaTeX, which is what I for various reasons must use in my specific application. Sorry I did not mention this in the original question. But with pdfLaTeX this works very nicely!
    – Elfendahl
    Dec 11 '20 at 21:32
  • @Elfendahl I added new notice about XeTeX in my answer.
    – wipet
    Dec 13 '20 at 8:12
  • Look at that! With that addition it does work in XeLaTeX! Thank you very much! I realize now I should have asked you about this and not just conclude that your solution did not work. Sorry about that. So now it seems I have two workable solutions to my little problem. That's great! I wish I could mark both answers as the solution, but I can't even upvote your solution because I am new here and do not yet have sufficient reputation to do that.
    – Elfendahl
    Dec 14 '20 at 9:37

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.