latex2e
latex2e copied to clipboard
\newcounter{index} does not raise any warning to user but ultimately causes \printindex to fail
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
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
"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.
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
- check whether
\c@index
is defined (by\newcounter{index}
) and in this case raise an error; - 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.
This issue has been automatically marked as stale because it has not had recent activity.