I need to change the background of a number of (full) \item
s 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}
\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):
Best Answer
There is room for improvement, but I am exhausted!
Using
tcolorbox
:Using
mdframed
:Caveats The code is crude and there is lot of room for improvements. Further,
\myitem
can not be the last item.