LaTeX/Colors

From Wikibooks, open books for an open world
< LaTeX
Jump to: navigation, search

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.

Contents

[edit] Adding the color package

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

\usepackage{color}

[edit] Entering colored text

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 is by

{\color{declared-color} 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 \textcolor and \color is the same as that between \texttt and \ttfamily, you can use the one you prefer.

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

\pagecolor{declared-color}

[edit] Entering colored background for the text

\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-color1}{declared-color2}{text}

[edit] Predefined colors

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

[edit] The 68 standard colors known to dvips

Invoke the package with the usenames and dvipsnames option. If you are using TikZ package you must declare the color package before that, otherwise it will not work.

\usepackage[usenames,dvipsnames]{xcolor}
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

[edit] Defining new colors

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

[edit] Place

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

[edit] Method

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}

You can use small letters rgb and choose a value between 0 and 1 or use capital letters RGB and choose a value between 0 and 255. The following code should give a similar results to the last code chunk.

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

Trick : When surfing on the web, you can get hexadecimal code for each color on a web page using the colorzilla extension to Firefox.

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

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

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 and cmyk.
  • color-spec is the description of the color

[edit] Color Models

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. 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 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 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 six hexadecimal numbers given in the form RRGGBB; similar to what is used in HTML \definecolor{orange}{HTML}{FF7F00}
cmyk Cyan, Magenta, Yellow, Black four numbers given in the form cyan,magenta,yellow,black \definecolor{orange}{cmyk}{0,0.5,1,0}

[edit] Advanced color settings

The xcolor package provides extended versions of the described color related commands. Tints can be defined as follow:

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

The first specifies 20 percent blue, the second is a mixture of 20 percent blue and 80 percent black and the last one is a mixture of 20 percent blue, 30 percent black and 50 percent green.

Other features include: support of hsb color model, html style notation, special row coloring support in tables and more.

[edit] Sources


Previous: Hyperlinks Index Next: Fonts
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export