0

I am attempting to use colorbox from xcolor to highlight changes to a revised manuscript I am submitting (my first LaTeX based manuscript). I have found that the method works well for single to multiple word changes but after ~5 words the colorbox either alters the spacing of the entire paragraph or, in the case of entire sentence changes, extends past the bounds of the page as a single like.

For example, my original paragraph might look like

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\newcommand{\highlight}[1] {\colorbox{yellow}{#1}}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vitae turpis nulla. Nam tincidunt fringilla elementum. Suspendisse et sem in velit bibendum pellentesque. Suspendisse luctus neque dignissim libero hendrerit mattis. Suspendisse arcu mi, dignissim eu porta nec, porta at ligula. Aliquam laoreet odio non consequat congue. Praesent non aliquam mauris, et convallis lectus. Donec id vulputate diam. Integer fringilla, libero quisvolutpat efficitur, augue massa vulputate enim, id hendrerit diam sapien vel lorem. 

\end{document}

enter image description here

But highlighting a phrase near the edge of a paragraph I get

enter image description here

Even worse, when highlighting multiple words and even sentences like so

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\newcommand{\highlight}[1] {\colorbox{yellow}{#1}}

\begin{document}

\highlight{Lorem} ipsum dolor sit amet, \highlight{consectetur adipiscing} elit. Vestibulum vitae turpis nulla. Nam tincidunt fringilla \highlight{elementum}. Suspendisse et sem in velit bibendum pellentesque. Suspendisse luctus neque dignissim libero hendrerit mattis. \highlight{Suspendisse arcu mi, dignissim eu porta nec, porta at ligula. Aliquam laoreet odio non consequat congue.} Praesent non aliquam mauris, et convallis lectus. Donec id vulputate diam. Integer fringilla, libero quisvolutpat efficitur, augue massa vulputate enim, id hendrerit diam sapien vel lorem. 

\end{document}

I get this ugly mess running off the edge of the page and adding spacing between lines when a highlight in the line above aligns with one in the line below.

enter image description here


Is there a way to fix my text highlighting implementation? Is there a better package to use that supports multi-line highlighting that works under Overleaf?

1
  • a colorbox is like \mbox so an unbreakable one line box, dont use it for in-paragraph highlights, see for example the soul package for highlight commands. Commented Mar 14, 2022 at 20:04

1 Answer 1

1

Try the soul (package). The command is \hl{<text>}.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\newcommand{\highlight}[1] {\colorbox{yellow}{#1}}

\usepackage{soul}% added <<<<<<<<<<

\begin{document}
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vitae turpis nulla. Nam tincidunt fringilla elementum. Suspendisse et sem in velit bibendum pellentesque. Suspendisse luctus neque dignissim libero hendrerit mattis. Suspendisse arcu mi, dignissim eu porta nec, porta at ligula. Aliquam laoreet odio non consequat congue. Praesent non aliquam mauris, et convallis lectus. \hl{Donec id vulputate diam. Integer fringilla, libero quisvolutpat efficitur, augue massa vulputate enim, id hendrerit diam sapien vel lorem. }
    
\end{document}

a

Change the color with

\definecolor{lightblue}{rgb}{.90,.95,1}
\sethlcolor{lightblue}

b

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .