Index ¦ Archives ¦ Atom

Making pastel-colored boxes using tcolorbox in LaTeX

One of the many questions I get when people see my \(\LaTeX\)-ed math notes is how do you create these nice pastel-colored boxes?

Example tcolorbox Example 2 tcolorbox

Two examples of pastel-colored boxes from my math notes

I feel like this is a very relevant and important question, because I asked the exact same thing when I was new to \(\LaTeX\).


Simple box using fbox and minipage

My first exposure to \(\LaTeX\) box-like environments was my mentor Ghaleo's genius tutorial notes. He used them for two main reasons:

  • to highlight important theorems, lemmas, etc;
  • an empty space for students to follow along and fill in the blanks live in class.

I tried to reproduce his box environment when I was writing my very first math notes on \(\LaTeX\) – my Linear Algebra & Geometry II notes.

Linear Algebra & Geometry II notes

A snippet of my Linear Algebra & Geometry II notes

It was achievable using the fbox environment alone, but for it to generalize well, I have to throw in a minipage usage as well. The code implementation is quite simple and requires no package imports. Just use the following code directly in your document:

% Simple box environment.
\fbox{ %
\begin{minipage}[t]{0.9\textwidth}
    % Your text here.
\end{minipage}
}

To make the box look cleaner, use the center environment as well. Here is a concrete example of using this box. The code:

\begin{document}

\begin{center}
\fbox{ %
\begin{minipage}[t]{0.9\textwidth}
    This is a plain small box.
\end{minipage}
}
\end{center}

\end{document}

would yield the box:

plainbox

Now the problem with this simple fbox is that... it's just plain black and white. I want something more vibrant and colourful, so I decided to start learning the tcolorbox package.


Transition to tcolorbox

As usual, the best way to learn is to straight dive in and do. After (very) little reading on the package's documentation, I started to recreate pretty boxes that other people have made from scratch. These boxes are usually from notes I found online like:

With my then minimal knowledge, I started writing my Real Analysis notes. I was quite happy with the end product but the overall design felt a bit odd (as you can see in the figure below). Nevertheless, my initial goal was to just make prettier boxes and I think I've achieved just that.

Real Analysis notes

A snippet of my Real Analysis notes

To reproduce the (proof) box above with a Cerulean colour, use the following code in your document's preamble:

% In preamble.
\usepackage[dvipsnames]{xcolor}
\usepackage[many]{tcolorbox}

\newtcolorbox{myAwesomeBox}{
    enhanced,
    sharp corners,
    breakable,  % Allows page break.
    borderline west={2pt}{0pt}{Cerulean},
    colback=Cerulean!10,  % Background color.
    colframe=Cerulean!10  % Frame (border) color.
}

To see this box in action, we instantiate this environment in our document:

\begin{document}

\begin{myAwesomeBox}
    This nice blue pastel box!
\end{myAwesomeBox}

\end{document}

This code should then yield:

Blue pastel box

I want to note two things regarding the imports:

  1. The dvipsnames option for xcolor is simply a choice of the set of colors I want from xcolor. It does affect the name of colors you can pass as arguments, for example, here I used Cerulean. For a full set of possible colors in xcolor, see here.

  2. The many option for tcolorbox loads additional libraries which allows us to use more features from tcolorbox. In particular for our use case, we want to be able to use enhanced. See the documentation for other options.

Here are some tweaks that you might want to consider:

  • If you want to add a nice smooth border-like effect, do this: instead of using colframe=Cerulean!10, remove that line and put boxrule=0pt instead. In general, boxrule adjusts the border stroke.

  • If you want to remove padding inside the box, add boxsep=0pt. In general, boxsep controls the inside padding.

There are many more parameters that you can control and these can be found in the tcolorbox documentation.

Nowadays, I write my notes using the NotesTex package which coincidentally uses tcolorbox with a similar setting as mine. I guess this is one of the reasons why it was a no brainer for me to use it in the first place. The nice thing about the package is that it provides a complete framework to write notes in \(\LaTeX\). In particular, you don't have to reinvent the wheel for everything (e.g. theorem environment, sidenotes). And for features that you feel are missing, you can simply add it on your own with ease. I've added plenty of features already and they integrate quite well. I'd highly recommend checking NotesTex out!

© Salman Faris. Built using Pelican. Theme by Giulio Fidente on github.