# LaTeX Error: \mathbb allowed only in math mode.

I'm writing a package which, among other things, gives abbreviations for \mathbb, as follows:

\newcommand{\A}{\mathbb A}
\def\B{\mathbb B}
\def\C{\mathbb C}
\newcommand{\D}{\mathbb D}
\newcommand{\E}{\mathbb E}
\newcommand{\F}{\mathbb F}
\def\G{\mathbb G}
\def\H{\mathbb H}
\newcommand{\I}{\mathbb I}
\newcommand{\J}{\mathbb J}
\newcommand{\K}{\mathbb K}
\def\L{\mathbb L}
\def\M{\mathbb M}
\newcommand{\N}{\mathbb N}
\def\O{\mathbb O}
\def\P{\mathbb P}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\def\S{\mathbb S}
\def\T{\mathbb T}
\def\U{\mathbb U}
\newcommand{\V}{\mathbb V}
\newcommand{\W}{\mathbb W}
\newcommand{\X}{\mathbb X}
\newcommand{\Y}{\mathbb Y}
\newcommand{\Z}{\mathbb Z}
\DeclareSymbolFont{bbold}{U}{bbold}{m}{n}
\DeclareSymbolFontAlphabet{\bb}{bbold}



./mworks.sty:844: LaTeX Error: \mathbb allowed only in math mode.

See LaTeX manual or LaTeX Companion for explanation.
Type G <return> for immediate help.
...

1.844 \newcommand{\I}{\mathbb
I}
?


Why does that happen?

-
I'm afraid it's not clear to me what your posting is about. Removing which braces, from which macros, is causing problems? Your screenshot seems to be related to the definition of \I -- which according to your setup is defined via \newcommand and not via \def; as such, it doesn't seem to be related to issues with \def. – Mico Apr 5 '14 at 10:35
@Mico you need to polish your crystal ball, the problem with \I is probably caused by mis-using \def on the line above:-) (see my answer) – David Carlisle Apr 5 '14 at 10:44
@DavidCarlisle - My crystal ball has been showing me nothing but dense fog of late. Congrats on figuring out what the cause of the reported problem was -- where do get your crystal ball polished? :-) – Mico Apr 5 '14 at 11:49
Congrats: You have essentially defined a new math language: Not LaTeX but MickGTeX. – Martin Schröder Apr 7 '14 at 10:48

## 2 Answers

You have probably discovered why it's very strongly discouraged to redefine kernel commands with \def.

For instance, \H is defined to give the “hungarian umlaut” accent; so, if you are talking about Erdős in your document, you'll get a puzzling error, even if you seem not to be using \H. Actually when \usepackage[utf8]{inputenc} is in force, LaTeX translates Erdős into Erd\H{o}s. Can you see the problem?

Don't redefine kernel commands unless you know precisely what you're doing. If \newcommand can't be used, you must check what the command means and, if it turns out to be a command for typesetting accents or similar things, don't redefine it.

It's also particularly bad is adding those commands in a package, even if it's for personal usage. If you pass it to your buddies, they'll start to use it and maybe include it in something they submit elsewhere. As Barbara Beeton observes in a comment,

if a package like this gets submitted with a manuscript to a publisher, it can become very expensive to correct, and may result in rejection of the manuscript.

I should add that she is a great expert (the expert, perhaps) in copy editing for journals and books at the AMS.

You can try saying

\def\box#1{\fbox{#1}}


and see what happens.

Of course, the syntax

\def{\A}{\mathbb A}


is invalid: while \newcommand{\A}{...} is good, no brace can follow \def. Irremediably wrong is also

\def\B \mathbb B


which makes really no sense. By the way, the preferred syntax would be \mathbb{B} with braces that clearly delimit what \mathbb is applied to.

-
you should add that it is especially bad to redefine kernel commands in a package, since packages are often passed on to other people who use them blindly, and they may not be able to figure out what has gone wrong. if a package like this gets submitted with a manuscript to a publisher, it can become very expensive to correct, and may result in rejection of the manuscript. – barbara beeton Apr 5 '14 at 11:21

As the posted code doesn't generate the error shown, I'll modify it so it does.

This complete document demonstrates the error.

\RequirePackage{amsfonts}
\newcommand{\A}{\mathbb A}
\def\B{\mathbb B}
\def\C{\mathbb C}
\newcommand{\D}{\mathbb D}
\newcommand{\E}{\mathbb E}
\newcommand{\F}{\mathbb F}
\def\G{\mathbb G}
\def\H \mathbb H
\newcommand{\I}{\mathbb I}

\stop


it produces

\$ pdflatex er11
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
restricted \write18 enabled.
entering extended mode
(./er11.tex
LaTeX2e <2011/06/27>
Babel <3.9j> and hyphenation patterns for 54 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/amsfonts/amsfonts.sty)

! LaTeX Error: \mathbb allowed only in math mode.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.10 \newcommand{\I}{\mathbb
I}
?


the immediate fix is to make it like the code posted in the question, or as egreg notes, better don't use one-letter commands and over-write LaTeX internals.

-