LaTeX/Colors
Adding colors to your text is supported by the xcolor 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 | edit source]To make use of these features, the xcolor package must be imported. xcolor starts from the basic facilities of the color package and extends it.
\usepackage{xcolor}
|
The package allows you to use the names of 19 base colors (black, white, blue, green, yellow, red etc.); these names are always available. Besides, the package has some options to get more predefined colors, which should be added globally. dvipsnames allows you to access more than 60 colors, and svgnames allows access to about 150 colors. If you need more color names, then you may also want to look at the x11names option that offers more than 300 colors.
The table option allows colors to be added to tables.
Entering colored text
[edit | edit source]The simplest way to type colored text is by:
\textcolor{declared-color}{text}
|
where declared-color is a color that, if necessary, was previously defined 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.
|
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 text
[edit | edit source]
\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 colors
[edit | edit source]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 | edit source]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[dvipsnames]{xcolor}
|
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.
\documentclass[usenames,dvipsnames]{beamer}
|
Be wary that the below color names are case-sensitive. For example, \color{olivegreen}
raises an "undefined color" error, but \color{OliveGreen}
works fine. Table can be sorted by color name, by hue, by saturation, or by lightness.
Name | Color | Hex | Hue | Saturation | Lightness |
---|---|---|---|---|---|
Apricot | FBB982 | 27.3 | 239.2 | 190.5 | |
Aquamarine | 00B5BE | 182.8 | 255.0 | 95.0 | |
Bittersweet | C04F17 | 19.9 | 200.4 | 107.5 | |
Black | 221E1F | 0.3 | 15.9 | 32.0 | |
Blue | 2D2F92 | 238.2 | 134.8 | 95.5 | |
BlueGreen | 00B3B8 | 181.6 | 255.0 | 92.0 | |
BlueViolet | 473992 | 249.4 | 111.8 | 101.5 | |
BrickRed | B6321C | 8.6 | 187.0 | 105.0 | |
Brown | 792500 | 18.3 | 255.0 | 60.5 | |
BurntOrange | F7921D | 32.2 | 237.6 | 138.0 | |
CadetBlue | 74729A | 243.0 | 42.1 | 134.0 | |
CarnationPink | F282B4 | 333.2 | 207.0 | 186.0 | |
Cerulean | 00A2E3 | 197.2 | 255.0 | 113.5 | |
CornflowerBlue | 41B0E4 | 199.1 | 191.5 | 146.5 | |
Cyan | 00AEEF | 196.3 | 255.0 | 119.5 | |
Dandelion | FDBC42 | 39.1 | 249.7 | 159.5 | |
DarkOrchid | A4538A | 319.3 | 83.6 | 123.5 | |
Emerald | 00A99D | 175.7 | 255.0 | 84.5 | |
ForestGreen | 009B55 | 152.9 | 255.0 | 77.5 | |
Fuchsia | 8C368C | 300.0 | 113.0 | 97.0 | |
Goldenrod | FFDF42 | 49.8 | 255.0 | 160.5 | |
Gray | 949698 | 0.2 | 4.9 | 150.0 | |
Green | 00A64F | 148.6 | 255.0 | 83.0 | |
GreenYellow | DFE674 | 63.7 | 177.3 | 173.0 | |
JungleGreen | 00A99A | 174.7 | 255.0 | 84.5 | |
Lavender | F49EC4 | 333.5 | 203.1 | 201.0 | |
LimeGreen | 8DC73E | 85.4 | 140.3 | 130.5 | |
Magenta | EC008C | 324.4 | 255.0 | 118.0 | |
Mahogany | A9341F | 9.1 | 176.0 | 100.0 | |
Maroon | AF3235 | 358.6 | 141.7 | 112.5 | |
Melon | F89E7B | 16.8 | 229.3 | 185.5 | |
MidnightBlue | 006795 | 198.5 | 255.0 | 74.5 | |
Mulberry | A93C93 | 312.1 | 121.4 | 114.5 | |
NavyBlue | 006EB8 | 204.1 | 255.0 | 92.0 | |
OliveGreen | 3C8031 | 111.6 | 113.8 | 88.5 | |
Orange | F58137 | 23.4 | 230.7 | 150.0 | |
OrangeRed | ED135A | 340.5 | 218.9 | 128.0 | |
Orchid | AF72B0 | 299.0 | 71.9 | 145.0 | |
Peach | F7965A | 22.9 | 231.4 | 168.5 | |
Periwinkle | 7977B8 | 241.8 | 80.1 | 151.5 | |
PineGreen | 008B72 | 169.2 | 255.0 | 69.5 | |
Plum | 92268F | 301.7 | 149.7 | 92.0 | |
ProcessBlue | 00B0F0 | 196.0 | 255.0 | 120 | |
Purple | 99479B | 298.6 | 94.8 | 113.0 | |
RawSienna | 974006 | 24.0 | 235.5 | 78.5 | |
Red | ED1B23 | 357.7 | 217.7 | 132.0 | |
RedOrange | F26035 | 13.7 | 224.2 | 147.5 | |
RedViolet | A1246B | 325.9 | 161.8 | 98.5 | |
Rhodamine | EF559F | 331.2 | 211.1 | 162.0 | |
RoyalBlue | 0071BC | 203.9 | 255.0 | 94.0 | |
RoyalPurple | 613F99 | 262.7 | 106.3 | 108.0 | |
RubineRed | ED017D | 328.5 | 252.9 | 119.0 | |
Salmon | F69289 | 5.0 | 218.9 | 191.5 | |
SeaGreen | 3FBC9D | 165.1 | 127.0 | 125.5 | |
Sepia | 671800 | 14.0 | 255.0 | 51.5 | |
SkyBlue | 46C5DD | 189.5 | 175.8 | 145.5 | |
SpringGreen | C6DC67 | 71.3 | 159.5 | 161.5 | |
Tan | DA9D76 | 23.4 | 146.6 | 168.0 | |
TealBlue | 00AEB3 | 181.7 | 255.0 | 89.5 | |
Thistle | D883B7 | 323.3 | 133.0 | 173.5 | |
Turquoise | 00B4CE | 187.6 | 255.0 | 103.0 | |
Violet | 58429B | 254.8 | 102.7 | 110.5 | |
VioletRed | EF58A0 | 331.4 | 210.4 | 163.5 | |
White | FFFFFF | 0.1 | 0.0 | 255.0 | |
WildStrawberry | EE2967 | 341.1 | 217.5 | 139.5 | |
Yellow | FFF200 | 56.9 | 255.0 | 127.5 | |
YellowGreen | 98CC70 | 93.9 | 120.9 | 158.0 | |
YellowOrange | FAA21A | 36.4 | 244.1 | 138.0 |
Defining new colors
[edit | edit source]If the predefined colors are not adequate, you may wish to define your own.
Place
[edit | edit source]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.)
Method
[edit | edit source]You need to include the xcolor package in your preamble to define new colors. 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 Models
[edit | edit source]Among the models you can use to describe the color are the following (several more are described in the xcolor manual):
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} |
Examples
[edit | edit source]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 features a handy command to define colors from color mixes:
\colorlet{notgreen}{blue!50!yellow}
|
Using color specifications directly
[edit | edit source]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,
\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 colors
[edit | edit source]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:
- Linux/BSD: The gcolor2 tool (usually also available in repositories)
- Linux/BSD: in newer systems you may want to use gcolor3 tool (usually also available in repositories)
- Microsoft Windows: The open-source Color Selector tool.
- Apple Macs: Hex Color Picker for creating custom colors and the built-in DigitalColor Meter for capturing colors on screen.
- Online utilities: See here for a Wikipedia article with several external links
Spot colors
[edit | edit source]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}
|
Sources
[edit | edit source]