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.

3
  • 3
    \colorbox doesn't support line breaks... Mar 11, 2015 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, 2015 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. Feb 21, 2019 at 13:23

1 Answer 1

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

0

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