latex-parser icon indicating copy to clipboard operation
latex-parser copied to clipboard

`\label` must directly follow preceding control sequence without newline in between

Open Holzhaus opened this issue 2 years ago • 3 comments

Before:

\section{foo}\label{sec:foo}

After reformatting:

\section{foo}
\label{sec:foo}

Although it looks nicer, this actually introduces a bug. Because the newline is treated as a space, it's possible that the space does not fit on the same page, which means that the \label ends up on the next page (resulting in a wrong page number).

Holzhaus avatar Mar 26 '22 19:03 Holzhaus

How frustrating...do you have an example of a document where this happens?

siefkenj avatar Mar 27 '22 00:03 siefkenj

Nope, but chktex warns about this. It'm getting warnings after reformatting:

% original.tex
\section{Hello}\label{foo}

Lorem ipsum dolor sit amet.
$ chktex original.tex
ChkTeX v1.7.6 - Copyright 1995-96 Jens T. Berger Thielemann.
Compiled with POSIX extended regex support.
No errors printed; No warnings printed; No user suppressed warnings; No line suppressed warnings.
% reformatted.tex
\section{Hello}
\label{foo}

Lorem ipsum dolor sit amet.
$ chktex reformatted.tex
ChkTeX v1.7.6 - Copyright 1995-96 Jens T. Berger Thielemann.
Compiled with POSIX extended regex support.
Warning 24 in doc.tex line 2: Delete this space to maintain correct pagereferences.
\label{foo}
^
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.

Holzhaus avatar Mar 27 '22 08:03 Holzhaus

I'll have to think about this. I don't think chktex is right here. A \section should be strongly glued to the next paragraph so they should end up on the same page. Even if they don't end up on the same page, I think there's a fair argument that the link serves the reader best pointing to the start of the content of the section rather than the section heading.

The only place I could see this going wrong is if there were a bunch of blank sections in a row. The sections headings wouldn't be glued to each other, so maybe this pushes the pageref to the other side?

In any case, I can also see how it's annoying to pretty-print code and suddenly get chktex warnings.

As a temporary workaround, you should be able to do

\section{foo}%
\label{bar}

siefkenj avatar Mar 27 '22 14:03 siefkenj