4

None of my \newcommand declarations work for \hspace, with or without arguments; I use the latest MathJax with the general libraries. Attempts so far:

\newcommand{hs}[1]{\hspace{#1}}
\newcommand{hs}[1]{\hspace{{#1}}}
\newcommand{hs}[1]{\hspace{{#1}px}}
\newcommand{hs1}[]{\hspace{10px}}

I have a pageful of other \newcommand declarations that work just fine, unsure what's failing here. Any remedy?

  • 5
    Welcome to TeX.SE! The command name has to be preceded by a backslash: \newcommand{\hs}[1]{\hspace{#1}}. Add a backslash and it will work. The and third versions add braces that shouldn't be there, so they won't work. The fourth version won't work because numbers can't be in command names, and the first optional argument to \newcommand can't be empty. – Phelype Oleinik Jan 25 at 18:12
  • Note that mathjax is not LaTeX so you should probably not expect any \newcommand to work. – daleif Jan 25 at 18:13
  • 2
    @daleif \newcommand works in MathJax. – egreg Jan 25 at 18:24
  • @PhelypeOleinik Excellent, it works - thanks. Feel free to post your response as an answer if you'd like the answer credit. – OverLordGoldDragon Jan 25 at 18:34
  • @egreg out of the box with no extra configuration? – daleif Jan 25 at 19:16
11

In usual conditions, all commands in TeX must be preceded by a backslash, to tell TeX that they are commands, so instead of

\newcommand{hs}[1]{\hspace{#1}}

you need

\newcommand{\hs}[1]{\hspace{#1}}

which will work as you expect.

Side note: If you just want to create a copy of a command with another name, you can use \let: \let\hs=\hspace. With the \newcommand approach the \hs command will use \hspace, and with the \let approach it will be a copy of \hspace. One advantage of the latter is that \hs*{argument} will work as you'd expect. With \newcommand it won't.

The second version adds an extra pair of braces that shouldn't be there, so it won't work as well.

\hspace{{1cm}} will expand to \hskip{#1}\relax, which isn't valid syntax and will throw you a Missing number error.

The third version is the same. The unit will only appear after the Missing number error, so it is of no use there. Besides, TeX doesn't know the unit px, so even if you remove the braces you'll still get a Illegal unit of measure.

The fourth contains, besides the illegal px unit, two problems. The first is that in usual circumstances numbers aren't allowed in command names, so you can't use \hs1. The second is that the first optional argument of \newcommand (the number of arguments) must not be empty ([]). An empty argument is very different from a missing argument! If you try to do that (after removing the number from the command name) you'll get another Missing number error because LaTeX was expecting a number there.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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