无法让 cancel.sty 正常运行

无法让 cancel.sty 正常运行

我是 Latex 的新手。我使用 LibreOffice 中的 TexMaths 公式编辑器。

我在 MiKTex 包管理器中安装了取消包。我不确定它安装在哪里,也不知道它是否安装正确。我所能看到的是,当我在包管理器中单击包时,它会显示:

Category: Formats\LaTex\LaTex contrib

File name:          Path:
----------          ------------
cancel.sty:         texmf\tex\latex\cancel
cancel.pdf:         texmf\doc\latex\cancel
cancel-src.tar.bz2: texmf\source

我刚刚做了一个简单的测试:

\usepackage{cancel}
\cancel{b}

我收到以下错误:

This is pdfTeX, Version 3.14159265-2.6-1.40.17 (MiKTeX 2.9)
entering extended mode
(tmpfile.tex
LaTeX2e <2016/03/31> patch level 1
...

("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\amsfonts\umsb.fd") (cancel.sty
! Missing $ inserted.
<inserted text> 
                $
l.53 


! LaTeX Error: Command \@ already defined.
               Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.80 \newcommand\@c
                   ancel[3]{%
! Undefined control sequence.
l.81 \OriginalPictureCmds
                         \@begin@tempboxa\hbox{\m@th$#2{#3}$}%
! Undefined control sequence.
...

l.182 \@i
         fundefined{OriginalPictureCmds}{\let\OriginalPictureCmds\relax}{}
)
! TeX capacity exceeded, sorry [input stack size=5000].
\cancel.sty-h@@k ->\cancel.sty-h@@k 
                                    \let \@unprocessedoptions \relax 
l.24 \cancel
            {b}
No pages of output.
Transcript written on tmpfile.log.

@JPi:如果我输入以下代码:

\documentclass{article}
\usepackage{amsmath}
\usepackage{cancel}
\newcommand{\tcancel}[1]{\leavevmode\cancel{\text{#1}}}


\begin{document}

\tcancel{x}
$\cancel{x}$ \tcancel{x}
\end{document}

它给出了类似的错误:

! LaTeX Error: Two \documentclass or \documentstyle commands.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.23 {$\displaystyle \documentclass{
                                    article}
(cancel.sty
! Missing $ inserted.
<inserted text> 
                $
l.53 


! LaTeX Error: Command \@ already defined.
               Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.80 \newcommand\@c
                   ancel[3]{%
! Undefined control sequence.
l.81 \OriginalPictureCmds
                         \@begin@tempboxa\hbox{\m@th$#2{#3}$}%
! Undefined control sequence.
l.81 \OriginalPictureCmds\@begin@tempboxa\hbox{\m
                                                 @th$#2{#3}$}%
! You can't use `macro parameter character #' in math mode.
<recently read> ##

<more messages omitted>

答案1

这似乎是cancel软件包中的一个错误。其文档称它在文本模式下也能工作,但事实似乎并非如此。有一个简单的解决方法:

\documentclass{article}
\usepackage{amsmath}
\usepackage{cancel}
\newcommand{\tcancel}[1]{\ensuremath{\cancel{\text{#1}}}}


\begin{document}

\tcancel{x}
$\cancel{x}$
\end{document}

* 编辑 *

回应@campa 的评论,

\documentclass{article}
\usepackage{cancel}
\newcommand{\tcancel}[1]{\leavevmode\cancel{#1}}


\begin{document}

\tcancel{x}
$\cancel{x}$ \tcancel{x}
\end{document}

相关内容