How to Change Text Color in LaTeX

Load \usepackage{xcolor} and write \textcolor{red}{this text} for a few words, or {\color{blue} …} to color everything until the closing brace. Define your own colors with \definecolor{brand}{HTML}{1F6FEB}. The same commands color parts of equations.

Color commands (xcolor)

  • \textcolor{red}{x}
    x

    Color one argument.

  • {\color{blue} x + y}
    x + y

    Color from here to the end of the group.

  • \definecolor{brand}{HTML}{1F6FEB}
    Text mode, no math preview

    Custom color from a hex code; also {RGB}{31,111,235}.

  • \textcolor{red!50}{x}
    Text mode, no math preview

    Mix: red at 50% with white (xcolor syntax).

  • \colorbox{yellow}{x}
    x

    Background highlight.

  • \fcolorbox{red}{yellow}{x}
    x

    Highlight with a colored frame.

Try it live

Change the formula and the preview redraws as you type. Copy the LaTeX, download a PNG or SVG, copy it for Word, or use Edit here on any example below to load it.

Templates

Text color examples

Each result below is drawn from the LaTeX under it. Copy the code, edit it on this page, or open it in the full editor.

  • Highlighting terms

    f ( x ) = a x 2 + b x + c
    LaTeX
    f(x) = \textcolor{blue}{a}x^2 + \textcolor{red}{b}x + \textcolor{green}{c}
  • Coloring a step

    ( a + b ) 2 = a 2 + 2 a b + b 2
    LaTeX
    (a + b)^2 = a^2 + \color{red}{2ab} + b^2
  • Highlighted result

    x = 4
    LaTeX
    \colorbox{yellow}{$x = 4$}
  • Cancelled term in gray

    x y x = y
    LaTeX
    \frac{\textcolor{gray}{\cancel{x}}\,y}{\textcolor{gray}{\cancel{x}}} = y

In a LaTeX document

These commands work in the text of a .tex document (Overleaf, TeX Live, MiKTeX), not inside math. The box on the right shows what the compiled PDF looks like.

  • Colored words in text

    LaTeX
    \usepackage{xcolor}
    ...
    This is \textcolor{red}{important}, and {\color{blue} this whole phrase is blue}.
    Compiled result

    This is important, and this whole phrase is blue.

Named and custom colors

xcolor knows basic names (red, green, blue, cyan, magenta, yellow, black, gray, white, darkgray, lightgray, brown, lime, olive, orange, pink, purple, teal, violet). Load it with [dvipsnames] for 68 more names such as ForestGreen and NavyBlue.

For brand or chart colors use \definecolor with HTML hex or RGB values, and mixes such as blue!30!white for lighter shades.

Color and readability

Readers who print in grayscale or have color vision deficiencies may not see the difference between red and green. Pair color with another cue (bold, a label, a pattern) and keep enough contrast against the background.

Common mistakes

  • Without xcolor (or color) the command is undefined.

    Instead of
    \textcolor{red}{x}  (no xcolor)
    Write
    \usepackage{xcolor}
  • \color does not take the text as an argument; it colors everything after it in the group.

    Instead of
    \color{red}{this text}
    Write
    \textcolor{red}{this text}
  • The HTML model takes the six hex digits without #.

    Instead of
    \definecolor{brand}{HTML}{#1F6FEB}
    Write
    \definecolor{brand}{HTML}{1F6FEB}

In Overleaf, MathJax and Word

  • Overleaf and pdfLaTeX

    xcolor is preinstalled; load it with \usepackage{xcolor} (or [dvipsnames] for more names).

  • MathJax and KaTeX

    MathJax and KaTeX support \color{red}{x} and \textcolor{red}{x}. Note that in MathJax \color takes the text as an argument, unlike LaTeX.

  • Microsoft Word

    In Word select the text and use Home › Font Color; inside an equation the same button colors parts of the formula.

Already have the whole document?

Move complete papers between PDF, Word and LaTeX without retyping; every equation stays editable.

Single printed formulas are free: 3 a day without an account, 6 with a free account. New accounts get 30 credits to try handwriting, pages and documents; failed conversions return their credits. See plans and credit prices.

Text color in LaTeX: questions

How do I change text color in LaTeX?
Load xcolor and use \textcolor{red}{text}.
How do I use a hex color?
\definecolor{name}{HTML}{1F6FEB}, then \textcolor{name}{…}.
How do I highlight text?
\colorbox{yellow}{text}, or the soul package's \hl{text} for highlights that break across lines.

Have the formula as a picture?

Paste a screenshot or photo of the equation and get its LaTeX, ready to copy or open in the editor.

Convert an image to LaTeX