3

I have atm1.tex which used to compile ok. It hasn't changed since last time I compiled it, but its base classes have changed:

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

How can I find where \text was already defined?

atm1.tex contains:

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

atm.cls contains:

\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 & &\\}
...

and simdoc.cls contains:

\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}
...

Clearly \text is defined in atm1.cls, and that's the only one I expected would exist. I suspect someone added a new \RequirePackage to simdoc.cls which introduces a new definition for \text. I could rename \text to something else, but I have hundreds of documents which inherit from this class (all broken) and thousands of uses of \text, so I'd rather not change all of them.

I tried changing \newcommand{\text} to \providecommand{\text}. It compiles, but it doesn't seem to use atm.cls's definition of \text (instead of a \text{Hello} taking up a line in the table, it overlaps the next line of the table).

I also tried: \let\text\undefined\newcommand{\text}.... This works, but is it safe? I'm scared about unintended consequences.

If the above idea is not safe, then my question is "how do I know which \RequirePackage defined \text?". Knowing this will help me search for alternatives.

7
  • 3
    \text is defined in amsmath, I'd strongly recommend not to redefine it.
    – user242026
    Commented May 17, 2021 at 6:55
  • Did you know \text was defined in amsmath by memory, or did you figure this out so quickly with a command or search?
    – Stewart
    Commented May 17, 2021 at 6:57
  • By memory. It is a widely used command. Searching in packages is more cumbersome.
    – user242026
    Commented May 17, 2021 at 6:59
  • 2
    Never redefine macros unless you know exactly what they do and what they are used for. Choose a different name. The classic example is \span, hardly anyone knows what it is used for, so it would be obvious to redefine it to be \operatorname{span} if you're writing about linear algebra. If you do that tables, matrices, align, etc no longer works. \text has been in amstext which is loaded by amsmath for many many years. The only way this has been able to compile is if amsmath has never been used.
    – daleif
    Commented May 17, 2021 at 7:06
  • apart from the question that \text is already defined in amsmath, your definition doesn't look like something that a class should provide. A command to create an empty line in a tabular with four columns is much to specific. Commented May 17, 2021 at 7:20

2 Answers 2

3

Dangerous bend sign

Since your \text is only used inside the checklist environment, you could redefine it from the amsmath version within that environment.

In atm.cls:

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

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

The \let control sequence will override LaTeX's redefinition prevention mechanisms. Because each environment is contained in its own TeX group, once the environment ends, \text will snap back to its amsmath definition.

The drawback is that you will not be able to use the amsmath version of \text inside the checklist environment. You could give it another name with the same \let pattern, but that won't be enough if some other command uses it indirectly.

1
  • I really like this answer, because it's solves the core of my problem. If I want to keep the simplicity that comes with a 4-letter command, changing the names won't alleviate risk of another name clash. Indeed the checklist environment is very restrictive and so this scope-limited override is perfect!
    – Stewart
    Commented May 19, 2021 at 20:50
6

\text is defined by amsmath (or rather by the amstext package that is included by amsmath). You will find thousands of examples on this site and defining it with a separate definintion will break most mathematical documents as they will almost all include amsmath

However to answer the question of how to find the package that defines a command, you can ask latex to show the current definition, or search the sources.

For the first, the document

\documentclass{article}

\usepackage{graphicx}

\usepackage{color}

\usepackage{amsmath}

\usepackage{hyperref}

\newcommand\text{zzz}

\begin{document}


\end{document}

Shows an error on the terminal:

! 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}

But if you modify it it:

\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}

Then the terminal output is

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}
?

Which clearly shows \text is undefined until amsmath is loaded.

2
  • Thanks! This is a good answer to this problem which I am sure to run into again (probably not with \text next time).
    – Stewart
    Commented May 17, 2021 at 7:30
  • 2
    FYI, The definition of \text came from \RequirePackage[most]{tcolorbox} which must import amsmath. We never included amsmath directly. This answer definitely helped me to figure that out.
    – Stewart
    Commented May 17, 2021 at 8:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .