# How to seamlessy color the background of a series of \items

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:

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}
\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{enumerate}[resume]
\item \blob
\item \blob
\end{enumerate}
\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):

-
Related, if not duplicate question: Box around a few items in an itemize environment – Alan Munn Dec 10 '13 at 2:53
I edited the question after trying your suggestion --- it does not work across pages. – Rmano Dec 10 '13 at 3:31

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}


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.

-
Marking as good; adding some of the trick I posted in my own answer below will do. Still I would like to see a clean way to the "last item" problem (it would be nice if \begin{list}\end{list} could behave as a no-op instead of erroring out). – Rmano Dec 11 '13 at 21:51

I have found a "workaround"--- I need to define a new environment. The follow snippet will highlight all the parts of the list hlenum (admittely adding spaces --- I should mix it with the other solutions proposed here, but it's too late...) included between \starthl and \stophl. If you change the list to a enumerate normal one, the highlight will disappear.

\stophl could not be the last item in the list, but you can omit it if you want the highlight to reach the end of the list.

It is inspired by this answer adding a \global in setting the counter --- which means that it will work only for the outmost list.

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{framed,xcolor}
\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}
\newcounter{glistresume}
\newcommand{\realstarthl}{%
\global\setcounter{glistresume}{\theenumi}
\end{enumerate}
\begin{enumerate}
\setcounter{enumi}{\theglistresume}
}
\newcommand{\realstophl}{%
\global\setcounter{glistresume}{\theenumi}
\end{enumerate}
\begin{enumerate}
\setcounter{enumi}{\theglistresume}
}
\newcommand{\starthl}{\relax}
\newcommand{\stophl}{\relax}
\newenvironment{hlenum}{%
\let\starthl\realstarthl
\let\stophl\realstophl
\begin{enumerate}}{%
\end{enumerate}%
}
\begin{document}
\begin{hlenum}
\item \blob
\starthl
\item \blob
\item \blob
\stophl
\item \blob
\starthl
\item \blob
\stophl          %% you can comment these two lines
\item last one   %% to have an highlight till the end of the list
\end{hlenum}
\end{document}

-