15

This question already has an answer here:

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.

marked as duplicate by karlkoeller, yo', user13907, Red, Mico Mar 11 '15 at 9:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    \colorbox doesn't support line breaks... – karlkoeller Mar 11 '15 at 8:42
  • 1
    One should keep in mind, that if this should be an article published by elsevier, they might reject the color box in advance – user31729 Mar 11 '15 at 8:50
  • If using soul \hl (great!) , you can change the color as such \definecolor{myBlue}{RGB}{0,191,255} \usepackage{soul} \sethlcolor{myBlue} [...] \hl{This text is highlighted} and this isn't. – user1323995 Feb 21 at 13:23
15

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

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