# Custom style creates incompatibility between mathtools and listings

My school, the University of Rhode Island (URI),
recommends using style files developed by the school for completing theses,
such as a custom document class file, urithesis.cls,
for the \documentclass{} command.

I have determined that this custom document class file
creates an incompatibility between the mathtools and listings packages.

I perused the .cls code and was not able to determine any obvious problems.
(This is expected, as I am not well-versed in custom document class files.)
I notified the owner/creator, and have not yet received a response.

Is this type of issue when using a custom document class
a common and generally easily-resolved issue?
(Both packages are highly-desirable to me.)

MWE:

In the above link, reiterated here for convenience,
the document class file is provided, as well as a minimum working example (MWE).

Please download the thesis.zip link for the complete MWE.
If the following code is added to the preface of the root file thesis.tex:

\usepackage{mathtools}
\usepackage{listings}


then an error message will occur.
Commenting either package out will resolve the issue.

The class does a very wrong thing, which explodes when calc is loaded together with listings.

% line 424
\newcounter{chaptercitecount}

% lines 795-801
\def\cl@chapter{%
\setcounter{chaptercitecount}{0}%
\if@sequential%
\@elt{section}\@elt{footnote}%
\else%
\@elt{equation}\@elt{figure}\@elt{footnote}\@elt{section}\@elt{table}%
\fi}


The \cl@chapter macro is executed whenever chapter is stepped, so as to do the required resetting. Doing \setcounter inside it is wrong anyway.

You have to fix the class, I'm afraid. The first bit should become

% Counts the number of references in each chapter to determine if there
% is a list of references for that chapter when using chapterref format.
\newcounter{chaptercitecount}[chapter]
\renewcommand{\thechaptercitecount}{\arabic{chaptercitecount}}


and the second part should be

\def\cl@chapter{%
\@elt{chaptercitecount}%
\if@sequential%
\@elt{section}\@elt{footnote}%
\else%
\@elt{equation}\@elt{figure}\@elt{footnote}\@elt{section}\@elt{table}%
\fi}


There are much better methods than to hook in \cl@chapter, though.

I'd suggest removing the redefinition of \cl@chapter, adding

\RequirePackage{chngcntr}


in the proper place (after \LoadClass) and, at the end of the class,

\if@sequential
\counterwithout*{figure}{chapter}
\counterwithout*{table}{chapter}
\counterwithout*{equation}{chapter}
\else
\counterwithin{equation}{chapter}
\counterwithin{figure}{chapter}
\counterwithin{footnote}{chapter}
\counterwithin{section}{chapter}
\counterwithin{table}{chapter}
\fi

• @kando I think I interpreted correctly what the class is doing; essentially, \@elt{chaptercitecount} will do \setcounter{chaptercitecount}{0}, but in a safe way. I'll try and suggest a better fix. Jun 20 '17 at 17:15
• @kando I added the better fix Jun 20 '17 at 17:29
• @kando The redefinition of \cl@chapter should be removed; the better definition of the chaptercitecount counter should be as in the previous fix. The new part can go essentially anywhere after \LoadClass. At the end, when all relevant counters are surely defined is a good place. Jun 20 '17 at 17:45
• @kando Remove from \def\cl@chapter{ up to the matching }. Jun 20 '17 at 18:06
• @kando REMOVE from \def\cl@chapter{ up to the matching }. Jun 20 '17 at 19:10