5

I have quite happily been using the two together for years, with fmtcount included in a \RequirePackage in my class definition, and longtable included as a \usepackage in the document files themselves.

But since I use longtable quite a lot, I decided to move it into my class definition file. The result was an error. Here is a very basic stripped-down example. The class definition file is:

\ProvidesClass{TableExample}

\RequirePackage{fmtcount}
\RequirePackage{longtable}

\PassOptionsToClass{a4paper}{article}
\ProcessOptions
\LoadClass{article}

Then a document that uses it is:

\documentclass{TableExample}

\begin{document}
Useless text.
\end{document}

This produces the following error when complied with pdflatex:

! LaTeX Error: Command \c@table already defined.
               Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.463 \newcounter{table}

I was surprised, because I've been using these together for years, just using fmtcount in the class definition file, and longtable in the document itself. So I figured maybe there is some sort of scope issue - the redefinition is perhaps allowable if it does not occur at the same stage of processing, maybe? (So, before processing the class definition file, versus after?) But after a little experimentation, I found this file compiles with no difficulty at all.

\documentclass[a4paper]{article}

\usepackage{fmtcount}
\usepackage{longtable}

\begin{document}
Useless text.
\end{document}

I did some searching, and found some links stating that the only real difference between \RequirePackage and \usepackage is that \RequirePackage can be issued before the \documentclass command, which isn't the case here. So I'm mystified as to why the two packages coexist when included in two successive \usepackage commands, but conflict when included in two successive \RequirePackage commands.

For what it's worth, I never use the two packages in the same part of the document (for example, I never use an fmtcount capability inside a longtable), but I imagine there might be trouble if I were to do that. Haven't tried it yet.

One remedy is just to leave things as they are, since they don't seem to be causing problems, and just continue to include longtable in my document files, rather than in the class definition. I'm just wondering why I get this odd behaviour.

  • 1
    You can reproduce this with \RequirePackage{longtable} \documentclass[a4paper]{article} \begin{document} Useless text. \end{document}. As far as I can see fmtcount has nothing to do with this. If you make sure to load the longtable package after the article class in TableExample.cls, the problem is gone. – moewe May 17 '18 at 6:20
  • You are correct, I think I made a mistake in my debugging procedure that caused me to think that fmtcount came into play. But I just removed it, and the error occurs exactly as is. Following your suggestion, moving the \RequirePackage{longtable} to after loading the class solves the problem - many thanks! – RLK May 17 '18 at 6:37
  • 3
    Important moral of the story: it's not my fault :-) – David Carlisle May 17 '18 at 7:06
  • @DavidCarlisle - That's also the gist of my answer. :-) – Mico May 17 '18 at 7:32
6

As @moewe has pointed out in an answer, the issue you're experiencing is not related to the fmtcount package. Instead, it is related solely to the fact that the longtable package is being loaded before rather than after the article document class file is loaded. The file longtable.sty contains the following code chunk:

\ifx\c@table\undefined
  \newcounter{table}
  \def\fnum@table{\tablename~\thetable}
\fi

I.e., a test is performed to see if a counter variable named table exists; if it does not, this counter is created via a \newcounter statement. (The code also creates a macro called \fnum@table, but that's not relevant here.) One can only surmise that the author of the longtable package chose to insert this chunk out of an overabundance of caution, to guard against a situation in which an unfortunate user is employing some nasty and brutish document class which, for unknowable reasons, does not provide a counter named table. (I suppose this includes the minimal document class, which -- as its name implies -- should never be used for the creation of "real" documents. For sure, I can't foresee why anyone would wish to create a longtable while using the minimal document class.)

As you can probably guess by now, the article document class does define a counter named table, via (how else?) a

\newcounter{table}

statement. Note that no precautions are taken in article.cls to check if a counter variable named table is already defined. In consequence, the LaTeX kernel spits out an error message and self-terminates. I think it's fair to say that the authors of the article document class (and of the other "standard" LaTeX document classes, such as report and book) simply did not anticipate the contingency of a package that defines the table counter being loaded before the document class is loaded.

You also stated that

I did some searching, and found some links stating that the only real difference between \RequirePackage and \usepackage is that \RequirePackage can be issued before the \documentclass command...

This statement is partially correct as it relies on the weasel-verb "can be issued". However, it wouldn't be correct to infer that there are no consequences to loading a given package before or after the document class file. Otherwise, why even make a distinction between \RequirePackage and \usepackage -- and why not load all packages before loading the document class? As you've discovered the hard way, some "real differences" do exist...


What's the moral of this story? In your own document class, which indirectly loads the article document class, you should make a habit of loading all packages after the following code chunk:

\PassOptionsToClass{a4paper}{article}
\ProcessOptions
\LoadClass{article}

unless you have a very good reason for loading the package(s) before loading the article document class. What might be such a "very good reason"? As @egreg has pointed out in a comment, "Packages should be loaded before \LoadClass or \LoadPackage only if necessary for setting up the code related to class or package options." [emphasis added] To the best of my knowledge, there are very few such packages. One such package was fixltx2e, which contained various fixes to the LaTeX kernel that were discovered between 1994 and 2015. If your LaTeX kernel is more recent than 2015/11/30, it's not necessary (or even advisable!) to load this package.

  • 2
    Packages should be loaded before \LoadClass or \LoadPackage only if necessary for setting up the code related to class or package options. – egreg May 17 '18 at 7:33
  • @egreg - Many thanks for this. I'll edit my posting to include this message. – Mico May 17 '18 at 7:36
  • The letter class (which is one of the base classes) doesn't define table, so it's useful if you need a table in a letter (for example, if it's an invoice). Since longtable is quite old, there may not have been any other letter-like classes around when it was written. – Nicola Talbot May 17 '18 at 10:39
  • @NicolaTalbot - Thanks for this. I must confess to never having used the letter document class, ever, for any of my own work; I was quite unaware until just now of the fact that it doesn't define a counter variable named table either. Good thing I didn't write that letter was among the "standard" LaTeX document classes. :-) [I'll let others fight over what is "standard"...] – Mico May 17 '18 at 10:45
  • 1
    @Mico I first used it back in the day when it was \documentstyle{letter} ;-) then when LaTeX2e appeared I made a custom class based on letter.cls and still use it. There's no real need for floats in a letter and tabular has always been sufficient for my letter-writing needs. – Nicola Talbot May 17 '18 at 14:28
6

The same problem can be reproduced with

\RequirePackage{longtable}
\documentclass[a4paper]{article}
\begin{document}
Useless text.
\end{document}

longtable.sty defines the counter table if it is not defined. But then the article class tries to define the table counter as well.

The solution is to load longtable after the (real) document class. So your .cls could look like

\ProvidesClass{TableExample}

\PassOptionsToClass{a4paper}{article}
\ProcessOptions
\LoadClass{article}

\RequirePackage{fmtcount}
\RequirePackage{longtable}
  • Thanks for the answer, I have moved all my \RequirePackage statements to after the article class is loaded. All except ifthen, since I use it to process my class options. – RLK May 17 '18 at 8:18

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.