1

I have tried to highlight a quoted section in my document using \colorbox{}{}, but when I put in there a large section it doesn't fit in my page anymore.

\colorbox{yellow}{
Quote from very very very very loooooooooongs section:
The properties of depth-first search depend strongly on whether the 
}

The output I want to get is something like this:

Quote from very very very very loooooooooongs section:
The properties of depth-first search depend strongly on whether the

Tiuri
  • 7,749
Ryk
  • 35

1 Answers1

1

A \colorbox creates a one-line box that is wide enough to contain all of the text given as argument. If this text is longer than one line, the \colorbox will be too long for the line and reach into the margins. Also, because \colorbox is one-line only, you cannot use \\ inside.

One way to overcome this is to put the text that you want to highlight into a \parbox. A \parbox creates a box of specified width and breaks the lines inside to match this width. In this example, I create a \parbox of width \textwidth-2\fboxsep, where \fboxsep is the inner margin left free to the left and right of a colorbox. Thus, this box fits exactly inside the page margins.

\documentclass{article}
\usepackage{showframe}
\usepackage{xcolor}
\setlength\parindent{0pt}
\begin{document}
\colorbox{yellow}{
Quote from very very very very loooooooooongs section:
The properties of depth-first search depend strongly on whether the 
}

\colorbox{yellow}{\parbox{\dimexpr\textwidth-2\fboxsep}{
Quote from very very very very loooooooooongs section:\\
The properties of depth-first search depend strongly on whether the 
}}
\end{document}

enter image description here


However, it is probably easier to use the powerful tcolorbox package to achieve the same thing. Here is an example where I define an environment mybox resulting in the same output as above, but with an easier user interface.

\documentclass{article}
\usepackage{showframe}
\usepackage{tcolorbox}
\newtcolorbox{mybox}{colback=yellow,boxsep=0pt,left=\fboxsep,right=\fboxsep,top=\fboxsep,bottom=\fboxsep,boxrule=0pt,arc=0pt,outer arc=0pt}
\setlength\parindent{0pt}
\begin{document}
\begin{mybox}
Quote from very very very very loooooooooongs section:
The properties of depth-first search depend strongly on whether the 
\end{mybox}
\end{document}

enter image description here

Tiuri
  • 7,749