Your support for our advertisers helps cover the cost of hosting, research, and maintenance of this document

Formatting Information — An introduction to typesetting with LATEX

Chapter 1: Writing documents

Section 1.6: White-space in LATEX

LATEX does its own spacing and alignment based on the layout defined in the templates (classes) and stylesheets (packages) you give it, plus the commands that you type in.

You cannot force white-space into your output by typing it into your editor, as you do with a wordprocessor. If you need extra white-space in your document, use a horizontal or vertical spacing command, or change the spacing parameters or the document or the environment involved (see § 6.1.1 below).

There are three simple rules for spacing in LATEX (see the note ‘Three rules for spacing in LATEX documents’ below) — the first two say that, essentially, multiple spaces and blank lines are ignored in the typesetting. That means you are free to use them in your editor for optical ease and personal convenience when editing.

Three rules for spacing in LATEX documents

  1. All consecutive spaces and TAB characters are treated as if they were a single space during typesetting;

  2. All consecutive newlines (linebreaks) are treated as if they were just two newlines (a paragraph break)

  3. Any white-space after a command ending in a letter is discarded when there is no argument present.

The third rule means that any simple command which ends in a letter and has no argument MUST be followed by white-space or an empty pair of curly braces before the text which follows it, to keep it separate. The following examples will all produce identical output.

\tableofcontents Thanks to Aunt Mabel for all her 
help with this book.

\tableofcontents	Thanks to Aunt Mabel for 
all her help with this book.

\tableofcontents
Thanks to Aunt Mabel for her help with this book.

\tableofcontents{}Thanks to Aunt Mabel for all her 
help with this book.

\tableofcontents




Thanks to Aunt Mabel for her help with this book.

The additional spacing or braces is not needed if

  • the command name ends with a non-letter, or;

  • it is directly followed by another command, or;

  • it occurs immediately before a closing curly-brace, or;

  • it is followed by a double newline (paragraph break)

If you forget the white-space, like this:

\tableofcontentsThanks to Aunt Mabel for all her 
help with this book.

then LATEX will treat everything up to the next non-letter as a command, so it will end up trying to make sense of a ‘command’ apparently called \tableofcontentsThanks. There’s no such command, of course, so LATEX will complain by displaying an error message about an ‘undefined control sequence’ (see § B.3.3.2 below).

With commands that take arguments you do not need to use extra white-space or curly braces after the command, because the curly braces will keep the command separate from any normal text which comes after it. The following example is therefore exactly equivalent to the one we just saw in § 1.5.3 above, and will typeset identically despite the absence of spaces between commands.

\chapter{Poetic Form}\label{pform}The shape of poetry 
when written or printed distinguishes it from prose.

By the same token, the following example is therefore also exactly equivalent (although rather unusual!):

\chapter        {Poetic Form}               \label
	  {pform} The shape of
poetry when written                  or
	      printed distinguishes it from prose.

That is, it will get typeset exactly the same. Here’s what you would normally type:

\chapter{Poetic Form}
\label{pform} 

The shape of poetry when written or printed 
distinguishes it from prose.

Exercise 1.6 — To space or not to space

Which of the following commands needs to be followed by white-space (or another command, or an empty pair of braces)?

  1. \begin{document}

  2. \LaTeX

  3. \chapter{Introduction}

  4. \today

  5. \textit{Star Trek}

Why would you want all that spacing in the examples (or none)? The answer is usually never, although extra blank lines in your editor between chapters or sections make editing easier. But a lot of LATEX is not typed by hand: it is generated by computer programs from other systems such as web scripts, XML documents, databases, filestores, mashup engines and other processes, and it makes life easier for the programmers if they don’t have to worry about the odd space or two creeping in here and there in normal text: it simply won’t have any effect. It also means that if you want to use extra spacing to make your text easier to edit, you don’t have to worry about unwanted linebreaks coming out between sections or paragraphs, tabbing in tables, or indentation in list items.