Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jfbu opened this issue May 21, 2022 · 6 comments
Assignees
Labels
bug enhancement fixed in dev Fixed in development branch, not in stable release

Comments

@jfbu
Copy link

jfbu commented May 21, 2022

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

@davidcarlisle
Copy link
Member

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

@FrankMittelbach
Copy link
Member

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

@eg9
Copy link
Contributor

eg9 commented May 27, 2022

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.

@stale
Copy link

stale bot commented Aug 13, 2022

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

@stale stale bot added the stale label Aug 13, 2022
@FrankMittelbach FrankMittelbach self-assigned this Nov 7, 2023
@FrankMittelbach FrankMittelbach added this to Pool (unscheduled issues) in upcoming LaTeX2e releases via automation Nov 7, 2023
@FrankMittelbach FrankMittelbach added this to the Release 2024 Spring milestone Nov 7, 2023
@FrankMittelbach
Copy link
Member

However, there may be documents around that did \newcounter{index} and did not use \makeindex: defining \c@index in the kernel would break them.

It is true that the environments theindex and thebibliography are not defined by the kernel but as part of the class file. However, they are documented as part of the core LaTeX syntax and so I think it would be okay to make sure that \theindex is not overwritten.

A possible alternative is to ensure that \newcounter is only executed if \the<counter> is undefined at that point.

@FrankMittelbach
Copy link
Member

Or maybe issue a warning that \the<counter> has already a definition and is therefore not touched.

FrankMittelbach added a commit that referenced this issue Nov 7, 2023
@FrankMittelbach FrankMittelbach moved this from Pool (unscheduled issues) to To do in upcoming LaTeX2e releases Nov 7, 2023
@FrankMittelbach FrankMittelbach moved this from To do to In progress in upcoming LaTeX2e releases Nov 7, 2023
FrankMittelbach added a commit that referenced this issue Nov 8, 2023
* possible fix for #823

* try again

* move docu block

* add news entry
@FrankMittelbach FrankMittelbach moved this from In progress to Done in dev in upcoming LaTeX2e releases Nov 8, 2023
@FrankMittelbach FrankMittelbach added the fixed in dev Fixed in development branch, not in stable release label Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug enhancement fixed in dev Fixed in development branch, not in stable release
Projects
Development

No branches or pull requests

4 participants