[Tex/LaTex] Undefined control sequence on legitimate LyX macro

errorslyxmacros

I'm trying to define a LyX macro, Insert -> Math -> Macro on the menus. It's a very simple macro. Source code under View -> View Source:

\global\long\def\Z{\mathbb{Z}}

The file compiles nicely if I don't use the macro. However, once I put a \Z inside a Math formula and try to compile, it gives me an Undefined control sequence with the following error message:

! Undefined control sequence.
\Z ->\mathbb 
             {Z}
l.32 d$\Z
         $
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.

Now, the most peculiar thing is that I have used this exact same macro on other files, where it works perfectly.

In case it might be of any help, I'm using the article document class on LyX 2.0.0. Could it be that the document encoding or document class make a difference? Or maybe the document language setting?

A complete log of the LaTeX errors is available at this link.

Thanks!

Best Answer

As egreg mentions in a comment, the reason you get the error is that neither the amsfonts nor amssymb package, where \mathbb is defined, is loaded. To force LyX to add one of these you can do one of two things:

  1. Go to Document --> Settings --> LaTeX preamble and add there either

    \usepackage{amssymb}
    

    or

    \usepackage{amsfonts}
    
  2. Go to Document --> Settings --> Math options. Uncheck Use AMS math package automatically and check Use AMS math package, as in the screenshot below. LyX will now have added

    \usepackage{amsmath}
    \usepackage{amssymb}
    

    to the document preamble.

    The first of these, the automatic one, adds these packages only if you use some of their features through the LyX GUI. E.g. if you do Insert --> Math --> AMS Align, the amsmath package will be added, and if you e.g. add some text with Blackboard bold via the math font button on the toolbar -- enter image description here -- then amssymb will be added.

    enter image description here