1. Introduction
Using colors in documents enhances their effectiveness and makes them more engaging and visually appealing to readers. Colors can be used in several scenarios like emphasizing key points or differentiating document sections with colorful titles.
A LaTeX document is generally organized into different components including chapters, sections, and subsections. For this tutorial, we’ll use the report document class, generally used to write long reports in LaTeX.
In this tutorial, we’ll learn different ways to create colorful titles in LaTeX.
2. Using the color Package
The color package provides color support for LaTeX documents. It is a part of latex-graphics bundle and requires a driver to print the device-independent file.
The color package has several options to alter the text color.
Let’s use a predefined color:
\documentclass{report}
\usepackage{color}
\begin{document}
\title{\color{red}Baeldung Colorful Titles}
\author{Author}
\date{\today}
\maketitle
\end{document}
This will generate a title page for our report: In the above example, the \usepackage{color} command loads the color package and \color{red} changes the current color to red until the end of the current environment, i.e., \title{}.
We can also use the \textcolor command instead of the \color command to get the same result:
\title{\textcolor{red}{Baeldung Colorful Title}}
In addition to the predefined colors, we can define custom colors with the \definecolor command:
\definecolor{lightblue}{rgb}{0.8,0.85,1}
Let’s understand each option used in this command:
- lightblue is the name of our defined color that can be used in later color commands
- rgb is a color model supported by the color package
- The last curly brace has a list of three numbers between 0 and 1, representing the red, green, and blue (rgb) components of the color, respectively
Additionally, we can disable all color commands by providing the monochrome option to the color package:
\usepackage[monochrome]{color}
3. Using the xcolor Package
xcolor is the extended color package for use in LaTeX documents. In addition to the commands and support from the color package, xcolor offers features like color tints, shades, tones, and complements in an easy driver-independent way.
With the xcolor package, we can specify cascaded color mixes of arbitrary colors using color expressions.
Let’s employ a color expression to change the color of a chapter title:
\documentclass{report}
\usepackage{xcolor}
\definecolor{lightblue}{rgb}{0.8,0.85,1}
\begin{document}
\chapter{\color{rgb:red,4;green,3;yellow,2}Baeldung Tests}
\section{\color{lightblue}Introduction}
Baeldung on ``Colorful Titles in LaTeX''
\end{document}
This results in: Here, we used an extended color expression to create a custom color by mixing 4 parts of red, 3 parts of green, and 2 parts of yellow.
The color model and the mixture of colors are separated by a colon (:) while the semicolon separates the colors (;). Finally, the comma (,) separates the color name from the number of parts of that color to add in a mixture of colors.
In this example, the section titles are colored but the section numbers remain unaffected by the color change. We can further change the color of section numbers using the titlesec package.
Moreover, we can also create a custom command for writing colorful text using the \newcommand command.
4. Using the titlesec Package
The titlesec package enables customization of section titles, especially in terms of formatting aspects.
Let’s change the color of section labels and numbers:
\documentclass{report}
\usepackage{xcolor}
\usepackage{titlesec}
\titleformat{\chapter}
{\normalfont\LARGE\bfseries\color{rgb:red,4;green,3;yellow,2}}{Chapter \thechapter}{1em}{}
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\titleformat{\section}
{\normalfont\LARGE\bfseries\color{blue}}{\thesection}{1em}{}
\titlespacing*{\section}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\begin{document}
\chapter{Baeldung Tests}
\section{Introduction}
Baeldung on ``Colorful Titles in LaTeX''
\end{document}
After compiling, we can see the colored section titles as well as colored section numbers:
As we can see, the section numbers are also colored in addition to the section titles.
Although the titlesec package allows us to format section titles, it has no built-in functions to add colors. Thus, we used the \color{} command from the xcolor package to color the section labels and titles.
The \titleformat{} command sets the formatting (text fonts and label style) for the mentioned section, i.e., chapters and sections. The \color{rgb:red,4;green,3;yellow,2} command specifies the color for the chapter title using the rgb color model.
In particular, the spacing before and after the chapter titles is controlled by the \titlespacing{} command.
5. Using the tcolorbox Package
The tcolorbox package was designed to create colored and framed boxes with a title in LaTeX documents.
Let’s create a basic colored box with tcolorbox:
\documentclass{report}
\usepackage{tcolorbox}
\begin{document}
\begin{tcolorbox}[coltitle=red,center title,fonttitle=\bfseries\large,colframe=white!75!blue,colback=blue!5!white,title=Colored Title]
Baeldung box with a new title.
\end{tcolorbox}
\end{document}
Compilation of this code results in:
The tcolorbox package uses tcolorbox environment to define and create colored and framed boxes, i.e. \begin{tcolorbox} … \end{tcolorbox}.
Let’s understand the options passed to the tcolorbox environment:
- coltitle sets the color of the title to a specified color, in our case, red
- center title centers the title horizontally within the colored box
- fonttitle specifies the font properties for the title, in this case, bold (\bfseries) and larger font (\large)
- colframe sets the background color of the frame including the upper part of the box (by default), in this case, a mixture of 75% white and 25% blue (white!75!blue)
- colback sets the background color of the bottom part of the box to light blue (blue!5!white)
- title specifies the title of the colored box
Additionally, the base package tcolorbox can be extended by program libraries. We can either load one or more of these libraries using the \tcbuselibrary{} command.
We can load all libraries while loading the tcolorbox package:
\usepackage[all]{tcolorbox}
This is a shortcut to load all libraries with a single command. Alternatively, tcolorbox enables us to load fewer libraries together using the many or most options.
Moreover, we can use rgb notation instead of named colors to define a custom color:
\definecolor{lightblue}{rgb}{0.8,0.85,1}
Now, we can use this defined lightblue color inside the tcolorbox definition.
6. Conclusion
In this article, we learned how to create colorful titles in LaTeX.
First, we used the color package to color the report title. It provides both foreground and background color management in a driver-dependent manner. Then, we defined a custom color that is compatible with both the color and xcolor packages. Furthermore, we practiced employing a color expression with the xcolor package. We customized the format and color of section labels using the combination of titlesec and xcolor packages. Finally, we used tcolorbox to create a colored box with a colored title.
Although we can select any method depending on our preferences and needs, the color package is the simplest way to create colorful titles in LaTeX.