4

After a hunt through several subfiles, I've narrowed down what I thought was a bug down to this MWE

\documentclass{article}
\usepackage{amsmath, amssymb}
\newcommand{\R}{\mathbb{R}}
\begin{document}
$\R$
\newdimen\R
\R=.4cm
$\R$
\end{document}

Clearly user error. Although this is what I get for copy and pasting code without paying too close attention to the variables they use. What commands should I use to be warned of this collision in compiling instead of getting the error ! Missing number... and having to scour several files for what this could be?

I feel like this is along the lines of \def versus \newcommand.

  • 1
    Use LaTeX's \newlength instead of \newdimen. – Henri Menke Dec 19 '18 at 5:30
  • by the way, use \the\R instead of \R if it is a dimen register and you want to typeset its value (in pt units) – user4686 Dec 19 '18 at 8:06
6

You should use LaTeX's \newlength and \setlength instead of \newdimen and = assignment.

\documentclass{article}
\usepackage{amsmath, amssymb}
\newcommand{\R}{\mathbb{R}}
\begin{document}
$\R$
\newlength\R
\setlength\R{.4cm}
$\R$
\end{document}
6

Yes, it's the same as the problem with \def.

The \newdimen macro comes from plain TeX and it's in LaTeX for historical reasons: in order to ease transition from plain TeX, LaTeX was loaded with a copy of plain TeX, barring a few commands that were reused (\line, for instance).

The only difference between plain TeX's and LaTeX's \newdimen is that the former is \outer and the latter isn't.

\outer\def\newdimen{\alloc@1\dimen\dimendef\insc@unt}  %% <-- plain TeX

\def\newdimen{\alloc@1\dimen\dimendef\insc@unt} %% <--- LaTeX (up to 2015)

The LaTeX definition has slightly changed in 2015, because a different set of allocation macros was introduced to cope with the extended range of registers afforded by e-TeX.

The \alloc@ macro in LaTeX was (and its current variant is) essentially the same as in plain; the \dimendef primitive (the ultimate responsible for assigning the desired meaning to \foo in \newdimen\foo) does no check for definedness of the control sequence.

The LaTeX commands that do definitions and allocations and are documented in the manual all do a check.

Users who know about \newdimen may exploit it, if they so wish, with the caveat that they should know what they're doing. And, much more important, they should not hand a preamble filled with \newdimen and \def to their mates or colleagues who aren't supposed to be TeX gurus and will be thoroughly confused by weird error messages such as Missing $ when trying to print *façon• (just because their friend's preamble had \def\c{\gamma}, this actually happened).

Is there a difference between \newdimen and \newlength, apart from the check? Yes, but minor: \newlength is a wrapper around plain TeX's \newskip, so it allocates a \skip register rather than a \dimen one. This difference may bite if one doesn't use LaTeX syntax for setting lengths, that is \setlength.

1

You can create a wrapper around \newdimen that gives you an error in that case.

\documentclass{article}
\usepackage{etoolbox}
\usepackage{amsmath, amssymb}
\newcommand{\R}{\mathbb{R}}
\newcommand{\newdimension}[1]{%
  \ifundef{#1}
    {\newdimen#1}
    {\errmessage{command \expandafter\string#1 already defined - can't create dimension}}}

\begin{document}
$\R$
\newdimension{\R}
\R=.4cm
$\R$

\newdimension{\mydim}
\mydim=.4cm
\showthe\mydim
\end{document}
  • That'll work. If there's not a native command, I'll accept this. – Robert Wolfe Dec 19 '18 at 5:25
  • As far as I know there is none, sorry... – AndiW Dec 19 '18 at 5:28
  • @AndiW Yes, there is, see my answer. – Henri Menke Dec 19 '18 at 5:34
  • Arrgghh... sometimes you don't think about the most basic things... I'll take the blame... – AndiW Dec 19 '18 at 5:36

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.