命令已定义。如何找到错误的定义?

命令已定义。如何找到错误的定义?

atm1.tex曾经编译过。自上次编译以来,它没有改变,但它的基类已经改变:

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

我如何找到\text已经定义的地方?

atm1.tex包含:

\documentclass[a4paper]{atm}
...
\begin{checklist} % Makes a table where we can add \text, \result, or \action lines
  \text{This line contains text}
  ...

atm.cls包含:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{atm}[acceptance test manual template]

\LoadClass{simdoc}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{simdoc}}
\ProcessOptions\relax

\RequirePackage{textcomp} % Allows use of degree symbol
\RequirePackage[clock]{ifsym} % Brings in \StopWatchStart
...
\newcommand{\text}[1]{& #1 & &\\}
...

simdoc.cls包含:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{simdoc}[Sim International document template]

\LoadClass{report}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{report}}
\ProcessOptions\relax

\RequirePackage[margin=3cm]{geometry}
\RequirePackage{longtable,array}
\RequirePackage{titlesec}
\RequirePackage[dvipsnames]{xcolor}
\RequirePackage{colortbl}
\RequirePackage{graphicx}
\RequirePackage{fancyhdr}
\RequirePackage{etoolbox}
\RequirePackage[hidelinks]{hyperref}
\RequirePackage{textcomp, booktabs}
\RequirePackage{url}
\RequirePackage{xparse}
\RequirePackage{caption}
\RequirePackage{xcolor}
\RequirePackage{soul}
\RequirePackage [most]{tcolorbox}
\tcbuselibrary{skins}
\usepackage[toc,page]{appendix}
...

显然,\text在 中已定义atm1.cls,而这是我唯一期望存在的。我怀疑有人添加了 new \RequirePackagesimdoc.cls从而为 引入了新的定义\text。我可以将其重命名\text为其他名称,但我有数百个文档继承自此类(全部损坏)并且 有数千个使用\text,因此我宁愿不更改所有这些文档。

我尝试将其更改\newcommand{\text}\providecommand{\text}。它可以编译,但似乎没有使用atm.cls的定义\text(它不是\text{Hello}占用表中的一行,而是与表的下一行重叠)。

我也尝试过:\let\text\undefined\newcommand{\text}...。这个方法有效,但是安全吗?我担心会出现意想不到的后果。

如果上述想法不安全,那么我的问题是“我怎么知道哪个\RequirePackage定义的\text?”。了解这一点将有助于我寻找替代方案。

答案1

危险弯道标志

由于你的\text只在checklist环境内部使用,所以你可以amsmath从该环境内的版本重新定义它。

atm.cls

\newcommand{\atm@text}[1]{& #1 & &\\}

\newenvironment{checklist}{%
   \let\text\atm@text%
   % rest of environment begin code
}{% environment end code }

控制\let序列将覆盖 LaTeX 的重定义预防机制。由于每个环境都包含在其自己的 TeX 组中,因此一旦环境结束,\text就会恢复到其amsmath定义。

缺点是您将无法在环境中使用该amsmath版本。您可以用相同的模式给它取另一个名字,但如果其他命令间接使用它,那就不够了。\textchecklist\let

答案2

\text由 定义amsmath(或者更确切地说是amstext由 包含的包定义amsmath)。您将在此站点上找到数千个示例,并且使用单独的定义来定义它会破坏大多数数学文档,因为它们几乎都包含amsmath

但是要回答如何找到定义命令的包的问题,​​您可以让 latex 显示当前定义,或者搜索源代码。

首先,该文件

\documentclass{article}

\usepackage{graphicx}

\usepackage{color}

\usepackage{amsmath}

\usepackage{hyperref}

\newcommand\text{zzz}

\begin{document}


\end{document}

终端上显示错误:

! LaTeX Error: Command \text 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.11 \newcommand\text{zzz}

但如果你修改它:

\show\text
\documentclass{article}
\show\text
\usepackage{graphicx}
\show\text
\usepackage{color}
\show\text
\usepackage{amsmath}
\show\text
\usepackage{hyperref}
\show\text
\newcommand\text{zzz}

\begin{document}


\end{document}

然后终端输出是

This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./aa274.tex
LaTeX2e <2020-10-01> patch level 4
L3 programming layer <2021-05-07>
> \text=undefined.
l.1 \show\text
              
? 
(/usr/local/texlive/2021/texmf-dist/tex/latex/base/article.cls
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
(/usr/local/texlive/2021/texmf-dist/tex/latex/base/size10.clo))
> \text=undefined.
l.3 \show\text
              
? 
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics/graphicx.sty
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics/graphics.sty
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics/trig.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics-cfg/graphics.cfg)
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics-def/pdftex.def)))
> \text=undefined.
l.5 \show\text
              
?
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics/color.sty
(/usr/local/texlive/2021/texmf-dist/tex/latex/graphics-cfg/color.cfg))
> \text=undefined.
l.7 \show\text
              
? 
(/usr/local/texlive/2021/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/local/texlive/2021/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/local/texlive/2021/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/local/texlive/2021/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/amsmath/amsopn.sty))
> \text=macro:
->\protect \text  .
l.9 \show\text
              
? 
(/usr/local/texlive/2021/texmf-dist/tex/latex/hyperref/hyperref.sty
(/usr/local/texlive/2021/texmf-dist/tex/generic/ltxcmds/ltxcmds.sty)
(/usr/local/texlive/2021/texmf-dist/tex/generic/iftex/iftex.sty)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pdftexcmds/pdftexcmds.sty
(/usr/local/texlive/2021/texmf-dist/tex/generic/infwarerr/infwarerr.sty))
(/usr/local/texlive/2021/texmf-dist/tex/generic/kvsetkeys/kvsetkeys.sty)
(/home/davidc/texmf/tex/generic/kvdefinekeys/kvdefinekeys.sty)
(/usr/local/texlive/2021/texmf-dist/tex/generic/pdfescape/pdfescape.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/hycolor/hycolor.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/letltxmacro/letltxmacro.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/auxhook/auxhook.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/kvoptions/kvoptions.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/hyperref/pd1enc.def)
(/usr/local/texlive/2021/texmf-dist/tex/latex/hyperref/hyperref-langpatches.def
) (/usr/local/texlive/2021/texmf-dist/tex/generic/intcalc/intcalc.sty)
(/usr/local/texlive/2021/texmf-dist/tex/generic/etexcmds/etexcmds.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/hyperref/puenc.def)
(/usr/local/texlive/2021/texmf-dist/tex/latex/url/url.sty)
(/usr/local/texlive/2021/texmf-dist/tex/generic/bitset/bitset.sty
(/usr/local/texlive/2021/texmf-dist/tex/generic/bigintcalc/bigintcalc.sty))
(/usr/local/texlive/2021/texmf-dist/tex/latex/base/atbegshi-ltx.sty))
(/usr/local/texlive/2021/texmf-dist/tex/latex/hyperref/hpdftex.def
(/usr/local/texlive/2021/texmf-dist/tex/latex/base/atveryend-ltx.sty)
(/usr/local/texlive/2021/texmf-dist/tex/latex/rerunfilecheck/rerunfilecheck.sty

(/usr/local/texlive/2021/texmf-dist/tex/generic/uniquecounter/uniquecounter.sty
)))
> \text=macro:
->\protect \text  .
l.11 \show\text
               
? 

! LaTeX Error: Command \text 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.12 \newcommand\text{zzz}
?

这清楚地表明,直到加载\text时才定义。amsmath

相关内容