# Writing math formula in LaTeX, 'undefined control sequence'

I'm using the amsmath package and writing a report. I am using a LaTeX manual pdf, which has a chapter on writing maths formulas. I have followed what it says and I do not understand why my equation is causing an 'undefined control sequence' error

I'm trying to write this maths formula:

\begin{equation}
q_k(p)= f(x_k) + \nablaf(x_k)^Tp + \frac{1}{2}p^T\nabla{^2}f(x_k)p
\end{equation}


The equation comes out nicely, except for the nabla squared term, there is some brackets around the 2 for some reason. However, the bigger problem is I can not move on because it says I have created an 'unidentified control sequence' and I do not know why.

• You're not the first, and you won't be the last. Also \nabla{^2} is a bit wrong. Your {...} should go around the thing being superscripted, as \nabla^{2}. As it happens, if your superscript consists of one and only one element, e.g. \nabla^2 this will work, but \nabla^2i will give you ∇² i, what you need is \nabla^{2i}. It's a good habit to get into always putting {...} around the whole thing being superscripted Dec 18 '16 at 18:39
• Now, as long as your {...} are paired you can sort of put them around whatever you like, and they will be ignored, {a} = a, however it can really upset spacing and is properly used to isolate something that might not otherwise be isolated. You could also probably get some crazy unexpected results and definitely if you go x^{{a + b} foo bar baz} you will find the whole thing is superscripted. The superscripting will keep going until TeX has matched all of the {s with }s so don't use {...} frivolously. But that is why you can get away with \nabla{^2} Dec 18 '16 at 18:46
• As David Carlisle mentions, posting the entire document helps us quite a bit. In this case, that'd mean adding the \documentclass and begin/end{document} lines. It also helps to post the error that comes out. In this case, that error would put \nablaf at the beginning of a line, which would indicate that the problem. But TeX errors are notoriously vague, so learning to read them can be difficult. Dec 18 '16 at 19:44

You have

\nablaf


which is probably undefined, I guess you wanted

\nabla f


(Please always post a complete document that generates the error, it is much easier to debug then)

• I knew it was going to be something simple, it's fixed now. Ok, I shall do that in future! Thanks for your help! Dec 18 '16 at 18:40
• @cameron If this fixed your problem, please click the green checkmark at the left to indicate that the answer worked for you. Dec 18 '16 at 20:35
• The only code where \nablaf works for me is when MATLAB uses its TeX interpreter in text fields. Dec 19 '16 at 12:37
• @Crowley as I indicated it is impossible to say without a full example, \nablaf is not defined by default but if some package that you have not shown has \newcommand\nablaf{hello} then use of nablaf will typeset hello. Perhaps matlab has inserted such a definition (perhaps not to hello :-) Dec 19 '16 at 13:08
• @DavidCarlisle It was slightly offtopic note. In my humble opinion MATLAB's TeX interpreter searches his command database for \n, \na,... ,\nabla and \nablaf. Then it uses longest matching string as TeX command and add (another) space after last char. MATLAB's \nablaf works exactly like TeX's \nabla f and \nabla f create double space between symbol and an f... Using LaTeX interpreter is completely different story, and source of great pain to newbies. Dec 19 '16 at 15:55

A control sequence is a sequence of characters that has special meaning to LaTeX - in other words, it tells the program to do something more interesting than just putting the corresponding characters into your document. Typically, they are written as a backslash followed by some letters (though there are some exceptions). Control sequences have to be defined at some point so that the LaTeX compiler knows what they mean; for example,

\newcommand{\onehalf}{\frac{1}{2}}


is one way to define the control sequence \onehalf to represent the fraction 1/2.

When LaTeX complains about an "undefined control sequence", it means you have used a control sequence that never got defined. The compiler "helpfully" shows you the control sequence that was not defined in the error message; for example:

! Undefined control sequence.
l.4 q_k(p)= f(x_k) + \nablaf
(x_k)^Tp + \frac{1}{2}p^T\nabla{^2}f(x_k)p


Look at the next line following "Undefined control sequence." The l.4 means that the control sequence in question was found on line 4 of your .tex file, and the specific control sequence shows up at the very end of that line. In this case, it's \nablaf. So that is the thing you need to change. Depending on the specific circumstances, you might need to define the control sequence using something like \newcommand, or you might need to load a package that defines it, or you might just have made a typo, as is the case here.