# Problem with \newcommand and \text

I'm having a problem with \newcommand. Below is my LyX document where the first line comes from the "Insert-Latex" command and the second line is using the "Insert-Math-Inline Formula"

 \newcommand{\kon}{k_\text{on}}
\kon


When I compile the document using "View PDF" I get this error

 Undefined control sequence
Description:
$\kon$
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., \hobx'), type I' and the correct
spelling (e.g., I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.


The weirdest part is that I've used this exact macro in other LyX documents and it works fine. If I remove the \text part, it also works fine. What's going on?

• Do you have Document > Settings... > Math Options > amsmath set to "load automatically" or "load always"? – Werner Sep 1 '16 at 19:37
• It is preferable to place the definitions like \newcommand{\kon}{k_\text{on}} in your Document > Settings... > LaTeX Preamble rather than mid-document (using Insert > TeX Code). – Werner Sep 1 '16 at 19:42
• I think one problem might be not wrapping the \text part in braces: I'd expect that to result in TeX grabbing \text as the only thing in the subscript and then complaining about argument of \text has an extra }. That naturally does not explain the undefined control sequence though… – MickG Sep 1 '16 at 19:42
• @AboAmmar -- the \newcommand statement is okay, as long as it is in the preamble. it's invoked in math mode properly. what isn't clear is the complete content of the error message; the context is lost, and that contains information that is essential to the proper identification of what's happening here. – barbara beeton Sep 1 '16 at 20:08

You need amsmath to be loaded in order to use \text. And the default setting for LyX is to "load [it] automatically". However, this automated loading requires you to use some amsmath-related environment (see Insert > AMS *), and unfortunately defining \text in a \newcommand doesn't invoke this loading.

You can change this choice so that amsmath is "load[ed] always" via Document > Settings... > Math Options > amsmath > "load always":

Note that LaTeX, like most programming languages, provides some form of scope to its definitions. So, if you Insert > TeX Code somewhere inside a table (just as an example) with the code being \newcommand{\kon}{k_\text{on}}, that macro - \kon - will only have a limited scope within the table environment. That's why it is usually best to insert definitions inside the Document > Settings... > LaTeX Preamble. Alternatively, you can always create your own definitions in a separate .style or .inc file that you include.

From the error message, I assume you forgot to add \usepackage{amsmath} or its extended \usepackage{mathtools} in the preamble. Using the \text{ .. } command requires either of these packages.

A MWE:

\documentclass[12pt]{article}
\usepackage{mathtools}
\newcommand{\kon}{k_\text{on}}
\begin{document}

$\kon$

\end{document}


But, when removing \usepackage{mathtools}, the following error message appears (which is the same as yours):

\kon ->k_\text
{on}
l.6 $\kon$
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., \hobx'), type I' and the correct
spelling (e.g., I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.