3

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}

David
  • 1,964
  • obviously you are allowed paragraphs in lists so I'm not sure i understand your comment abut trivlist. The problem is \setbox\@tempboxa\hbox{#1}% \ifdim\wd\@tempboxa>\linewidth as you can not have a vertical constrict in an \hbox I think you can simply delete that test, – David Carlisle Jun 18 '18 at 20:03
  • If I delete the test, for the inline text it will use the parbox which gives a weird behavior. – David Jun 18 '18 at 20:06
  • sure but why complicate the test you can just use \colorbox{..}{test} for the inline case, almost all tex commands use different forms for h and v mode, \mbox for hmode and \parbox for 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:08
  • I think I did not get u properly. What I understood from your comment is that I should make two different commands for h and v modes. Is that correct?? – David Jun 18 '18 at 20:13
  • That's what I would do. It is possible to make the test a bit more robust but it's always going to be the most complicated and fragile part of the code for what is basically the cosmetic feature of avoiding having two commands. – David Carlisle Jun 18 '18 at 20:38

1 Answers1

5

Why not nest enumerate in a shaded* environment? It will have the advantage to break across pages. Here's a code with the framedand enumitem packages:

\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
\usepackage{framed}
\usepackage{enumitem}

\begin{document}
\hl{\lipsum[2]}

\lipsum[1]

Some inline \hl{ text} need to be highlighted.

\colorlet{shadecolor}{pink}
\begin{shaded*}
\begin{enumerate}[wide = 0pt, before=\vspace*{-\dimexpr\topsep+\partopsep+1.5ex}, after=\vspace*{-\dimexpr\topsep+\partopsep+1.5ex}]
    \item This enumeration gives no error
    \item This enumeration gives no error
\end{enumerate}
\end{shaded*}
 Some normal text.

\end{document} 

enter image description here

Bernard
  • 271,350
  • Hi friend Bernard I wanted to inform you that I quoted you in my reply. Thanks and good work. https://tex.stackexchange.com/questions/436663/graphic-tree-like-symbols-in-text – Sebastiano Jun 18 '18 at 20:48
  • Oh! yes. That's a good solution. – Bernard Jun 18 '18 at 21:00
  • Do you think this is a good idea? After the strong contrast with a user, I think that many have ignored me. is a feeling. – Sebastiano Jun 18 '18 at 21:03
  • For me it's fine. I had thought of using this symbol, but not of scaling it slightly as you did, so I finally chose to mix two larger symbols. – Bernard Jun 18 '18 at 21:06
  • Yeah...I think this is a good solution after your explanation on why there should be two \newcommands rather than checking the length. I just have once question: Why the * in \begin{shaded*} ...? – David Jun 19 '18 at 03:51
  • 1
    @David: The framed package defines two variants of the environment: shaded, for which the contents has its normal width, between the margins, hence the shading overflows into margins (by \fboxsep), and shaded*, for which it is the shading which is \textwidth wide, hence the contents has à slightly smaller width (equal to \textwidth-2\fboxsep). – Bernard Jun 19 '18 at 07:16