LaTeX error "Command \vec already defined": cause and fix
\newcommand, \newenvironment and \newcounter refuse to overwrite a name that exists. The second line of the message, "Or name \end... illegal", belongs to the same check: a new command name must not start with \end.
Why it happens
- LaTeX or a package uses the name. Short names are often taken:
\vec,\time,\box,\S,\d,\tand\divall exist in plain LaTeX. - The same
\newcommandline runs twice, for example because a file with definitions is loaded two times. \newenvironment{table}reports\table,\newcounter{section}reports\c@section, and\newtheorem{proof}withamsthmreports\proof: these commands create a command of that name internally.- The new name starts with
\end, such as\endresult.
How to fix it
- Pick another name, for example
\vectinstead of\vec. - Use
\renewcommandwhen you want to replace the existing command on purpose. Packages that call the old command then get your version too. - Use
\providecommandwhen the definition should only apply if the name is still free. - Remove the duplicate definition.
A document that produces the message
\documentclass{article}
\newcommand{\vec}[1]{\mathbf{#1}}
\begin{document}
The vector $\vec{v}$ has length 1.
\end{document}The corrected document
\documentclass{article}
\renewcommand{\vec}[1]{\mathbf{#1}}
\begin{document}
The vector $\vec{v}$ has length 1.
\end{document}Both documents were compiled with pdfLaTeX from TeX Live 2023. The first produces the message, the second compiles without an error.
Finding the line in the editor
The editor lists every error and warning of a compile run with file and line. Select an entry and the cursor jumps to that line. See Error list with jump to the line.