Next: , Previous: , Up: Definitions   [Contents][Index]

12.12 \ignorespaces & \ignorespacesafterend





Both commands cause LaTeX to ignore blanks (that is, characters of catcode 10 such as space or tabulation) after the end of the command up to the first box or non-blank character. The first is a command from plain TeX, and the second is LaTeX-specific.

The \ignorespaces is often used when defining commands via \newcommand, or \newenvironment, or \def. The example below illustrates. It allows a user to show the points values for quiz questions in the margin but it is inconvenient because, as shown in the enumerate list, users must not put any space between the command and the question text.

  \item\points{10}no extra space output here
  \item\points{15} extra space between the number and the `extra'

The solution is to change to this.


A second example shows blanks being removed from the front of text. The commands below allow a user to uniformly attach a title to names. But, as given, if a title accidentally starts with a space then \fullname will reproduce that.

\newcommand{\honorific}[1]{\def\honorific{#1}} % remember title
\newcommand{\fullname}[1]{\honorific~#1}       % put title before name

\honorific{Mr/Ms}  \fullname{Jones} \\  % no extra space here
\honorific{ Mr/Ms} \fullname{Jones}     % extra space before title

To fix this, change to \newcommand{\fullname}[1]{\ignorespaces\honorific~#1}.

The \ignorespaces is also often used in a \newenvironment at the end of the begin clause, as in \begin{newenvironment}{env name}{... \ignorespaces}{...}.

To strip blanks off the end of an environment use \ignorespacesafterend. An example is that this will show a much larger vertical space between the first and second environments than between the second and third.


Putting a comment character % immediately after the \end{eq} will make the vertical space disappear, but that is inconvenient. The solution is to change to \newenvironment{eq}{\begin{equation}}{\end{equation}\ignorespacesafterend}.

Unofficial LaTeX2e reference manual