2

I want to define this macro:

\newcommand{stackonetwo}{\begin{smallmatrix} 1 \\ 2 \end{smallmatrix}}

But I think the compiler doesn't like \\ inside the new command. It returns the error Missing number, treated as zero. Why is this error happening, and what is the correct way to circumvent this error and do what I wanna do?

2
  • 4
    I haven't tested this, and I don't think it's the cause of the error message, but stackonetwo isn't preceded by a backslash, so it isn't a valid control sequence. If, once you've added the backslash the error still occurs, add a second pair of backslashes around the entire contents of the macro argument. If that doesn't get rid of the error, then it's something else and more information is needed. May 24, 2020 at 1:59
  • oops!!! I said to add a second pair of backslashes. No!! That should be a second pair of braces. May 24, 2020 at 20:13

2 Answers 2

10

You say that you get the error missing number, but it is always best to look at the first error. If you (or your editor) scroll past an error TeX will try to recover but such recovery often introduces spurious following errors.

\documentclass{article}

\usepackage{amsmath}

\newcommand{stackonetwo}{\begin{smallmatrix} 1 \\ 2 \end{smallmatrix}}

\begin{document}

\end{document}

Produces the error

! LaTeX Error: Missing \begin{document}.

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

l.5 ...egin{smallmatrix} 1 \\ 2 \end{smallmatrix}}

only if you scroll past that do you get

! Missing number, treated as zero.
<to be read again> 
                   a
l.5 ...egin{smallmatrix} 1 \\ 2 \end{smallmatrix}}

? 

then if you scroll past that TeX gets really confused and you get

! Missing control sequence inserted.
<inserted text> 
                \inaccessible 
l.5 ...egin{smallmatrix} 1 \\ 2 \end{smallmatrix}}

? 

the initial error is due to the missing \ in \stackonetwo which is causing the letters to "fall out" of the definition and start to be typeset as a paragraph of text before \begin{document}

If you fix that error then

\documentclass{article}

\usepackage{amsmath}

\newcommand{\stackonetwo}{\begin{smallmatrix} 1 \\ 2 \end{smallmatrix}}

\begin{document}

\end{document}

runs without error and will typeset the matrix as intended when used in math mode.

7
  • This does not work in text mode, and OP has not stated that she exclusively aims to use the command in math mode.
    – user78499
    May 24, 2020 at 14:09
  • 2
    @Roboticist that is by design. neither does smallmatrix. While it's possible to use \ensuremath that's really an anti-pattern. TeX commands should almost always be strongly math or strongly text. As this is a shorthand for a smallmatrix it's reasonable to assume that math is intended here. May 24, 2020 at 14:19
  • Simply adding \ensuremath won't provide simultaneous math-text compatibility to your answer. Furthermore, "TeX commands should almost always be strongly math or strongly text." is more like a personal opinion, rather than a principle, though I respect your expertise and experience to claim that.
    – user78499
    May 24, 2020 at 14:37
  • 1
    What a long answer to say, you forgot the backslash. ;) More importantly, I scrolled up through the compiler output and there was no Missing \begin{document} error. Testing it out, this because I defined the newcommand inside the begin/end document environment ... which compiles fine. May 24, 2020 at 15:31
  • 2
    @MikePierce that's why question should always provide a test document, had to make some guesses, I suppose mostly the answer was a comment on the other answer (which did not assume you had forgotten a backslash rather that you wanted a definition form without backslash) but if your original question had (or if you edit it so it does) show where you used the command I'd change this answer. The main point of the answer was to say to check the first error and if that doesn't apply here the answer isn't much help:-) May 24, 2020 at 15:34
5

Your command has two problems. First, there is no \ before the given command name. Second, smallmatrix envoriment needs to be invoked in math mode. Taken both into account, compilation issue is resolved.

\documentclass{standalone}

\usepackage{amsmath}

\newcommand{\stackonetwo}{$\begin{smallmatrix} 1 \\ 2 \end{smallmatrix}$}

\begin{document}    

\stackonetwo

\end{document}

EDIT 1: As Vincent correctly noted, the code above messes with the math-mode usages of the command. Alternatively, the version below lets one use the cammand in both text and math modes.

\documentclass{standalone}

\usepackage{amsmath}

\newcommand{\DefineNamedFunction}[1]{
    \expandafter\providecommand\csname#1\endcsname
    {\ensuremath{\begin{smallmatrix} 1 \\ 2 \end{smallmatrix}}}
}

\begin{document}    

    \DefineNamedFunction{stackonetwo}

    $\stackonetwo$ 

    \stackonetwo

\end{document}

EDIT 2: According to the recommendation of David Carlisle (to address the comment of egreg), the better version of my code may be as follows.

\documentclass{standalone}

\usepackage{amsmath}

\newcommand{\DefineNamedFunction}[1]{%
    \expandafter\newcommand\csname#1\endcsname
    {\ensuremath{\begin{smallmatrix} 1 \\ 2 \end{smallmatrix}}}%
}

\begin{document}    

    \DefineNamedFunction{stackonetwo}

    $\stackonetwo$ 

    \stackonetwo

\end{document}
11
  • 1
    You're right that smallmatrix has to be called in math mode, but I'm not sure it's a good idea to add the $ in the command's definition, especially if \stackonetwo is intended to be used in equations. For example, with this definition, \stackonetwo cannot be used directly in a display math environment.
    – Vincent
    May 24, 2020 at 3:18
  • @Vincent: I just added an alternative version.
    – user78499
    May 24, 2020 at 3:40
  • 3
    What's the advantage in using such a complicated definition when \newcommand{\stackonetwo}{...} is much simpler? Besides, \providecommand can lead to unexpected results if you say \DefineNamedFunction{box}{...}.
    – egreg
    May 24, 2020 at 12:19
  • 1
    @Roboticist so why use \providecommand so that the clash is silently ignored, rather than \newcommand so that the user is alerted? May 24, 2020 at 14:36
  • 2
    but I think that this doesn't actually answer the question:-) The OP got an error because a \ was omitted, so the fix is to add the forgotten backslash not to define a new definition form that does not need it. May 24, 2020 at 14:49

Not the answer you're looking for? Browse other questions tagged .