[Tex/LaTex] When is leading/opening whitespace of a line in a tex file important

chktexsourcecode

According to https://en.wikibooks.org/wiki/LaTeX/Basics#Spaces

… opening space is generally ignored.

First of all, I don't know what generally refers to.

I came across this using chktex a program to warn about possible typographic or logical errors which latex itself does not warn about.

For instance, chktex raises a warning if in front of \label or \index a space/tab is found (Warning 24: Delete this space to maintain correct pagereferences.).

The reason for this warning is that labels should always be on the same page to that the label relates to (see $ texdoc chktex, chapt. 7, p. 19, v1.7.4).

However, I usually indent lines within my figure environments:

\begin{figure}
  \centering
  \includegraphics{}
  \caption{}
  \label{}
\end{figure}

Therefore, chktex complains about the indentation of \label{} .

In this particular case being a floating box, everything inside the floating box is, afaik, anyhow on the same page (possibly similarly to \begin{equation}...\n\label{eq:first}\end{equation}).

The question: I wonder whether an opening space in front of \label can lead to a wrong pagereference?
Consider indentation in figure environments or for example this situation:

\paragraph{Introduction}%
  text of paragraph...%
  \label{par:end_intro}%can this be on a new page

Consequently, is it reasonable to extend chktex to distinguish "opening spaces" from spaces after first non-space characters to remove false positives as in my case the indentation within the figure environment?
Or in other words is the warning in my case a false positive?

If someone knows a better situation where this is relevant, I'd be happy if you could insert this to the question.

Update chktex version 1.7.5 (2015-12-07) and higher tracks whether the previous line ended in a comment and does not raise warning 24 in the above given example.

Best Answer

The check program is clearly being over cautious (ie wrong) to say page breaks can always happen at spaces before \label.

However the form you give is not optimal,. but for a different reason: If you put the \label after the caption (whether or not you leave a blank space or newline before it) the vertical space is adversely affected.

Consider

enter image description here

\documentclass{article}

\setlength\belowcaptionskip{10pt}
\begin{document}

\begin{figure}[h]
  \caption{ay\label{a}}%
  \begin{center}
    x
  \end{center}
\end{figure}
\begin{figure}[h]
  \caption{ay}\label{b}
  \begin{center}
    x
  \end{center}
\end{figure}

\end{document}

The first form has visibly less space between the caption and the centred text as the space from center is merged with the space already added by the caption. However in the second version the \write node from \label prevents the center environment from detecting the vertical space already added so it adds its full amount of space.

(Note the first example posted here was completely wrong as explained in the other answer. The example above has been added after the original posting.)

Related Question