[Tex/LaTex] LaTeX Error: \mathbb allowed only in math mode.

amsmathmath-modepackage-writing

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}

The reason for the \def\newcommand oscillation is that I have seen some of those commands are not always defined, so choosing \newcommand would give problems in some cases while \renewcommand would in others, while \def bypasses this by not checking if the commands are defined or not. The strange thing which this question is about is that removing the braces from the \defs (e.g. in \def\B{\mathbb B} removing those around \mathbb B, and similarly from all the other \def commands) seems to cause problems. For example, removing them all except those of \B and \C causes:

./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?

Best Answer

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.

Related Question