# Can spaces at the beginning of a line ever cause problems?

To structure my sourcecode, I often use spaces for padding:

\newcommand{\st}[1]{%
\begin{tabular}{lr}
\emph{Student} & \emph{Score}\\
#1
\end{tabular}%
}


Can this ever cause problems? Like putting no % at the end some lines.

• Spaces at the beginning of a line are irrelevant to TeX? You can put as many as you want. – Johannes_B Nov 27 '13 at 13:35
• Except in verbatim like context spaces at start of line are ignored, but note you have missing % on lines 1 and 5 which could produce anomalous white space in the result. – David Carlisle Nov 27 '13 at 13:37
• @DavidCarlisle: Thanks, good call on the missing % :-). Someone add it as an answer so I can accept it. – Frank Seifert Nov 27 '13 at 13:41
• @Johannes_B you were first:-) – David Carlisle Nov 27 '13 at 13:43

Usually, spaces at the beginning of a line are ignored by TeX. However, this is not the case when writing in a verbatim like context. Tabulators at the beginning of a line are ignored as well.

Whitespace characters like NO-BREAK SPACE (U+00A0) behave in a different way. To use them properly, a modern engine like LuaLaTeX in conjunction with fontspec is needed.

Please be aware, that the end of a line is nothing more than a ␣ (space) for LaTeX, if in M-mode (middle of a line), so you have to be careful in the definition of your own commands.

\documentclass{article}
\newcommand\testa{h
ello}
\newcommand\testb{h%
ello}
\begin{document}
\testa \par \testb

\begin{verbatim}
h
a
ll
o
\end{verbatim}
\end{document}

• Does the same apply for tabs or other whitespace characters? – ComFreek Nov 27 '13 at 15:33
• @ComFreek Good question, I added the information to the answer. Can somebody please varify the behaviour for »other whitespace character«? I somehow would have guessed a different behaviour. – Johannes_B Nov 27 '13 at 17:11
• I'm not really sure about U+00A0, which in the normal setting of LuaLaTeX and XeLaTeX has category code 12 when fontspec is loaded. – egreg Nov 27 '13 at 17:12
• @egreg I tried it out myself, but as I said, i don't think this should happen. Am i missing something? – Johannes_B Nov 27 '13 at 17:14
• @Johannes_B Try loading fontspec – egreg Nov 27 '13 at 17:17

The comment environment (comment package) should start in a new line without a space before. From the manual:

The opening and closing commands should appear on a line of their own. No starting spaces, nothing after it.

Otherwise it won't work and cause strange errors.

• the same is true for \end{verbatim} – barbara beeton Nov 27 '13 at 17:38