1

I tried to define a new math operator - essentially a math macro. I wanted to define an operator for the Eccentricity of a graph or network. The code seems to work when I generate a PDF, however I am seeing some errors in the .log file. Here is the actual code.

\DeclareMathOperator{\Eccentricity}{\textbf{Ecc}\,}
\renewcommand{\ecc}[1]{\Eccentricity(#1)}

The error I am getting is:

! LaTeX Error: \ecc undefined.

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

l.153 \renewcommand{\ecc}
                         [1]{\Eccentricity(#1)}
Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.

The message does not really indicate what the problem is. Can anyone suggest how to fix this error?

  • 4
    Why are you using \renewcommand instead of \newcommand? – Ulrike Fischer Dec 21 '18 at 18:32
  • Oh I think I did that because I copied something from another snippet. If there is no pre-existing command, then does \renewcommand fail? – krishnab Dec 21 '18 at 18:33
  • 2
    Yes it fails in that case. Alternative: \providecommand – Steven B. Segletes Dec 21 '18 at 18:33
  • Okay great, that worked. I did not realize that little pitfall. Thanks for the quick response everyone. – krishnab Dec 21 '18 at 18:35
3

\renewcommand checks whether the command is defined or not and generates an error if it's not defined. In this case, \ecc is not defined, so the message indicates exactly what the problem is.

You're most likely interested in \newcommand, which works for commands that have not been defined previously:

enter image description here

\documentclass{article}

\usepackage{amsmath}

\DeclareMathOperator{\Eccentricity}{\mathbf{Ecc}}
\newcommand{\ecc}[1]{\Eccentricity(#1)}

\begin{document}

$\ecc{G}$

\end{document}

If needed, you can adjust the spacing around \Ecc. However, in general, there's no need for it.

| improve this answer | |
  • Thanks so much. Yes, when I changed from \renewcommand, to \newcommand this fixed the errors. Thanks so much. – krishnab Dec 21 '18 at 18:36

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.