27

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.

  • I'm certainly open to the idea of improving ChkTeX to handle these situations (more) correctly. I've opened bug savannah.nongnu.org/bugs/?45980 to track this issue. Unfortunately, it may be somewhat difficult to get "right". Especially without making ChkTeX too complicated. – Ivan Andrus Sep 23 '15 at 5:16
  • The answers here suggest that chktex's advice is not so helpful here. To turn it off entirely you can add a line PostLink = { \index } to ~/.chktexrc. This leaves the analogous warning for indices intact. – Nathan Musoke May 1 '18 at 2:11
17

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

  • 2
    \@bsphack in vertical mode should carry over \prevdepth. ;-) – egreg Sep 1 '15 at 8:46
  • Thanks for your insightful answer. I will adapt my usage of labels in floating environments with captions. Some texteditors (or their plugins) are not aware of this. I know wrong usage patterns in vim latex-suite macro EFI, github.com/honza/vim-snippets/blob/master/UltiSnips/…, kile (v2.1.0), latexila (v2.4.0). There are probably more out there. – Hotschke Sep 1 '15 at 8:46
  • @egreg in the latex sources you will see some attempts at vertical space hacks but there were "issues" and they were never really enabled. – David Carlisle Sep 1 '15 at 8:58
  • @DavidCarlisle There is a late answer referring to your answer. – jjdb May 10 '17 at 14:46
  • 1
    @jjdb see update – David Carlisle May 11 '17 at 14:49
11

If no special setting to spaces is active (essentially, we're not in verbatim mode), spaces at the beginning of a line are ignored. If chktex complains, it's for nothing, in this case.

Actually, the program is generally wrong in warning about spaces before \label or \index, because their definition starts with \@bsphack, whose duty is to check whether horizontal glue precedes it and, in this case, remember it and suppress it; the next \@esphack command will insert the remembered the space (after the label or index entry) and ignore subsequent spaces.

Here's an example

\documentclass{article}

\begin{document}

Here is an example of a ``misplaced label'' \label{mispl} according
to \texttt{chktex}.

Here is an example of a ``misplaced label'' according
to \texttt{chktex}.

\end{document}

with the output by chktex

ChkTeX v1.7.4 - Copyright 1995-96 Jens T. Berger Thielemann.
Compiled with POSIX extended regex support.
Warning 24 in splab.tex line 5: Delete this space to maintain correct pagereferences.
Here is an example of a ``misplaced label'' \label{mispl} according  
                                           ^
No errors printed; One warning printed; No user suppressed warnings; No line suppressed warnings.
See the manual for how to suppress some or all of these warnings/errors.

However, it is apparent that the typesetting is correct:

enter image description here

Note that your example can be simplified: there's no need for %.

\begin{figure}
  \centering
  \includegraphics{file}
  \caption{Caption text}
  \label{whatever}
\end{figure}

This is because \caption starts and ends with a \par, so \label is processed in vertical mode. The end-of-line after the \includegraphics will be suppressed by \par and \label is processed in vertical mode, where spaces do nothing.

My personal preference goes to

\begin{figure}
\centering

\includegraphics{file}

\caption{Caption text}
\label{whatever}

\end{figure}

I generally prefer blank lines to indentation, that makes clearer the distinction between various parts of the input. However it's just my style.

  • Thanks for your answer. The answer by David Carlisle points out that labels should be inserted into the caption command argument. Regarding the coding style: Well, I guess I do not prefer blank lines instead of indentation. I use blank lines to separate paragraphs, sections, chapters. This is the standard way for paragraphs anyway. In this context the empty line between\ includegraphics and \caption is in my opinion sensible but I would not add the other ones. I certainly appreciate that experienced users share their coding style. IMHO coding style can help to write better sourcecode. – Hotschke Sep 1 '15 at 9:00
  • @egreg There is a late answer referring to your answer. – jjdb May 10 '17 at 14:47

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.