[Tex/LaTex] How to cross out a word in LaTex

formatting

How can I cross out a word in a sentence in Latex without using a package?

MWE:

\documentclass{article}
\usepackage{cancel}
\begin{document}
%BeginDocument
The first \xcancel{three commands} work in text mode also i.e.,\xcancel{science}
%End Document
\end{document}

Best Answer

Your professor should know better. This teaches nothing about LaTeX, in my opinion; using packages is the way.

But here it is; it includes a (perhaps too clumsy) implementation of Euclid's algorithm for the greatest common divisor, in order to pass a correct pair of values to \line.

\documentclass{article}

\makeatletter
\newcommand{\crossout}[1]{%
  \begingroup
  \settowidth{\dimen@}{#1}%
  \setlength{\unitlength}{0.05\dimen@}%
  \settoheight{\dimen@}{#1}%
  \count@=\dimen@
  \divide\count@ by \unitlength
  \count0=20 \count4=\count@
  \loop
  \count2=\count0 % keep a copy
  \divide\count2\count4 \multiply\count2\count4
  \ifnum\count2<\count0
    \advance\count0 -\count2 % the remainder
    \count2=\count0
    \count0=\count4
    \count4=\count2
  \repeat
  \count0=20 \divide\count0\count4
  \count2=\count@ \divide\count2\count4
  \begin{picture}(0,0)
  \put(0,0){\line(\count0,\count2){20}}
  \put(0,\count@){\line(\count0,-\count2){20}}
  \end{picture}%
  #1%
  \endgroup
}
\makeatother

\begin{document}

This word is \crossout{crossed} out

\end{document}

enter image description here

Of course, presenting this solution would be cheating. And, no, it won't work in all cases, due to strict limitations on the pairs accepted by \line. Your professor surely knows that TeX doesn't draw oblique lines.

With the standard package pict2e it's easier and it will work in any case.

\documentclass{article}
\usepackage{pict2e}

\makeatletter
\newcommand{\crossout}[1]{%
  \begingroup
  \settowidth{\dimen@}{#1}%
  \setlength{\unitlength}{0.05\dimen@}%
  \settoheight{\dimen@}{#1}%
  \count@=\dimen@
  \divide\count@ by \unitlength
  \begin{picture}(0,0)
  \put(0,0){\line(20,\count@){20}}
  \put(0,\count@){\line(20,-\count@){20}}
  \end{picture}%
  #1%
  \endgroup
}
\makeatother

\begin{document}

This word is \crossout{crossed} out

\crossout{word}

\crossout{U}

\end{document}

enter image description here


A different solution (pdftex or luatex only)

\documentclass{article}

\makeatletter
\newcommand{\crossout}[1]{%
  \begingroup
  \sbox\z@{#1}%
  \dimen\z@=\wd\z@
  \dimen\tw@=\ht\z@
  \dimen\z@=.99626\dimen\z@   % get big points
  \dimen\tw@=.99626\dimen\tw@ % get big points
  \edef\co@wd{\strip@pt\dimen\z@}%  just the number
  \edef\co@ht{\strip@pt\dimen\tw@}% just the number
  \leavevmode
  \rlap{\pdfliteral{q 1 J 0.4 w 0 0 m \co@wd\space \co@ht\space l S Q}}%
  \rlap{\pdfliteral{q 1 J 0.4 w 0 \co@ht\space m \co@wd\space 0 l S Q}}%
  #1%
  \endgroup
}
\makeatother

\begin{document}

This is \crossout{crossed} out

\end{document}

enter image description here

Related Question