Last modified on 22 November 2014, at 14:25

LaTeX/Colors

LaTeX logo.svg

LaTeX

Getting Started
  1. Introduction
  2. Installation
  3. Installing Extra Packages
  4. Basics

Common Elements

  1. Document Structure
  2. Text Formatting
  3. Paragraph Formatting
  4. Colors
  5. Fonts
  6. List Structures
  7. Special Characters
  8. Internationalization
  9. Rotations
  10. Tables
  11. Title creation
  12. Page Layout
  13. Importing Graphics
  14. Floats, Figures and Captions
  15. Footnotes and Margin Notes
  16. Hyperlinks
  17. Labels and Cross-referencing

Mechanics

  1. Errors and Warnings
  2. Lengths
  3. Counters
  4. Boxes
  5. Rules and Struts

Technical Texts

  1. Mathematics
  2. Advanced Mathematics
  3. Theorems
  4. Chemical Graphics
  5. Algorithms
  6. Source Code Listings
  7. Linguistics

Special Pages

  1. Indexing
  2. Glossary
  3. Bibliography Management
  4. More Bibliographies

Special Documents

  1. Letters
  2. Presentations
  3. Teacher's Corner
  4. Curriculum Vitae

Creating Graphics

  1. Introducing Procedural Graphics
  2. MetaPost
  3. Picture
  4. PGF/TikZ
  5. PSTricks
  6. Xy-pic
  7. Creating 3D graphics

Programming

  1. Macros
  2. Plain TeX
  3. Creating Packages
  4. Themes

Miscellaneous

  1. Modular Documents
  2. Collaborative Writing of LaTeX Documents
  3. Export To Other Formats

Help and Recommendations

  1. FAQ
  2. Tips and Tricks

Appendices

  1. Authors
  2. Links
  3. Package Reference
  4. Sample LaTeX documents
  5. Index
  6. Command Glossary

edit this boxedit the TOC

Adding colors to your text is supported by the color package. Using this package, you can set the color of the font of the text, and set the background color of the page. You can use one of the predefined colors such as white, red, or yellow, or you can define your own named colors. It's also possible to color formulas in math-environments.

Adding the color packageEdit

To make use of these color features the color package must be inserted into the preamble.

\usepackage{color}

Alternatively, one can write:

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

The \usepackage is obvious, but the initialization of additional commands like 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. The package loaded here is the xcolor package.

If you need more colors, then you may also want to look at adding the x11names to the initialization section as well, this offers more than 300 colors, but you need to make sure your xcolor package is the most recent you can download.

Entering colored textEdit

The simplest way to type colored text is by:

\textcolor{declared-color}{text}

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

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.

LaTeX colour demo 1.png

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

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

\pagecolor{declared-color}

Entering colored background for the textEdit

\colorbox{declared-color}{text}

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

\colorbox{declared-color1}{\color{declared-color2}text}

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

\fcolorbox{declared-color-frame}{declared-color-background}{text}

Predefined colorsEdit

The predefined color names are

white, black, red, green, blue, cyan, magenta, 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 dvipsEdit

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.

\usepackage[usenames,dvipsnames]{xcolor}

Name Color   Name Color
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 colorsEdit

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

PlaceEdit

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.)

MethodEdit

In the abstract, the colors are defined following this scheme:

\definecolor{name}{model}{color-spec}

where:

  • 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 ModelsEdit

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

Color Models
Model Description Color Specification Example
gray Shades of gray
(0-1)
Just one number between 0 (black) and 1 (white), so 0.95 will be very light gray, 0.30 will be dark gray. \definecolor{light-gray}{gray}{0.95}
rgb Red, Green, Blue
(0-1)
Three numbers given in the form red,green,blue; the quantity of each color is represented with a number between 0 and 1. \definecolor{orange}{rgb}{1,0.5,0}
RGB Red, Green, Blue
(0-255)
Three numbers given in the form red,green,blue; the quantity of each color is represented with a number between 0 and 255. \definecolor{orange}{RGB}{255,127,0}
HTML Red, Green, Blue
(00-FF)
Six hexadecimal numbers given in the form RRGGBB; similar to what is used in HTML. \definecolor{orange}{HTML}{FF7F00}
cmyk Cyan, Magenta, Yellow, Black
(0-1)
Four numbers given in the form cyan,magenta,yellow,black; the quantity of each color is represented with a number between 0 and 1. \definecolor{orange}{cmyk}{0,0.5,1,0}

ExamplesEdit

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:

\definecolor{orange}{rgb}{1,0.5,0}

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

\definecolor{orange}{RGB}{255,127,0}

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.

\color{blue!20}
\color{blue!20!black}
\color{blue!20!black!30!green
}

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

\colorlet{notgreen}{blue!50!yellow}

Using colour specifications directlyEdit

Normally one would predeclare all the colours as above, but sometimes it is convenient to directly use a colour without naming it first. To achieve this, \color and \textcolor 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 colorsEdit

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 colour 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:

SourcesEdit


Previous: Paragraph Formatting Index Next: Fonts