Cancel terms in an equation in LaTeX (cancel)

The cancel package strikes through a part of a formula: \cancel{x} draws a diagonal line through its argument. \cancelto{0}{x} draws an arrow that points to a value.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{cancel}

Strike through a term

\cancel{...} draws a line from the bottom left to the top right of its argument.

\[
  \frac{\cancel{3} \cdot 5}{\cancel{3} \cdot 7} = \frac{5}{7}
  \qquad
  \cancel{x} + y - \cancel{x} = y
\]
Change the formula

The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.

3⋅53⋅7=57x+y−x=y\frac{\cancel{3} \cdot 5}{\cancel{3} \cdot 7} = \frac{5}{7} \qquad \cancel{x} + y - \cancel{x} = y

Open this formula in the editor

Other directions

\bcancel{...} draws the line from the top left to the bottom right, and \xcancel{...} draws both lines as a cross.

\[
  \bcancel{a + b}
  \qquad
  \xcancel{a + b}
\]
Change the formula

The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.

a+ba+b\bcancel{a + b} \qquad \xcancel{a + b}

Open this formula in the editor

Arrow to a value

\cancelto{value}{term} strikes through the term with an arrow and prints the value at its tip. It works only in math mode.

\[
  f(x) = x^2 + \cancelto{0}{\frac{1}{x}}
\]
Result
The compiled example

Open in the editor

Coloured lines

Redefine \CancelColor to colour the line and leave the term black. The colour command needs the xcolor package.

Complete document:

\documentclass{article}
\usepackage{xcolor}
\usepackage{cancel}
\renewcommand{\CancelColor}{\color{red}}
\begin{document}
\[
  \frac{\cancel{a} b}{\cancel{a} c} = \frac{b}{c}
\]
\end{document}
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages