0

I defined a certain command, but now I want to change its definition. I tried to simply change it, but it doesn't compile.

What should I do?

Thank you very much.

EDIT

The former command was

\newcommand{\dt}{\frac{\partial \gamma}{\partial t}}

Then I wanted to change it to

\newcommand{\dt}{\dot{\gamma}}

but it didn't work. Now I tried with

\renewcommand{\dt}{\dot{\gamma}}

but I got this message:

LaTeX Error: \dt undefined.

6
  • 1
    Using \renewcommand instead of \newcommand should work, but we don't see any code here. A \def\.... should always work, apart from syntax or other errors – user31729 Apr 14 '16 at 18:01
  • Perhaps \renewcommand solves your problem. Please include a minimal working example.showing what you have and what you want if you still need help. – Ethan Bolker Apr 14 '16 at 18:01
  • 1
    Please be specific as what you mean by "simply chang[ing]" a command. – Mico Apr 14 '16 at 18:04
  • Without an example of “before the cure and after the cure” it's difficult to say. – egreg Apr 14 '16 at 18:06
  • 2
    could you provide a (minimal) example document where the error occurs? For me, it's working: \documentclass{article} \newcommand{\dt}{\frac{\partial \gamma}{\partial t}} \begin{document} $\dt$\renewcommand{\dt}{\dot{\gamma}}$\dt$ \end{document} – riddleculous Apr 14 '16 at 18:25
3

There are several methods:

  1. Fresh definition.

    \newcommand{\dt}{\frac{\partial \gamma}{\partial t}}
    
  2. LaTeX's \renewcommand requires that the command exists.

    \renewcommand{\dt}{\dot{\gamma}}
    
  3. If it is unclear, whether the command is defined, then \providecommand can be used to ensure that the macro is defined.

    \providecommand{\dt}{}
    \renewcommand{\dt}{\frac{a}{b}}
    
  4. When the macro is assigned to a undefined macro or \relax, then \newcommand thinks that the macro is undefined and does not complain.

    \let\dt\relax
    \newcommand{\dt}{\mathop{}\!\mathrm{d}t}
    
  5. The TeX primitive \def does not care for a previous definition, it defines and overwrites a previously defined macro.

    \def\dt{d^t}
    

Full example:

\documentclass[a5paper]{article}
\begin{document}

% 1
\newcommand{\dt}{\frac{\partial \gamma}{\partial t}}
\begin{equation}\dt\end{equation}

% 2
\renewcommand{\dt}{\dot{\gamma}}
\begin{equation}\dt\end{equation}

% 3
\providecommand{\dt}{}
\renewcommand{\dt}{\frac{a}{b}}
\begin{equation}\dt\end{equation}

% 4
\let\dt\relax
\newcommand{\dt}{\mathop{}\!\mathrm{d}t}
\begin{equation}\dt\end{equation}

% 5
\def\dt{d^t}
\begin{equation}\dt\end{equation}

\end{document}

Result

6
  • Out of 3-5, is there any way preferrable / "nicer" than the others? I have the feeling that the later ways get "dirtier", is that right? – riddleculous Apr 14 '16 at 18:32
  • Thank you very much! But actually I solved my problem just adding two extra brackets, i.e. \newcommand{\dt}{{\dot{\gamma}}} Apparently without those extra brackets I had problems when writing something like $ \nabla_\dt$ – Onil90 Apr 14 '16 at 18:47
  • 2
    @riddleculous 3 is "pure LaTeX", 5 uses the underlying TeX primitive directly and is shorter to write. It is a matter of taste, which style you prefer. – Heiko Oberdiek Apr 14 '16 at 18:59
  • 2
    @Onil90: this is why you should provide a full example where the error occurred. This is a standard mistake that could have been easily spotted. – riddleculous Apr 14 '16 at 19:01
  • @Onil90 Yes, the additional curly braces makes a subformula, which can be used in subscripts and superscripts. In this case is the horizontal spacing not affected. A subformula is set as \mathord in the same way as the "ordinary" \dot{\gamma}. – Heiko Oberdiek Apr 14 '16 at 19:01

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.