8

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}
Sverre
  • 20,729
Sarah
  • 113
  • Welcome to TeX.SE. I've edited your posting the tag "tikz-styles" as you've stated you want to accomplish the task without using a package -- tikz is a package. I've also deleted the 'color' tag. – Mico Apr 26 '15 at 22:57
  • What do you mean by “cross out”? Drawing a cross over it or just a horizontal line? – egreg Apr 26 '15 at 22:59
  • Without using a package? Then you'd just be implementing half of cancel.sty yourself?!?! – kahen Apr 26 '15 at 22:59
  • I'm new in latex and I have no idea how to use it. what does it mean please half of cancel.sty @kahen – Sarah Apr 26 '15 at 23:02
  • 1
    Please add a minimal working example that takes the form \documentclass{...}\usepackage{....}\begin{document}...\end{document}. If possible, it should compile and have the minimum amount of code needed to illustrate your problem. This makes it much easier for people to help you - and much more likely that they will! –  Apr 26 '15 at 23:06
  • drawing a cross over it please @egreg – Sarah Apr 26 '15 at 23:07
  • 2
    why do you not want to use a package, why do you want to implement this yourself, especially if you are new to latex? – David Carlisle Apr 26 '15 at 23:07
  • 1
    @ Andrew \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}

    – Sarah Apr 26 '15 at 23:09
  • 1
    @ David Carlisle I used the package cancel which cross over a word but usually it's used for mathematical equations, and the professor didn't accept it and he told me if you use a package so you're doing nothing – Sarah Apr 26 '15 at 23:12

2 Answers2

8

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

egreg
  • 1,121,712
3

Your Professor wants you to learn TeX, he is a wise man! Try the below code.

\documentclass{article}
\usepackage{xcolor}
    \makeatletter
    \newbox\@tempboxb
    \def\cancel#1{%
      \leavevmode
      \setbox\@tempboxa\hbox{#1}
      \setbox\@tempboxb\hbox{x}
       \hbox to 0pt{\hbox to \wd\@tempboxa {\color{red}\leaders\copy\@tempboxb\hfill\kern0pt}}#1}
    \makeatother
\begin{document}
This is \cancel{a very long word}
\end{document}

The approach is to put the words in a box and measure the width. Then draw a rule (using leaders, you can use any symbol such as x or -) over it. A package such as ulem will use more sophisticated macros to provide methods to adjust the cross lines, underline etc...

yannisl
  • 117,160