[Tex/LaTex] Troubleshooting extra spaces

spacing

My document has lots of extra whitespace appearing between various elements. The problems appear around one macro which appears multiple times. After every line in the macro, I've placed %'s, however, extra white spaces are still appearing.

  • What method can I use to track what is causing these white spaces?
  • Is it possible that items such as ifstrequal (from etoolbox), macros referenced inside macros, mathmode-textmode switching, glossary entries, \pageref's and other cross-referencing and labeling commands. or indented space before code will cause these problems?

Best Answer

Scanner and parser for TeX are closely linked. If we assume that all characters have their usual catcode, then note:

  • Line endings are treated as spaces.
  • Multiple consecutive spaces are combined into one.
  • Multiple consecutive blank lines are interpreted as paragraph end (like \par).
  • Spaces following numbers that are read as numbers, will be interpreted as the end of the number and therefore ignored at output. This seldom affects normal LaTeX code, because in LaTeX \setcounter, \addtocounter, \value, \roman, \Roman, \arabic, \Alph and \the should be used to handle counters and their values.
  • Spaces behind a macro are interpreted as the end of the macro (example: \normalsize \normalfont is the same like \normalsize\normalfont without space between \normalsize and \normalfont).
  • Non-letters immediately following a macro are interpreted as the end of the macro too, but will be interpreted (example: \normalsize9 will be interpreted like \normalsize and 9, \textbf9 is interpreted as \textbf with argument 9 and therefor will result in a bold 9, and \textbf98 will be interpreted as \textbf with argument 9 followed by 8 and will result in a bold 9 followed by a nonbold 8).
  • Except inside the code of classes and packages and between \makeatletter and \makeatother character @ if a non-letter.
  • Argument parentheses are non-letters!.
  • Spaces between the closing and the opening bracket of macro argument will be ignored (e.g. \stackrel{a} {b} is same like \stackrel{a}{b}).
  • Spaces between group brackets are not ignored (in horizontal mode).
  • Spaces after optional arguments (e.g. after \section[foo]) or optional stars off macros (e.g. after \section*) are ignored.
  • LaTeX defines some statements to attempt to summarize spaces before and after the instruction (for example, \index{foo}). That does not always work.
  • LaTeX defines some environments with ignoring spaces immediate after the environment. This usually applies to those environments, that switch into vertical mode (ie in particular not for tabular and minipage that will used to set vertical material in horizontal mode) like verbatim.

This means for example that with a macro like this:

\newcommand*{\Beispiel}[1]{
  { \sffamily{ \bfseries
      #1}
  }
}

three significant spaces will be set before and two after the argument.

Just for better illustration here the same definition again, but I've replaced the significant spaces by \ and the line endings, which are significant spaces too, by :

\newcommand*{\Beispiel}[1]{¶
  {\ \sffamily{\ \bfseries
      #1}¶
  }¶
}

To remove these spaces you have simply omit some of them and comment out the other:

\newcommand*{\Beispiel}[1]{%
  {\sffamily{\bfseries
      #1}%
  }%
}

That's it almost. The last thing to mention is that a horizontal shift to the right of an entire block, such as a tabular, \parbox or a minipage sometimes is caused simply by the normal paragraph indentation. This's always the case if that block is at the beginning of the paragraph or a stand alone paragraph. In this case, a simple solution may be to but \noindent at the beginning of the paragraph (e.g. before the \begin{tabular}).