Defining and Using Colours

By setting colour definitions in the stylesheet, you can reference them throughout the stylesheet or in the document.

If you have corporate colours, you will need to use these colours in your documentation, perhaps on the title page or in the headers or footers.

It is best to define these colours in the stylesheet. Several commands are provided for this.

color and xcolor packages

Before defining colours, you add the colour and xcolor packages. The color package provides basic colours to your document. You add it using the following command.

1
\usepackage{color}

The xcolor package allows you to define custom colours using one of the colour models such as gray, rgb, cmyk, and so on. Use the xcolour package if you want to define colours based on corporate branding.

Note: If you use the xcolor package, comment out the color package.

Setting up xcolor for the document

You can define xcolor for use in your document as follows:

1
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

where

Defining custom colours

Once you add the xcolor package, you can create a set of predefined colours that can be used throughout the document. You use the /definecolor command with the colour type and settings as arguments, as shown below.

1
\definecolor{name}{colour model}{specification}

The xcolor package provides many more options. I found that I only needed to use the rgb model, give the new colour a name and define its rgb values. For example, if you wanted to define a colour for use on the title page, give it a descriptive name, provide a colour model, and then provide colour values.

1
\definecolor{mynewcolor}{RGB}{25,25,25}

Packages and commands for the title page

You can define for the package xcolor color settings for the title page.

1
2
\definecolor{titlepagecolor}{RGB}{0,38,75} % Blue
\definecolor{namecolor}{RGB}{241,133,0} % Orange

The colours are given names so you can refer to them within the document. In the colour definitions, these are titlepagecolor and namecolor.

Colour definitions for main sections of documents

You can then define some colour settings for use in various elements in the document.

1
2
3
4
5
6
7
8
9
\definecolor{blue}{rgb}{0,38,75} % Blue

\definecolor{orange}{rgb}{241,133,0} % Orange

\definecolor{gray}{rgb}{0.4, 0.0, 0.4} % Gray

\definecolor{codeBackground}{RGB}{251, 251, 244} % background color for code boxes

%\definecolor{codeBackground}{white} % White

As above, these colours are given names for reference in the stylesheet and document.

More defined colours for boilerplate elements

Here are five colours that I defined within the stylesheet for use on the title page, headers and footers, and other elements. They use the rgb colour model.

1
2
3
4
5
\definecolor{blue}{rgb}{0,38,75}                 % Blue: title page
\definecolor{orange}{rgb}{241,133,0}             % Orange: headers/footers/headings
\definecolor{gray}{rgb}{0.4, 0.0, 0.4}           % Gray: as required.
\definecolor{codeBackground}{RGB}{251, 251, 244} % Code box background colour
\definecolor{codeBackground}{white}              % White: as required.

Defining colours for information Boxes

You can define background colour settings and line colour settings for information boxes used in the document.

1
2
3
4
5
6
7
\definecolor{warning}{RGB}{255,231,231}   % Background for Warning Box
\definecolor{note}{RGB}{255,255,211}      % Background for Note Box
\definecolor{info}{RGB}{224,239,255}      % Background Info Box
\definecolor{infoline}{RGB}{158,182,212}  % Line Color for Info Box
\definecolor{noteline}{RGB}{247,223,146}  % Line Color for Note Box
\definecolor{warnline}{RGB}{51,51,51}     % Line Colors for Warning Box
\definecolor{warnline}{RGB}{155,231,231}  % Additional line color for Warning Box

Using colour definitions

Once you have defined a colour, you can using it within other styles or commands in the stylesheet.

For example, I defined an orange colour for the title page:

1
\definecolor{chaptertitlecolor}{RGB}{232,134,12}

I then referenced this colour in the style for chapter titles.

1
2
3
4
\titleformat{\chapter}
  {\fontsize{20pt}{0em}
  \selectfont\bf\color{chaptertitlecolor}}
  {\thechapter.}{1em}{}

If I want to change the colour of chapter titles, I need only change the definition of chaptertitlecolor.