Using colors in LaTeX

Posted: 21st October 2011 by Tim in LaTeX
Tags: , , , , , , , , ,

If you want to add a bit of color to your LaTeX document, you can do this quite easily with the color package. This package works with both pdflatex and latex and gives you the following color options:

  • blue
  • cyan
  • green
  • magenta
  • red
  • yellow

To use the colors, simply include the color package and wrap you text in \color{<color>}{<text>}.

If you want to use a different color, then you can define it at the top of the document using the \definecolor{<name>}{<type>}{<values>} command. <type> and <value> combinations may be as follows:

type values example
rgb red, green and blue values between 0 and 1 \definecolor{dark_purple}{rgb}{0.4, 0.0, 0.4}
RGB red, green and blue values between 0 and 255 \definecolor{dark_purple}{RGB}{102, 0, 102}
cmyk cyan, magenta, yellow and black values between 0 and 1 \definecolor{dark_purple}{cmyk}{0.0, 1.0, 0.0, 0.6}
HTML red, green and blue values in hex between 00 and FF \definecolor{dark_purple}{HTML}{660066}
gray shades of gray between 0 and 1 \definecolor{dark_grey}{gray}{0.3}

Confused? Time for an example. This will create a document with the phrase hello world in dark purple:

\documentclass{article}
\usepackage{color}
\definecolor{dark_purple}{rgb}{0.4, 0.0, 0.4}
\begin{document}
    \color{dark_purple}{hello world}
\end{document}

  1. Sarah says:

    Is it possible to use color for a line?

  2. swapna says:

    Even I wanted to know can v give colour for line

  3. Nano Thermite 911 False Flag says:

    Is it possible to use HSV, Hue, Saturation, Luminosity or Value to define the color?

    It has the advantage that you keep H and S constant, and then tweak V to change your darkness.

    Kind regards,
    Nano Thermite 911 Red-Gray Chips