4

I have some paragraphs to highlight for later edit in my latex document. For that I use the following code

\newcommand{\hl}[1]{\noindent\colorbox{pink}{\parbox{\dimexpr\linewidth-2\fboxsep} {#1}}}

This does job perfectly when there is a paragraph. But when I need to highlight only a word in a paragraph, I should be able to switch back to just

\colorbox{pink}{#1}

How to implement this automation in the above \newcommand{\hl}.

Kindly note that I did not want to add any package for this purpose other than xcolor for some reason.

MWE:

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}
\newcommand{\hl}[1]{\noindent\colorbox{pink}{\parbox{\dimexpr\linewidth-2\fboxsep} {#1}}} %Highlighter. 
\begin{document}
\hl{\lipsum[2]}

\lipsum[1]

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

\end{document}
7

This will measure the contents of the argument to \hl and use a \parbox if the contents are wider than \linewidth:

\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. 

Some other very, very \hl{longer inline text to demonstrate the command} that needs to be highlighted.

But note that this is a very limited solution. \TeX's paragraph building algorithm doesn't allow us to know how much of the line is left so, for example, this kind of thing happens: Some other very, very \hl{longer inline text to demonstrate the command} that needs to be highlighted.

\end{document}

enter image description here

  • I need this to be done automatically. may be an if statement which measure the length of the text and decides what to use with \parbox or \hbox – David Jun 18 '18 at 13:45
  • @David Oh, hold on a little. – Phelype Oleinik Jun 18 '18 at 13:46
  • @David Done. But it's a very limited approach. For something better, you would need a package, as in egreg's answer... – Phelype Oleinik Jun 18 '18 at 13:59
  • Thank you so much..Its works well and suits my question. But for curiosity, why this depreciate the Theorem in the theorem environment when we use \hl{\begin{theorem} \dots \end{theorem} }? – David Jun 18 '18 at 15:22
  • @David Because the command defined here writes its contents in a box and then puts this box to the output stream. The theorem environment has its contents typeset in a \trivlist (similar to an itemize), then it cannot be easily boxed and measured. Even if you put the \hl inside the theorem, things get weird because \trivlist does not allow paragraph breaks (then the Perhaps a missing \item error). You can, however, hack the theorem environment and use: \begin{theorem}[Theorem of stuff]\item\hl{\lipsum[2]}\end{theorem}, but remember: it wasn't me who suggested you to do it ;) – Phelype Oleinik Jun 18 '18 at 17:55
3

I suggest to use soul for this:

\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}

\sethlcolor{pink}

\begin{document}

\hl{Nam dui ligula, fringilla a, euismod sodales,
  sollicitudin vel, wisi. Morbi auctor lorem non justo. Nam lacus
  libero, pretium at, lobortis vitae, ultricies et, tellus. Donec
  aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna,
  vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit
  mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a nulla.
  Cum sociis natoque penatibus et magnis dis parturient montes,
  nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper
  vestibulum turpis. Pellentesque cursus luctus mauris.}

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

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.