5

For example, suppose I had an equation

x = 2 + 1 - 2 - 1 (obviously toy example)

I want the +2 to cancel out with the -2, +1 to cancel out with the -1

So in Latex I would want something like

enter image description here

Is there a simple way to implement multi-colored strike through or cancel in Latex?

enter image description here

A similar question is found here but it is for global coloring crossing out using different colour

1 Answer 1

7

One possibility is to use cancel. (EDIT: Replaced \textcolor by color, big thanks to Werner!)

\documentclass{article}
\usepackage{cancel,xcolor}
\newcommand{\Cancel}[2][black]{{\color{#1}\cancel{\color{black}#2}}}
\begin{document}
\[x~=~\Cancel[blue]{2}+\Cancel[red]{1}-
 \Cancel[blue]{2}-\Cancel[red]{1}\]
\end{document}

enter image description here

Note, however, that an ambient color will be without effect, i.e. instead of {\color{green}\Cancel[blue]{2}+\Cancel[red]{1}} you'd need to do \Cancel[blue]{\color{green} 2}+\Cancel[red]{\color{green} 1} unfortunately. I guess that there will be better ways and am looking forward to learn new tricks.

EDIT: Just for fun: a TikZ version. The option X will do an \xcancel sort of cross out.

\documentclass{article}
\usepackage{tikz}
\newif\ifCancelX
\tikzset{X/.code={\CancelXtrue}}
\newcommand{\Cancel}[2][]{\relax
\ifmmode%
  \tikz[baseline=(X.base),inner sep=0pt] {\node (X) {$#2$};
  \tikzset{#1}
  \draw[#1,overlay,shorten >=-2pt,shorten <=-2pt] (X.south west) -- (X.north east);
  \ifCancelX
    \draw[#1,overlay,shorten >=-2pt,shorten <=-2pt] (X.north west) -- (X.south east);   
  \fi}
\else
  \tikz[baseline=(X.base),inner sep=0pt] {\node (X) {#2};
  \tikzset{#1}
  \draw[#1,overlay,shorten >=-2pt,shorten <=-2pt] (X.south west) -- (X.north east);
  \ifCancelX
    \draw[#1,overlay,shorten >=-2pt,shorten <=-2pt] (X.north west) -- (X.south east);
  \fi}%
\fi}

\begin{document}
\[x~=~\Cancel[blue,X]{2}+\Cancel[red]{\frac{1}{2}}-
 \Cancel[blue]{2}-\Cancel[red, line width=1pt]{\frac{1}{2}}\]
\end{document}

enter image description here

6
  • I never knew you could use color with cancel ...
    – Fraïssé
    Aug 13, 2018 at 22:17
  • @EnlightenedOne both start with c ... ;-) (Note that of course an ambient color will be without effect, i.e. instead of \textcolor{green}{\Cancel[blue]{2}+\Cancel[red]{1} you'd need to do \Cancel[blue]{\textcolor{green}{2}}+\Cancel[red]{\textcolor{green}{1}} unfortunately. )
    – user121799
    Aug 13, 2018 at 22:20
  • 1
    Don't use \textcolor{<color>}{<stuff>}, rather use {\color{<color}<stuff>}. This translates your implementation to work within math and text mode, rather than currently just in text mode. Try with \Cancel[blue]{\frac{1}{2}}...
    – Werner
    Aug 13, 2018 at 22:25
  • @Werner Thank you! I did not know that. Do you want to post an answer and I delete mine?
    – user121799
    Aug 13, 2018 at 22:31
  • 1
    @pzorba75 I added a TikZ version that accepts an X in the options. I don't know to do it otherwise in one command. Of course, your suggestion works and is much more minimal than loading TikZ.
    – user121799
    Aug 14, 2018 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.