The standard tool for working with colour in LaTeX is the **xcolor** package. A superset that supersedes the older color package, it not only paints text and backgrounds but also lets you mix colours themselves through colour expressions such as red!50!blue — its defining feature. This page walks through loading it, the colour models, the named palettes, and mixing.
Loading the package
To use colour, just put \usepackage{xcolor} in the preamble. It was written by Uwe Kern (2003–2021; now maintained by the LaTeX Project) and is designed to be driver-independent, so colour comes out the same whether you target PDF or DVI. You will see \usepackage{color} in older documents, but for new work choose xcolor: colour mixing (expressions) and the extra name sets are xcolor-only features that color lacks.
Out of the box you get 19 base colour names, but options pull in extra predefined name sets. dvipsnames adds the 68 PostScript (dvips) colours, svgnames the ~151 SVG 1.1 / CSS colours, and x11names the ~317 X11 colours. The table option additionally loads **colortbl**, letting you shade table cells and rows (see the “Colored tables” page).
\usepackage{xcolor} % 基本(19 色)/ basics (19 colours)
\usepackage[dvipsnames]{xcolor} % + 68 色 / + 68 PostScript names
\usepackage[svgnames]{xcolor} % + 約151色 / + ~151 SVG names
\usepackage[x11names]{xcolor} % + 約317色 / + ~317 X11 names
\usepackage[dvipsnames,table]{xcolor} % 色名 + 表の着色 / names + colortblOptions can be combined — write [dvipsnames,svgnames] to pull in several sets at once. The names are designed not to clash internally, but it is cleaner to load only the sets you actually use.
Applying colour
The workhorse is **\textcolor{colour}{text}, which colours just the text you pass to it. When you would rather switch colour for everything that follows (instead of wrapping a span in braces), use the declarative form {\color{colour} … }** inside a group { }. Think of \color as a switch that stays in effect until the end of its group.
これは \textcolor{red}{赤い} 文字です。
{\color{blue} ここから先は青。} ここは元の色に戻ります。To paint a background, **\colorbox{colour}{text} puts the text on a solid filled box of that colour. For a frame and a fill in different colours, use \fcolorbox{framecolour}{fillcolour}{text} — the first argument is the border, the second the fill. To recolour the whole page background there is \pagecolor{colour}** (undo it with \nopagecolor). It affects every page from that point on, so place it carefully if you only want, say, a black title page.
\colorbox{yellow}{ハイライト}
\fcolorbox{red}{yellow}{赤枠・黄背景}
\pagecolor{black} % これ以降、ページ全体が黒地にEvery one of these commands accepts a colour expression or an explicit model in place of a bare name. For instance \textcolor{red!50!blue}{…}, or with a bracketed model \textcolor[rgb]{1,0,0}{…} (models are covered next).
Defining your own colours
To give your own colour a name, use **\definecolor{name}{model}{spec}**. Define it once in the preamble and afterwards it behaves like any other colour name in \textcolor and friends. For example, to register a muted blue as myblue:
\definecolor{myblue}{rgb}{0.2,0.4,0.8}
% 使う / use it:
\textcolor{myblue}{この文字は myblue}A second tool, **\colorlet{name}{colour}**, makes a new colour name from an existing colour (or expression). Where \definecolor builds a colour from a model and numbers, \colorlet names the result of mixing colours you already have. To name red tinted halfway toward white as half, write \colorlet{half}{red!50}. The ! mixing notation is detailed in the next section.
Colour models
The second argument to \definecolor is the colour model — the scheme for expressing a colour as numbers. xcolor supports several, from print-oriented cmyk to the HTML hex notation familiar from the web, so you can pick what suits the job. The common ones:
| Model | Values | Example |
|---|---|---|
rgb | Red, green, blue, each 0–1 | {0.2,0.4,0.8} |
RGB | Red, green, blue, each integer 0–255 | {51,102,204} |
HTML | Six hex digits, RRGGBB | {FF8800} |
cmyk | Cyan, magenta, yellow, black, each 0–1 | {0,0.5,1,0} |
gray | One value (0 = black, 1 = white) | {0.5} |
named | Refers to an existing colour name (e.g. dvips) | {Periwinkle} |
The same orange can be written several ways — only the notation differs, not the result. The four lines below all define essentially the same colour. HTML takes six hex digits, so you can paste a web colour code straight in; cmyk is for print (four-colour separation).
\definecolor{o1}{rgb}{1,0.5,0}
\definecolor{o2}{RGB}{255,128,0}
\definecolor{o3}{HTML}{FF8000}
\definecolor{o4}{cmyk}{0,0.5,1,0}You can also name a model inline, with brackets, at the point of use. For a one-off colour without defining a name, \textcolor[HTML]{FF8800}{…} or \color[gray]{0.5} is the quick way. The named model is a special one that refers to existing names (such as those from dvipsnames); in everyday use you can simply write the colour name directly.
Colour expressions — mixing
This is where xcolor shines. In place of a name, you can write a colour expression using the exclamation mark ! anywhere a colour is expected. The base form is **colourA!pct!colourB**, mixing pct percent of A with the rest as B. So red!50!blue is 50% red and 50% blue; red!75!blue is 75% red and 25% blue.
Omit colour B and the remainder is taken to be white. So red!20 means “20% red + 80% white” — a pale red (a tint). Mix with black instead and you get a darker shade: blue!30!black is “30% blue + 70% black,” a deep navy. A leading minus sign, as in -red, gives the complement (the inverse) of red.
red!50!blue** — purple, an even mix of red and bluered!20** — 20% red, 80% white: a pale red (tint)blue!30!black** — 30% blue, 70% black: a dark blue (shade)-red** — the complement of red (toward cyan)green!40!yellow!60** — expressions chain; mixing proceeds left to right
Because expressions work everywhere, you can write \textcolor{blue!30!black}{…}, \colorbox{yellow!40}{…}, and use them freely inside \definecolor or \colorlet. Name a mix once and you can retune your document’s whole palette from a single line — that is the design.
\textcolor{red!50!blue}{半分ずつの紫}
\colorbox{yellow!40}{薄い黄色の背景}
\colorlet{accent}{blue!30!black} % アクセント色を一括管理
\textcolor{accent}{見出しの色}Available colour names
Even with no options, these 19 base colours are always available.
- black, blue, brown, cyan, darkgray, gray, green, lightgray, lime, magenta, olive, orange, pink, purple, red, teal, violet, white, yellow
For a richer palette, add a name set with the options above. dvipsnames brings 68 colours such as RoyalBlue, Periwinkle, and BurntOrange; svgnames brings the ~151 CSS-familiar names like DarkSlateGray and Tomato. Names are case-sensitive, so writing royalblue for RoyalBlue gives an “undefined colour” error. To see what each looks like, the colour charts in the xcolor manual (open it with texdoc xcolor) are the reliable reference.