5

I want to define a new command via \newcommand{\endomorphism}[1]{\mathrm{End}\left(#1\right)}, but this does not work since a latex error tells me "Command \endomorphism already defined" (see MWE below). When I try \renewcommand{\endomorphism}[1]{\mathrm{End}\left(#1\right)} the error "Command \endomorphism undefined" is returned.

As a workaround I found that using the command name with a capital "e", i.e. \Endomorphism, works (again, see MWE). However, this is not desired.

What is the problem here? Can I somehow resolve it? I figured something might be clashing with the standard command \end but I am not proficient enough to figure that out myself :(. Thx.

\documentclass{article}

\usepackage[utf8]{inputenc}
\newcommand{\endomorphism}[1]{\mathrm{End}\left(#1\right)}
%\renewcommand{\endomorphism}[1]{\mathrm{End}\left(#1\right)}
\newcommand{\Endomorphism}[1]{\mathrm{End}\left(#1\right)}

\begin{document}
Let $\endomorphism{V} = \{\textrm{this does not work}\}$ and $\Endomorphism{V} = \{\textrm{linear mapping from $V$ to $V$}\}$.
\end{document}
4
  • 1
    The error-message is: ! LaTeX Error: Command \endomorphism already defined. Or name \end... illegal, see p.192 of the manual. The 2nd sentence is important. ;-) With a recent tex-distribution you can do \NewDocumentCommand{\endomorphism}{m}{\mathrm{End}\left(#1\right)}. Aug 9 at 21:06
  • On my system the following hack works: \newcommand{{{{ \endomorphism}}}}[1]{\mathrm{End}\left(#1\right)} (There must be a space between the 4-th curly opening brace and \endomorphism. ;-) Aug 9 at 21:29
  • Currently (August 2022) you cannot apply \newcommand but you can apply \renewcommand to commands whose name is "illegal". So how about: \csname @ifundefined\endcsname{endomorphism}{\def\endomorphism{}\csname @firstoftwo\endcsname\renewcommand}{}\newcommand{\endomorphism}[1]{\mathrm{End}\left(#1\right)} Aug 18 at 2:12
  • 1
    Would it be better to make the other question a duplicate of this one? This one has a MWE, and that one is using \input{settings.tex}.
    – Teepeemm
    Aug 18 at 13:37

1 Answer 1

10

You are not allowed to define a command whose name begins by \end because those names are used in the internal coding of the environments (when you define an environment foo, two commands are created : \foo and \endfoo).

However, if you actually want to define the command \endomorphism, you can by using the TeX command \def (the concept of environment is a concept of LaTeX and raw TeX knows nothing about environment and that's why \def does not forbide \endomorphism (of course, you should not define an environment called {omorphism})).

\documentclass{article}

\def\endomorphism#1{\mathrm{End}\left(#1\right)}

\begin{document}
Let $\endomorphism{V} = ...$
\end{document}

Remark

Defining a command whose output is \mathrm{End}\left(#1\right) is not recommended because the systematic use of \left( and \right) is discouraged: you will have extra small horizontal spaces on both sides of the parentheses... Many people recommend to only use \DeclareMathOperator.

4
  • 1
    If you go this route, it might be wise to leave a comment in your code about why you're not using \newcommand. Otherwise, you might come back to this in a few months and "clean up the source file" and end up back where you started. (small typo: raw TeX knows nothing about environment)
    – Teepeemm
    Aug 9 at 22:06
  • @Teepeemm: You are right. Aug 9 at 22:12
  • Thx for the quick reply and provided understanding of the situation. And also thanks for the additional remark!
    – Hannes
    Aug 10 at 7:47
  • \newenvironment is not needed for defining environments. If this was the case, "illegalizing" \end..-macros was not necessary as an environment's \end..-macro would be defined in any case and \newcommand's/\newenvironment's checking whether commands are already defined would be sufficient for catching up the situation. But if defining a command \endfoo and some time afterwards only defining a macro \foo "by hand" for future invocation via \begin-macro, then the carrying-out of the command \endfoo at the end of the foo-environment might form some kind of undesired behavior. Aug 18 at 2:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.