1 Mikazshura

Usepackage Color Example Essay

My LaTeX preamble

Since I started my PhD I have forced myself to use LaTeX for all of the documents that I write (yes, absolutely everything), and this has really helped me get to grips with how to do things in LaTeX. Overall I have been very impressed – my documents now look really professional, and LaTeX actually works really well and isn’t that hard to get to grips with.

I thought I’d write this post to explain the various packages that I always load in my LaTeX preamble. I’ve often been meaning to put all of my preamble into a style file so that I can load it in one line, but I’ve never quite got round to it. Maybe I’ll write another blog post about this when I get round to doing it…

Anyway, without further ado, below is my standard LaTeX preamble, with brief comments explaining the packages I’ve loaded. Below the code I go into further detail about the packages I use most.

\documentclass[12pt]{article} % Pretty much all of the ams maths packages \usepackage{amsmath,amsthm,amssymb,amsfonts} % Allows you to manipulate the page a bit \usepackage[a4paper]{geometry} % Pulls the page out a bit - makes it look better (in my opinion) \usepackage{a4wide} % Removes paragraph indentation (not needed most of the time now) \usepackage{parskip} % Allows inclusion of graphics easily and configurably \usepackage{graphicx} % Provides ways to make nice looking tables \usepackage{booktabs} % Allows you to rotate tables and figures \usepackage{rotating} % Allows shading of table cells \usepackage{colortbl} % Define a simple command to use at the start of a table row to make it have a shaded background \newcommand{\gray}{\rowcolor[gray]{.9}} \usepackage{textcomp} % Provides commands to make subfigures (figures with (a), (b) and (c)) \usepackage{subfigure} % Typesets URLs sensibly - with tt font, clickable in PDFs, and not breaking across lines \usepackage{url} % Makes references hyperlinks in PDF output \usepackage{hyperref} % Provides ways to include syntax-highlighted source code \usepackage{listings} \lstset{frame=single, basicstyle=\ttfamily} % Provides Harvard-style referencing \usepackage{natbib} \bibpunct{(}{)}{;}{a}{,}{,} % Provides good access to colours \usepackage{color} \usepackage{xcolor} % Simple command I defined to allow me to mark TODO items in red \newcommand{\todo}[1] {\textbf{\textcolor{red}{#1}}} % Allows fancy stuff in the page header \usepackage{fancyhdr} \pagestyle{fancy} % Vastly improves the standard formatting of captions \usepackage[margin=10pt,font=small,labelfont=bf, labelsep=endash]{caption} % Standard title, author etc. \title{COMP6024\\Model Specification based on \citet{Telfer:2010}} \author{Robin Wilson\\ID: 21985588} \date{} % Put text on the left-hand and right-hand side of the header \fancyhead{} \lhead{COMP6024} \rhead{Robin Wilson} \chead{}

So, which of those are most important, and why do I use them:

  • amsmath – This is a package by the American Mathematical Society for typesetting mathematics sensibly. I’m not a mathematician, but I often have to include maths in my documents. Nearly all of the tutorials you read on the internet for doing maths in LaTeX will be for amsmath, and it generally gives very good results (and every possibly mathematical symbol/feature you will ever need).
  • parskip – I hate indented paragraphs in wordprocessed documents – I’m sure there are various typsetting rules against doing it. Anyway, this removes all paragraph indentation without screwing anything else up!
  • booktabs, rotating and colortbl – These provide everything I need to produce good quality tables. Booktabs gives me a number of different options for alignment, as well as providing and which give thicker lines for the top and bottom of tables. Rotating allows you to produce sideways tables and figures when they won’t fit properly on a portrait page, and. Colortbl allows shading of tables cells. I’ve defined my own command here because I often want to shade a table cell with a light gray. This command () is simply replaced with .
  • subfigure – This gives a really simple way to include various image files as one figure, labelling each one as (a), (b), (c), with different captions. Simple, but works really well.
  • url – Again, this does one very simple thing very well: it typsets URLs sensibly. Simply wrap the url in and it typesets it in typewriter font, makes it into a clickable link (in PDF files) and stops it breaking over lines
  • caption – This is one of the most important packages in my list. It allows you to style the captions produced for tables and figures to make them actually look different from the rest of the text. This is very important to help guide the reader around the page. The way I call the package is , and this makes the caption indented, in a smaller font, with the label (eg. Figure 10) in bold and separated from the rest of the caption by a dash. Again, this really tidies up the appearance of documents.
  • fancyhdr – This lets you put useful things in the header of the document. For example, for my university work I put my name on the right-hand side of the header, and the course code on the left-hand side of the header. The simple and commands do that for me easily.

I haven’t been through all of my packages above, but the rest of them are fairly easily understandable. In my view, they are the really key packages for almost any type of LaTeX document.

Categorised as:Academic, LaTeX

Adding colors to your text is supported by the package (supersedes package color). Using this package, you can set the font color, text background, or page background. You can choose from predefined colors or define your own colors using RGB, Hex, or CMYK. Mathematical formulas can also be colored.

Adding the xcolor package[edit]

To make use of these features, the xcolor package must be imported.

The package has some options to get more predefined colors, which should be added globally. usenames allows you to use names of the default colors, the same 16 base colors as used in HTML. The dvipsnames allows you access to more colors, another 64, and svgnames allows access to about 150 colors. The initialization of "table" allows colors to be added to tables by placing the color command just before the table.

If you need more colors, then you may also want to look at the x11names option. This offers more than 300 colors.

Entering colored text[edit]

The simplest way to type colored text is by:


where declared-color is a color that was defined before by .

Another possible way by

{\color{declared-color}some text}

that will switch the standard text color to the color you want. It will work until the end of the current TeX group. For example:

\emph{some black text, \color{red}followed by a red fragment}, going black again.

The difference between and is the same as that between and , you can use the one you prefer. The environment allows the text to run over multiple lines and other text environments whereas the text in must all be one paragraph and not contain other environments.

You can change the background color of the whole page by:


Entering colored background for the text[edit]


If the background color and the text color is changed, then:


There is also \fcolorbox to make framed background color in yet another color:


Predefined colors[edit]

The predefined color names are

black, blue, brown, cyan, darkgray, gray, green, lightgray, lime, magenta, olive, orange, pink, purple, red, teal, violet, white, yellow.

There may be other pre-defined colors on your system, but these should be available on all systems.

If you would like a color not pre-defined, you can use one of the 68 dvips colors, or define your own. These options are discussed in the following sections

The 68 standard colors known to dvips[edit]

Invoke the package with the usenames and dvipsnames option. If you are using tikz or pstricks package you must declare the xcolor package before that, otherwise it will not work.


This above syntax may result in an error if you are using beamer with tikz. To go around it, include usenames and dvipsnames options when defining the document class.

NameColor ColorName
Apricot   Aquamarine
Bittersweet   Black
Blue   BlueGreen
BlueViolet   BrickRed
Brown   BurntOrange
CadetBlue   CarnationPink
Cerulean   CornflowerBlue
Cyan   Dandelion
DarkOrchid   Emerald
ForestGreen   Fuchsia
Goldenrod   Gray
Green   GreenYellow
JungleGreen   Lavender
LimeGreen   Magenta
Mahogany   Maroon
Melon   MidnightBlue
Mulberry   NavyBlue
OliveGreen   Orange
OrangeRed   Orchid
Peach   Periwinkle
PineGreen   Plum
ProcessBlue   Purple
RawSienna   Red
RedOrange   RedViolet
Rhodamine   RoyalBlue
RoyalPurple   RubineRed
Salmon   SeaGreen
Sepia   SkyBlue
SpringGreen   Tan
TealBlue   Thistle
Turquoise   Violet
VioletRed   White
WildStrawberry   Yellow
YellowGreen   YellowOrange

Defining new colors[edit]

If the predefined colors are not adequate, you may wish to define your own.


Define the colors in the preamble of your document. (Reason: do so in the preamble, so that you can already refer to them in the preamble, which is useful, for instance, in an argument of another package that supports colors as arguments, such as the listings package.)


You need to include the xcolor package in your preamble to define new colors. In the abstract, the colors are defined following this scheme:



  • name is the name of the color; you can call it as you like
  • model is the way you describe the color, and is one of gray, rgb, RGB, HTML, and cmyk.
  • color-spec is the description of the color

Color Models[edit]

Among the models you can use to describe the color are the following (several more are described in the xcolor manual):

ModelDescriptionColor SpecificationExample
Shades of gray
Just one number between 0 (black) and 1 (white), so 0.95 will be very light gray, 0.30 will be dark gray.
Red, Green, Blue
Three numbers given in the form red,green,blue; the quantity of each color is represented with a number between 0 and 1.
Red, Green, Blue
Three numbers given in the form red,green,blue; the quantity of each color is represented with a number between 0 and 255.
Red, Green, Blue
Six hexadecimal numbers given in the form RRGGBB; similar to what is used in HTML.
Cyan, Magenta, Yellow, Black
Four numbers given in the form cyan,magenta,yellow,black; the quantity of each color is represented with a number between 0 and 1.


To define a new color, follow the following example, which defines orange for you, by setting the red to the maximum, the green to one half (0.5), and the blue to the minimum:


The following code should give a similar results to the last code chunk.


If you loaded the xcolor package, you can define colors upon previously defined ones.

The first specifies 20 percent blue and 80 percent white; the second is a mixture of 20 percent blue and 80 percent black; and the last one is a mixture of (20*0.3) percent blue, ((100-20)*0.3) percent black and (100-30) percent green.


xcolor also feature a handy command to define colors from color mixes:


Using color specifications directly[edit]

Normally one would predeclare all the colors as above, but sometimes it is convenient to directly use a color without naming it first. To achieve this, and have an alternative syntax specifying the model in square brackets, and the color specification in curly braces. For example:

{\color[rgb]{1,0,0} This text will appear red-colored}\textcolor[rgb]{0,1,0}{This text will appear green-colored}

Creating / Capturing colors[edit]

You may want to use colors that appear on another document, web pages, pictures, etc. Alternatively, you may want to play around with rgb values to create your own custom colors.

Image processing suites like the free GIMP suite for Linux/Windows/Mac offer color picker facilities to capture any color on your screen or synthesize colors directly from their respective rgb / hsv / hexadecimal values.

Smaller, free utilities also exist:

Spot colors[edit]

Spot colors are customary in printing. They usually refer to pre-mixed inks based on a swatchbook (like Pantone, TruMatch or Toyo). The package colorspace extends xcolor to provide real spot colors (CMYK and CIELAB). They are defined with, say:

\definespotcolor{mygreen}{PANTONE 7716 C}{.83, 0, .40, .11}


Leave a Comment


Your email address will not be published. Required fields are marked *