24

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.

mahmood
  • 3,359
  • 3
    \colorbox doesn't support line breaks... – karlkoeller Mar 11 '15 at 08: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 –  Mar 11 '15 at 08: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 '19 at 13:23

1 Answers1

23

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