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?
  • 4
    Since you already added the % after each line in the macro (assuming with no spaces before that), without a compilable MWE that illustrates this it is going to be difficult to help. – Peter Grill Nov 19 '11 at 2:06
  • There's a nice page about unwanted space at KOMA-Script homepage. Unfortunately it is in German. Maybe someone who understands German and is better in English than me would translate it? – Schweinebacke Nov 19 '11 at 8:13
  • Ok, I've tried to translate it myself. Maybe someone could improve it. – Schweinebacke Nov 19 '11 at 8:50
  • While a spurious space may be caused by a bad definition is a package (one was discovered a few months ago in a very obscure corner of babel), the cause is probably our own macro writing or usage. A minimal example of both is necessary to diagnose the problem, if the advices in the given answer don't solve it. – egreg Nov 19 '11 at 10:25

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:

  { \sffamily{ \bfseries

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 :

  {\ \sffamily{\ \bfseries

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


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}).

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.