This is a continuation of this question.
The following code works very well to highlight a paragraphs(and inline texts too).
\makeatletter
\newcommand{\hl}[1]{%
\setbox\@tempboxa\hbox{#1}%
\ifdim\wd\@tempboxa>\linewidth
\noindent
\colorbox{pink}{%
\parbox{\dimexpr\linewidth-2\fboxsep}{#1}%
}%
\else
\colorbox{pink}{#1}%
\fi}%Highlighter.
\makeatother
But when we use for enumerate environment this gives the following error.
Something's wrong--perhaps a missing \item. }
As Phelype Oleinik mentioned in this comment \trivlist does not allow paragraph breaks and so I am curious to know how to hack this behaviour and give a global solution, even if I highlight over the pages irrespective of environments(eg. theorem) I use(like Microsoft Word, Libre office writer..etc).
How to hack the enumeration to highlight the enumerated list?
Minimal Working Example:
\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}
\makeatletter
\newcommand{\hl}[1]{%
\setbox\@tempboxa\hbox{#1}%
\ifdim\wd\@tempboxa>\linewidth
\noindent
\colorbox{pink}{%
\parbox{\dimexpr\linewidth-2\fboxsep}{#1}%
}%
\else
\colorbox{pink}{#1}%
\fi}%Highlighter.
\makeatother
\begin{document}
\hl{\lipsum[2]}
\lipsum[1]
Some inline \hl{ text} need to be highlighted.
\hl{
\begin{enumerate}
\item This enumeration gives error
\end{enumerate}
}
\end{document}

\setbox\@tempboxa\hbox{#1}% \ifdim\wd\@tempboxa>\linewidthas you can not have a vertical constrict in an\hboxI think you can simply delete that test, – David Carlisle Jun 18 '18 at 20:03parboxwhich gives a weird behavior. – David Jun 18 '18 at 20:06\colorbox{..}{test}for the inline case, almost all tex commands use different forms for h and v mode,\mboxfor hmode and\parboxfor v mode, why make complcated tests rather than have a vhl wrapping colorbox{parbox and a hhl wrapping colorbox – David Carlisle Jun 18 '18 at 20:08handvmodes. Is that correct?? – David Jun 18 '18 at 20:13