5

I'm new to latex so please be gentle.

I installed cancel using this command:

tlmgr install cancel

Then I created a test file called test.tex with the following content:

\documentclass[12pt]{article}                                                    
\usepackage{cancel}                                                              

\begin{document}                                                                 
\cancel{test}                                                                    
\end{document}                                                                   

When I run pdflatex test.tex, I get the following output:

This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2015/01/01>
Babel <3.9l> and hyphenation patterns for 2 languages loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo))
(/home/quant/texmf/tex/latex/cancel/cancel.sty)
No file test.aux.
! You can't use `\/' in vertical mode.
\@cancel ...ht \unitlength \p@ \canc@thinlines {\/
                                                  \raise \dimen@ \hbox {\ooa...
l.5 \cancel{test}

However, when I replace \cancel{test} with something like some text \cancel{test} it compiles fine. How can I get it to compile with the striked out content at the beginning of the line?

  • 1
    \mbox{}\cancel{test} when it's at the beginning of a paragraph. – egreg Apr 6 '16 at 22:55
  • 1
    @egreg thanks, that works! Could you explain why this is necessary? Post it as an answer and I'll mark it as the answer. – quant Apr 6 '16 at 22:57
  • Between paragraphs or at the beginning of the document TeX operates in "vertical mode", sorting boxes (e.g. paragraphs) underneath each other. It says in the error message that you can't use \/ in vertical mode. That \/occurs within the macro \cancel, which obviously does not check if TeX at present is in vertical mode, where \/ doesn't work, or in horizontal mode, where it does. egreg's \mbox{} just begins a new paragraph with an empty box, TeX obediently switches into horizontal mode and \/is happy. – Blackface Apr 7 '16 at 7:52
6

This is what I consider a small bug in cancel. The definition of \cancel reads

\DeclareRobustCommand\cancel[1]{\ifmmode
  \mathpalette{\@cancel{\@can@slash{}}}{#1}\else 
  \@cancel{\@can@slash{}}\hbox{#1}\fi}

but it should be

\DeclareRobustCommand\cancel[1]{\ifmmode
  \mathpalette{\@cancel{\@can@slash{}}}{#1}\else 
  \leavevmode\@cancel{\@can@slash{}}\hbox{#1}\fi}

The problem is that when \@cancel ends its working, it issues an \hbox command for printing the result, but \hbox doesn't start paragraph mode. With \leavevmode this problem is avoided.

You can use \mbox{}\cancel{test} when the cancelled word starts a paragraph or patch the faulty command:

\documentclass[12pt]{article}
\usepackage{cancel,xpatch}

\makeatletter
\xpatchcmd{\cancel}{\else\@cancel}{\else\leavevmode\@cancel}{}{}
\makeatother

\begin{document}

\cancel{test}

\end{document}

Instead of the patch with \xpatchcmd you can state the fixed definition in full:

\documentclass[12pt]{article}
\usepackage{cancel}

\makeatletter
\DeclareRobustCommand\cancel[1]{\ifmmode
  \mathpalette{\@cancel{\@can@slash{}}}{#1}\else 
  \leavevmode\@cancel{\@can@slash{}}\hbox{#1}\fi}
\makeatother

\begin{document}

\cancel{test}

\end{document}

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.