hckr.fyi // thoughts

Change the Background Color of a Blockquote in LaTeX

by Michael Szul on

Anyone who has been following me on this web site is well aware that I've been diligently working on a Bot Framework book for close to two years now. I've gone through two revisions, five proofreading sessions, and countless code changes. I've been signed to an O'Reilly Media contract, had my contract pulled, been contacted by two other publishers, and ultimately have been powering along at my own pace, doing my own thing.

I'm almost done.

The book itself is completely finished. It's been copy-edited, had some technical reviews, and is in the process of being indexed. All that remains in the finer points of formatting and typesetting.

I wrote the book in Markdown, but in order to produce a PDF proof, I used Pandoc to convert it to LaTeX (which creates the final PDF). The formatting was… off. I had to start diving into LaTeX, which was something that I never thought I'd have to do.

By default, > block quote indicators in markdown translate to \begin{quote} syntax in LaTeX. This will give you a centered text block with margins on either side. Not exactly distinguishing, and it certainly doesn't provide the type of notation or separation I wanted.

Luckily, you have all kinds of options in LaTeX. Ultimately, I decided on the following solutions.

First, we import the tcolorbox package:

\usepackage[most]{tcolorbox}
    

Next, we define a color for the block quote. In this case, we call it "block-gray":

\definecolor{block-gray}{gray}{0.85}
    

Lastly, we define our color box:

\newtcolorbox{blockquote}{colback=block-gray,grow to right by=-1mm,grow to left by=-1mm,boxrule=0pt,boxsep=0pt,breakable}
    

This creates a new color box like the command suggestions. We call this color box "blockquote". With the options, we define the background color, and how we want the block quote to grow (from the left and from the right). We then set some boxing rules, and whether or not the block quote can be breakable.

In the case of our example, I have a -1 millimeter margin growth, which makes the block quote slightly smaller in width than the standard paragraph text.

We use this command like any other custom command:

\begin{blockquote}
    This is some text that will appear is a slightly smaller box with a gray background.
    \end{blockquote}
    

That's it. Quick and simple--although it did take some research to find exactly what I wanted, and even that took some modifications. LaTeX is a rich format with a pretty big learning curve, so as I work through some of the formatting issues, you mind find a few more posts here on the subject.