13

There are many questions in this site on changing the background color of quoted text, but I am bewildered and disheartened by the huge proliferation of different ways to do this, each using a different additional package. I can't believe that such a common and conceptually simple task doesn't have one basic, canonical solution. (This solution may be inadequate for specific situations, of course, but only then I would consider bringing in speciallized tools.)

Basically, I'm looking for something analogous in appearance to

this effect (but without the fancy borders, just the background color change)

...or

this effect (but without the change of font or foreground color, just the background color change)

...or preferably somewhere in-between, namely, the simple color change of the latter, but without the change of font.

Doesn't "bare LaTeX" provide a way to do this?

kjo
  • 861

3 Answers3

16

A simple code based on framed and quoting which can break across pages:

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{framed}
\usepackage{quoting}

 \colorlet{shadecolor}{LavenderBlush2}
\usepackage{lipsum}
\newenvironment{shadedquotation}
 {\begin{shaded*}
  \quoting[leftmargin=0pt, vskip=0pt]
 }
 {\endquoting
 \end{shaded*}
}

\begin{document}

\lipsum*[1-4]
\begin{shadedquotation}
\lipsum*[5-6]
\end{shadedquotation}
\lipsum*[7-10]

\end{document} 

enter image description here

Bernard
  • 271,350
12

Color support needs xcolor (or just color).

\documentclass{article}
\usepackage{xcolor}

\usepackage{lipsum} % for mock text

\newsavebox{\coloredquotationbox}
\newenvironment{coloredquotation}
 {%
  \begin{trivlist}
  \begin{lrbox}{\coloredquotationbox}
  \begin{minipage}{\dimexpr\linewidth-2\fboxsep}
 }
 {%
  \end{minipage}
  \end{lrbox}
  \item\relax
  \parbox{\linewidth}{
    \begingroup
    \color[RGB]{224,215,188}%
    \hrule
    \color[RGB]{249,245,233}%
    \hrule
    \color[RGB]{224,215,188}%
    \hrule
    \endgroup
    \colorbox[RGB]{249,245,233}{\usebox{\coloredquotationbox}}\par\nointerlineskip
    \begingroup
    \color[RGB]{224,215,188}%
    \hrule
    \color[RGB]{249,245,233}%
    \hrule
    \color[RGB]{224,215,188}%
    \hrule
    \endgroup
  }
  \end{trivlist}
 }

\begin{document}

\lipsum*[3]
\begin{coloredquotation}
\lipsum*[4]
\end{coloredquotation}
\lipsum*[5]

\end{document}

enter image description here

egreg
  • 1,121,712
  • 5
    @kjo should be aware (as of course you are) that this will not break across pages, which is one of the reasons why package solutions such as tcolorbox exist. – Alan Munn Jun 08 '18 at 22:45
  • Thanks! Even though I used this site's quotation look as a convenient example, it is far fancier than I need. In particular, the top and bottom rules for the quotation box are unnecessary. IMO, this answer would be greatly improved if the solution were simplified as much as possible, by removing the top and bottom rules. IOW, the difference between the quoted and unquoted text could be as minimal as only a change in the background color. – kjo Jun 13 '18 at 15:01
8

An example with tcolorbox (is not almost canonical?) was missing.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\definecolor{linequote}{RGB}{224,215,188}
\definecolor{backquote}{RGB}{249,245,233}

\newtcolorbox{myquote}{%
    enhanced, breakable, 
    size=fbox,
    frame hidden, boxrule=0pt,
    sharp corners,
    colback=backquote,
    borderline horizontal={.5pt}{0pt}{linequote},
    borderline horizontal={.5pt}{1pt}{linequote}
}

%% Important!!
%% Use ! before "O{}" with xparse 2018-05-12 
%% See: https://tex.stackexchange.com/q/434928/1952
\NewTCBListing{mycode}{ !O{} }{%
    enhanced, breakable, 
    size=fbox,
    frame hidden, boxrule=0pt,
    sharp corners,
    colback=gray!30,
    listing only, 
    listing options={%
        style=tcblatex,
        keywordstyle=\color{brown!70!black},
        texcsstyle=*\color{brown!70!black}
    },
    #1}

\listfiles
\begin{document}
\lipsum[1]

\begin{myquote}
\lipsum[2]
\end{myquote}

\begin{mycode}
\documentclass{article}
\begin{document}
    Hello
\end{document}
\end{mycode}

\end{document}

enter image description here

Update A minimal default version with an optional parameter to change anything.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\definecolor{linequote}{RGB}{224,215,188}
\definecolor{backquote}{RGB}{249,245,233}

\newtcolorbox{myquote}[1][]{%
    enhanced, breakable, 
    size=minimal,
    frame hidden, boxrule=0pt,
    sharp corners,
    colback=backquote,
    #1
}


\begin{document}
\lipsum[2]

\begin{myquote}
\lipsum[2]
\end{myquote}

\begin{myquote}[colback=red!30, size=small]
\lipsum[2]
\end{myquote}

\end{document}

enter image description here

Ignasi
  • 136,588
  • (Repeating the comment I wrote to egreg.) Thanks! Even though I used this site's quotation look as a convenient example, it is far fancier than I need. In particular, the top and bottom rules for the quotation box are unnecessary. IMO, this answer would be greatly improved if the solution were simplified as much as possible, by removing the top and bottom rules. IOW, the difference between the quoted and unquoted text could be as minimal as only a change in the background color. – kjo Jun 13 '18 at 15:03
  • 2
    @kjo I've updated a more minimal version, no top and bottom lines, no space around the paragraph, just background color. Although you can change the default design with the optional parameter. And once the box is defined, it's use is like a regular quotation environment. – Ignasi Jun 13 '18 at 16:28