latex2e icon indicating copy to clipboard operation
latex2e copied to clipboard

\newcounter{index} does not raise any warning to user but ultimately causes \printindex to fail

Open jfbu opened this issue 2 years ago • 6 comments

Brief outline of the bug

\newcounter{index} redefines \theindex hence causes \printindex to fail with

./testindex.ind:3: LaTeX Error: Lonely \item--perhaps a missing list environmen
t.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.3   \item f
             oobar, 1
Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.

The problem here is that \newcounter issues no warning whatsoever.

Minimal example showing the bug

\RequirePackage{latexbug}       % <--should be always the first line (see CONTRIBUTING)!
\documentclass{article}

  % Any preamble code goes here
\usepackage{makeidx}
\makeindex
% bad, but user is not warned
\newcounter{index}
\begin{document}

  % Demonstration of issue here
test % btw without it nothing goes to idx...
\index{foobar}

% lonely \item error
\printindex
\end{document}

Log file (required) and possibly PDF file

testindex.log

jfbu avatar May 21 '22 08:05 jfbu

hmm you get essentially the same from

\documentclass{article}


% bad, but user is not warned
\newcounter{bibliography}
\begin{document}

\cite{qqq}

\begin{thebibliography}{99}
\bibitem{qqq}hmm
\end{thebibliography}

\end{document}

It's always been this way, but I suppose the standard classes could define

\expandafter\def\csname c@index\endcsname{}
\expandafter\def\csname c@bibliography\endcsname{}

If we did decide it was safe to catch this after all this time

davidcarlisle avatar May 21 '22 09:05 davidcarlisle

"perhaps a missing item" is not the world best error message but given that this is the way it is for all LaTeX documents since the age of dawn I doubt that many people ever ran into it. But I guess we can make the above definitions to improve that a bit. I can't think of any bad side-effects because of them.

FrankMittelbach avatar May 21 '22 09:05 FrankMittelbach

The idea to name the internal environments fo the index and the bibliography as theindex and thebibliography was not so good to begin with, particularly for theindex. There's also theglossary to take care of.

The chance that some user wants \newcounter{bibliography} or \newcounter{glossary} is quite slim, but index is a good name for a counter. However, there may be documents around that did \newcounter{index} and did not use \makeindex: defining \c@index in the kernel would break them.

We might possibly add a check at begin document: if the document is making an index (this can be checked by comparing \makeindex to \@empty), we should

  1. check whether \c@index is defined (by \newcounter{index}) and in this case raise an error;
  2. define \c@index so a \newcounter{index} in the body of the document would raise an error.

Case 2 can arise if there are macros defining counters that can be used in the document.

eg9 avatar May 27 '22 11:05 eg9

This issue has been automatically marked as stale because it has not had recent activity.

stale[bot] avatar Aug 13 '22 04:08 stale[bot]