[Tex/LaTex] How to seamlessy color the background of a series of \items

backgroundscolorlists

I need to change the background of a number of (full) \items in a numbered list. I saw the answers to How to highlight an entire paragraph? and (naively) tried to do the following:

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[framemethod=tikz]{mdframed}
\newcommand{\blob}{A text which is long, but not so long  as a
  \texttt{\textbackslash lipsum}  paragraph and show the
  problem about changing the background color}
\begin{document}
\begin{enumerate}
  \item \blob
  \begin{mdframed}[hidealllines=true,backgroundcolor=blue!20]
  \item \blob
  \item \blob
  \end{mdframed}
  \item \blob
  \item \begin{mdframed}[hidealllines=true,backgroundcolor=blue!20]
        \blob   \end{mdframed} 
\end{enumerate}
\end{document}

Unfortunately, that does not work. The first mdframed simply stop the listng, and the second way of using it has a strange effect:

Example rendering

I would like to have just the background change, without changing the list spacing. The change of background will happen sometime across a page break.

I tried to use the method proposed in Box around a few items in an itemize environment, but that method has two shortcomings for my application:

  • (minor) it can just highlight one section in the list — if I need a couple of them it doesn't work;
  • (major) does not work if the highlighted text cross a page boundary.

Next I tried to suspend and resume the list, but it gave me other problems:

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{enumitem}
\usepackage{framed,xcolor}
\colorlet{shadecolor}{blue!20}
\newcommand{\blob}{A text which is long, but not so long  as a
  \texttt{\textbackslash lipsum}  paragraph and show the
  problem about starting and resuming lists multiple times}

\begin{document}
  \begin{enumerate}
    \item \blob
  \end{enumerate}
\begin{shaded}
  \begin{enumerate}[resume]
    \item \blob
    \item \blob
  \end{enumerate}
\end{shaded}
  \begin{enumerate}[resume]
    \item \blob
    \item last one
  \end{enumerate}
\end{document}

It seemed a good idea, but intermixing the shaded environment mangles the [resume] option (probably due to locality of definitions into the shaded environment):

Other failed try...

Best Answer

There is room for improvement, but I am exhausted!

Using tcolorbox:

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\tcbuselibrary{breakable}
\newcommand{\blob}{A text which is long, but not so long  as a
  \texttt{\textbackslash lipsum}  paragraph and show the
  problem about changing the background color}
\usepackage{enumitem,lipsum}
\newcommand{\myitem}[1]{%
    \end{enumerate}
    \begin{tcolorbox}[enhanced,title=,
                    frame hidden,
                    colback=blue!20,
                    breakable,
                    left=0pt,
                    right=0pt,
                    top=0pt,
                    bottom=4pt,
                    boxsep=0mm,
                    arc=0mm,boxrule=0pt,
                    nobeforeafter, 
                    ]
        \begin{enumerate}[resume*,itemsep=0pt,topsep=0pt,leftmargin=!]
            \item #1
        \end{enumerate}
    \end{tcolorbox}
    \begin{enumerate}[resume*]
}
\begin{document}
\begin{enumerate}
  \item \blob
  \item \blob
  \myitem \blob
  \item \blob
  \myitem \lipsum
  \item \blob              %% \myitem can't come at last :-(  
\end{enumerate}
\end{document}

enter image description here

Using mdframed:

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[framemethod=tikz]{mdframed}
\newcommand{\blob}{A text which is long, but not so long  as a
  \texttt{\textbackslash lipsum}  paragraph and show the
  problem about changing the background color}
\usepackage{enumitem,lipsum}
\newcommand{\myitem}[1]{%
    \end{enumerate}
    \begin{mdframed}[hidealllines=true,
                    backgroundcolor=blue!20,
                    leftmargin=0,
                    rightmargin=0,
                    innerleftmargin=0,
                    innerrightmargin=0,
                    skipabove=-3\itemsep,
                    skipbelow=0]
        \begin{enumerate}[resume*,itemsep=0pt,topsep=0pt,]
            \item #1
        \end{enumerate}
    \end{mdframed}
    \begin{enumerate}[resume*]
}
\begin{document}
\noindent X\hrulefill X
\begin{enumerate}
  \item \blob
  \myitem \blob
  \item \blob
  \myitem \lipsum
  \item \blob
  \item \blob
\end{enumerate}
\end{document}

Caveats The code is crude and there is lot of room for improvements. Further, \myitem can not be the last item.