2

When I use newcommand with no argument, I get expected behavior. When I add an argument to my command, latex adds an extra space. Here's an example:

\documentclass{minimal}
\parindent=0 pt
\parskip=12 pt
\newcommand{\mySpace}[1]{\par \vspace{#1pt} \noindent}
\begin{document}
Hullo world. 
\mySpace{1}
Hullo world. 

\renewcommand{\mySpace}{\par \vspace{1pt} \noindent}
Hullo world
\mySpace
Hullo world
\end{document}

This code returns the following:

Hullo world
 Hullo world
Hullo world
Hullo world

I.e., there is an unexpected space at the beginning of the second line. Is this intended behavior? If so, I'm curious about why? How would I restore expected behavior with an argument?

Thanks for any advice!

  • 3
    When there is no argument, the (end-of-line) space following the macro name is absorbed. With the argument, the (end-of-line) space appears after the right brace, which is not absorbed. For comparison, with no argument version, nonetheless try \myspace{} and you will see the space appear there as well. – Steven B. Segletes Oct 16 '15 at 23:59
  • You need to use %, as suggested in What is the use of percent signs (%) at the end of lines? – Werner Oct 17 '15 at 0:02
4

The definition with argument can use \ignorespaces at the end of the definition text to prevent that the following space matters in horizontal mode after \noindent:

\documentclass{minimal}
\setlength{\parindent}{0 pt}
\setlength{\parskip}{12 pt}
\newcommand*{\mySpace}[1]{\par \vspace{#1pt} \noindent\ignorespaces}
\begin{document}
Hullo world. 
\mySpace{1}
Hullo world. 

\renewcommand{\mySpace}{\par \vspace{1pt} \noindent}
Hullo world
\mySpace
Hullo world
\end{document}

Result

Case with argument

The original definition:

\newcommand{\mySpace}[1]{\par \vspace{#1pt} \noindent}

\par ends the paragraph, current mode is vertical, where spaces are ignored. The space after \par is already ignored as it ends the command token. There are two kinds of command token, which start with a backslash:

  • Long form, where the name consists of letters (one or more) (\par, \a, \vspace, ...).

  • Short form, where the name consists of one non-letter character (\,, \:, ...).

In the first case, a following space is ignored, in the latter case it is not, because TeX already knows, that the name cannot contain more characters.

The space after \vspace{#1pt} becomes a space token, but it is ignored, because TeX is still in vertical mode. The mode is now changed by \noindent. Thus the space after \mySpace{1pt} matters and it can be seen in the output.

This space is ignored, when \noindent is followed by \ignorespaces.

BTW: #1 is expected to be a number, not whole paragraphs, then the argument can be made non-\long, which means that TeX complains during the scanning of the argument, if it find empty lines or \par tokens. The arguments of a command are made non-\long, if the star form of \newcommand is used.

Case without argument

The space after \mySpace is ignored as space after a command name, see above.

  • Thanks very much for all this helpful information. I'm happy to use \ignorespaces, but I inferred from your BTW remark that if I used the star form, i.e., \newcommand*{\mySpace}[1]{\par \vspace{#1pt} \noindent} then the space would be eliminated. It is not however. Did I misunderstand? – Leo Simon Oct 17 '15 at 7:52
  • @LeoSimon \ignorespaces does the trick. The star form is just a convenience, if the closing curly brace after the argument is missing. Then TeX detects this error at the next paragraph and not at the end of the file. – Heiko Oberdiek Oct 17 '15 at 8:10

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.