[Tex/LaTex] How to highlight an entire paragraph

boxescolorhighlightingparagraphs

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?

Best Answer

Here's a list of other possible solutions

  1. A solution that admits page breaks using mdframed:

    \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 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 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 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 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