Menu

HowTo_AvoidSpaces

How to avoid unwanted spaces in the output

In order to make the problem and the solution understandable, I have to elaborate a bit and explain the background. TeX's parser and scanner are closely connected. If we assume that all characters have their usual catcodes, then the following applies:

  • Line endings are treated as spaces.
  • Several consecutive blank characters are combined into one.
  • Several consecutive blank lines are treated as a paragraph end (\par).
  • Spaces after numbers that are read as numbers are treated as the end of the number. This affects ordinary LaTeX code less, since LaTeX works with \setcounter, \addtocounter, \value, \roman, \Roman, \arabic, \Alph, and \the<counter name>.
  • Spaces after a macro are interpreted as the end of the macro (example: “\normalsize \normalfont” is interpreted as \normalsize and \normalfont).
  • Non-letters immediately after a macro also terminate the macro, but are interpreted (example: “\normalsize9” is interpreted as \normalsize and 9; “\textbf9” is interpreted as \textbf with argument 9 and “\textbf98” is interpreted as \textbf with argument 9 and as 8).
  • Except in classes and packages, and between \makeatletter and \makeatother, the at sign (@) is a non-letter.
  • Argument brackets are non-letters!
  • Spaces between argument brackets are usually ignored.
  • Spaces between pairs of group brackets are not ignored.
  • Spaces after optional arguments or optional asterisks on macros are usually ignored.
  • LaTeX defines some statements in such a way that it tries to merge spaces before and after the statement (for example, \index{foo}). However, this does not always work.
  • LaTeX defines some environments in such a way that spaces are ignored afterwards. This usually applies to environments that switch to vertical mode (so in particular not to tabular and minipage, which set vertical material within horizontal mode).

This means, for example, that for a macro defined as follows:

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

three significant spaces are inserted before the argument and two after it. For better clarification, here is the definition again, where I have marked the significant spaces as and the line ends that become significant spaces by :

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

If these spaces are to be eliminated, simply omit some and comment out the others

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

That's almost it. The last thing to mention is that sometimes a shift of a whole block such as a tabular, \parbox or a minipage is simply caused by the normal paragraph indentation. This is always the case when this block is placed at the beginning of the paragraph or even all by itself in the paragraph. In this case it helps either not to make a paragraph, for example to remove existing blank lines, or if necessary to insert a \noindent at the beginning of the paragraph.


Related

Issues: #52
Wiki (English): HowTo_Layout

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.