LaTeX/Colors

From Wikibooks, the open-content textbooks collection

< LaTeX(Redirected from LaTeX/Packages/Color)
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.

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}

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:

a standard black text, {\color{red} followed by a red one}, 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] The 68 standard colors known to dvips

Invoke the package with the usenames and dvipsnames option.

\usepackage[usenames,dvipsnames]{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
WildStrawberry Yellow YellowGreen YellowOrange

[edit] Defining new colors

When setting colors of the text, you can use one of the predefined names: white, black, red, green, blue, cyan, magenta, yellow. As these will probably not suffice, you can define new colors.

[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,128,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.

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

The models you can use to describe the color are the following:

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 number 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}
cmyk Cyan, Magenta, Yellow, Black four numbers given in the form cyan,magenta,yellow,black \definecolor{orange}{cmyk}{0,0.5,1,0}


Previous: Hyperlinks Index Next: Packages