[Tex/LaTex] colorbox doesn’t wrap the text correctly

colorformatting

There is a problem with using \colorbox command. As you can see in the example and its output, the statements in the \colorbox doesn't correctly wrapped. Is there any suggestion for that?

\documentclass[review]{elsarticle}
\usepackage{color}

\begin{document}
\begin{frontmatter}
\begin{abstract}
This is an example to show that without using the colorbos command in latex, the text is correctly wrrapped as the formatting suggests.

\colorbox{red}{I am trying to understand how the colorbox command works in latex. As you can see, adding the a phrase inside the colorbox command causes the text to exceed the page boundary. However, I want to see a wrap!}

\end{abstract}
\end{frontmatter}
\end{document}

enter image description here

The Elsevier tex package is available here.


UPDATE

Apart from the solution by SoundsOfSilence, which is a working solution, I found that \usepackage{soul} and \hl{the text} is much better and supports line breaking. More info is available here.

Best Answer

You could introduce a \parbox, of width, say, 0.9\textwidth, for example or use a tcolorbox solution.

Spacings could be adjusted by using various options

\documentclass[review]{elsarticle}
\usepackage{xcolor}
\usepackage{tcolorbox}

\tcbset{width=0.9\textwidth,boxrule=0pt,colback=red,arc=0pt,auto outer arc,left=0pt,right=0pt,boxsep=5pt}
\begin{document}
\frontmatter
\begin{abstract}
This is an example to show that without using the colorbos command in latex, the text is correctly wrrapped as the formatting suggests.

\colorbox{red}{\parbox{0.9\textwidth}{I am trying to understand how the colorbox command works in latex. As you can see, adding the a phrase inside the colorbox command causes the text to exceed the page boundary. However, I want to see a wrap!}}

\begin{center}
\begin{tcolorbox}
I am trying to understand how the colorbox command works in latex. As you can see, adding the a phrase inside the colorbox command causes the text to exceed the page boundary. However, I want to see a wrap!
\end{tcolorbox}
\end{center}
\end{abstract}

\end{document}

enter image description here