share
TeX - LaTeXHow to highlight an entire paragraph?
[+58] [3] ceiling cat
[2012-06-10 19:22:37]
[ color boxes paragraphs highlighting ]
[ https://tex.stackexchange.com/questions/59340/how-to-highlight-an-entire-paragraph ]

I'd like to highlight paragraphs such that the highlight color appears as one whole rectangle underlying the entire paragraph. I have looked around but nothing gives me the desired effect so far.

For example \fcolorbox{} from xcolor cannot highlight an entire paragraph; \hl{} from soul leaves out spacings between lines uncolored.

I am wondering how I can do this?

(4) use package mdframed. You'll get the documentation with running texdoc mdframed - user2478
(2) See also: marcodaniel.github.com/mdframed - Marco Daniel
The answers to yesterday's related question show how to achieve an uncoloured frame. - dgs
Sorry. Wanted to show how to accomplish that in HTML, but the board does not handle styles. - Alexander Bogomolny
[+78] [2012-06-10 19:48:31] Gonzalo Medina [ACCEPTED]

Here's a list of other possible solutions

  1. A solution that admits page breaks using mdframed [1]:

    \documentclass{article}
    \usepackage[framemethod=tikz]{mdframed}
    \usepackage{lipsum}
    
    \begin{document}
    
    \lipsum[4]
    \begin{mdframed}[hidealllines=true,backgroundcolor=blue!20]
    \lipsum[2]
    \end{mdframed}
    \lipsum[4]
    
    \end{document}
    

    enter image description here

    The environments provided by the mdframed package are highly customizable so, for example, using the (inner)leftmargin, (inner)rightmargin options, you can produce this (the textwidth remains unaltered and the frame extends a little into the margins):

    \documentclass{article}
    \usepackage[framemethod=tikz]{mdframed}
    \usepackage{lipsum}
    
    \begin{document}
    
    \lipsum[4]
    \begin{mdframed}[hidealllines=true,backgroundcolor=blue!20,innerleftmargin=3pt,innerrightmargin=3pt,leftmargin=-3pt,rightmargin=-3pt]
    \lipsum[2]
    \end{mdframed}
    \lipsum[4]
    
    \end{document}
    

    enter image description here

  2. Here's now a solution using the framed [2] package and its shaded environment; this solution also admits page breaks:

    \documentclass{article}
    \usepackage{xcolor}
    \usepackage{framed}
    \usepackage{lipsum}
    
    \colorlet{shadecolor}{blue!20}
    
    \begin{document}
    
    \lipsum[4]
    \begin{shaded}
    \lipsum[2]
    \end{shaded}
    \lipsum[4]
    
    \end{document}
    

    enter image description here

  3. Now a solution using the tcolorbox [3] package; this solution admits page breaks inside tcolorbox if you load the breakable library and use the breakable key:

    \documentclass{article}
    \usepackage{xcolor}
    \usepackage{tcolorbox}
    \tcbuselibrary{breakable}
    \usepackage{lipsum}
    
    \begin{document}
    
    \lipsum[4]
    \begin{tcolorbox}[breakable,notitle,boxrule=0pt,colback=blue!20,colframe=blue!20]
    \lipsum[2-20]
    \end{tcolorbox}
    \lipsum[4]
    
    \end{document}
    

    enter image description here

  4. A solution using the adjustbox [4] package (no page breaks allowed):

    \documentclass{article}
    \usepackage{xcolor}
    \usepackage{adjustbox}
    \usepackage{lipsum}
    
    \begin{document}
    
    \lipsum[4]
    
    \noindent\adjustbox{bgcolor=blue!20,minipage=[t]{\linewidth}}{\lipsum[4]}
    
    \lipsum[4]
    
    \end{document}
    

    enter image description here

  5. And finally, one solution using the fancypar [5] package (this solution won't accept page breaks either):

    \documentclass{article}
    \usepackage{fancypar}
    \usepackage{lipsum}
    
    \begin{document}
    
    \lipsum[4]
    \ZebraPar[colorone=blue!20,colortwo=blue!20]{\lipsum[4]}
    \lipsum[4]
    
    \end{document}
    

    enter image description here

[1] http://www.ctan.org/pkg/mdframed
[2] http://www.ctan.org/pkg/framed
[3] http://www.ctan.org/pkg/tcolorbox
[4] http://www.ctan.org/pkg/adjustbox
[5] http://www.ctan.org/pkg/fancypar

(3) Another option would be where the box extends slightly into the margins so that the paragraph flow does not get altered. - Peter Grill
(1) @PeterGrill yes; I added a note about the flexibility of mdframed and a new example. - Gonzalo Medina
(1) Can you provide a small list of packages. For example: adjustbox, tcolorbox and framed. I think all packages have their advantages. - Marco Daniel
@MarcoDaniel yes; it's a god idea. I've updated my answer with a list of examples using various packages. - Gonzalo Medina
Feedback for a recent recent TeXlive installation. framed approach: the shadecolor is not a valid identifier anymore. mdframed approach: it changes the paragraph indentation in the first line (w/o mdframed env the first line is indented, w/ mdframed it is not). - Jan-Philip Gehrcke
(1) The link to the framed package points to tcolorbox instead. - Jan-Philip Gehrcke
@Jan-PhilipGehrcke Thanks for noticing it. I've fixed the link. - Gonzalo Medina
1
[+19] [2012-06-10 19:37:11] doncherry

A combination of \colorbox and a \parbox works, as long as your paragraph doesn't need to be broken over pages. That won't work.

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}
\begin{document}
\noindent\colorbox{yellow}{%
    \parbox{\dimexpr\linewidth-2\fboxsep}% a box with line-breaks that's just wide enough
        {% your paragraph here:
        \lipsum[1]%
        }
    }
\end{document}

output


(2) The overfull box can be avoided very simple by \parbox{\dimexpr\linewidth-2\fboxsep} - Marco Daniel
@MarcoDaniel Nice solution to the overfull box :-) - azetina
@MarcoDaniel: Thanks, I included that. So \dimexpr means something like "calculate the following"? Where would I find an explanation of such macros? I didn't find anything in latex2e nor in macros2e. - doncherry
(2) @doncherry those are e-TeX macros: texdoc etex. - Gonzalo Medina
2
[+11] [2012-06-10 20:13:05] azetina

Is the todonotes [1] package a possiblity? If so, Highlighting a Paragraph in LaTeX [2] by Hinnerk Brügmann provides a solution. For some reason or the other the lipsum package didn't work well with it. The user provides the following code:

\documentclass[letterpaper]{article}
\usepackage{lipsum}
\usepackage[bordercolor=white,backgroundcolor=gray!30,linecolor=black,colorinlistoftodos]{todonotes}
\newcommand{\rework}[1]{%
\todo[color=yellow,inline]{Rework: {#1}}%
}
\begin{document}
\lipsum[2]
%------------------------------
% Update suggested by @Fran
%------------------------------
\rework{\protect\lipsum[2]}
%------------------------------
\lipsum[2]
\end{document}

[1] http://www.tex.ac.uk/tex-archive/macros/latex/contrib/todonotes/todonotes.pdf
[2] http://www.multiasking.com/2010/07/highlighting-a-paragraph-in-latex/

3