0

I was using this template: https://www.overleaf.com/latex/templates/university-of-michigan-dissertation-template-unofficial/tpnjzndnrzmf

As long as I add \usepackage{mathtools} in package.tex, there is a "!" after Appendices.

enter image description here

I suspect this is because of the conflict between mathtools and amsmath after reading this: mathtools vs amsmath

But even if I comment out \RequirePackage{amsmath} in thesis-umich.cls, the problem is still present. How can I fix this problem?

Ypbor
  • 399
  • 2
    Can you please provide a minimal example showing this? I could not reproduce – mickep May 04 '22 at 04:46
  • 2
    And exactly how does that page say there is a conflict between amsmath and mathtools? – daleif May 04 '22 at 05:01
  • 1
    Here's a test, does that ! Appear if you load calc or graphicx instead of mathtools (it loads those two) – daleif May 04 '22 at 05:05
  • 3
    As expected this is nt caused by mathtools it self, but by calc. One can cut the error down to `\documentclass{thesis-umich} \usepackage{calc} \begin{document} test

    \appendix

    test \end{document}` and btw this generates an error. Don't ignore errors just because it produces a PDF, the PDF may contain the wrong output.

    – daleif May 04 '22 at 06:59

1 Answers1

9

The problem has nothing to do with mathtools it self, but rather to it loading the calc package`. So we can reduce the problem to

\documentclass{thesis-umich} 
\usepackage{calc} 
\begin{document} 
test  
\appendix  
test 
\end{document}

which produces the error

! Undefined control sequence.
\@calc@post@scan ...st@scan \else \def \calc@next 
                                                  {\calc@error #1}\fi \fi \f...
l.11 \appendix

When using Overleaf, please never ever ignore compilation errors. Overleaf, like many LaTeX editors run in a mode that tries to complete the compilation even if there are errors. This may cause issues in the PDF with things not being typeset correctly. Often a single error causes many others ...

The problem her is this definition in thesis-umich.cls

\renewcommand{\appendix}{ %
 % Move to new page.
 \clearpage %
 % Renew the counters.
 \renewcommand*{\thechapter}{\Alph{chapter}} %
 % Start over the chapter counter.
 \setcounter{chapter}{0} %
 % Add a pdf anchor.
 \phantomsection %
 % Add blank space to table of contents
 \addtocontents{toc}{\vspace{3.9ex}}
 % Add the page to the table of contents.
 \addcontentsline{toc}{backchapter}{Appendices}
 % Stop adding sections to the table of contents.
 \addtocontents{toc}{\setcounter{tocdepth}{1}} %
 % Header for appendices.
 \renewcommand{\@chapapp}{APPENDIX} %
 % Renew the chapter and section labels.
 \let\@chapter\@chapter@appendix %
}

especially the line

\addtocontents{toc}{\setcounter{tocdepth}{1}} %

whenever calc is loaded, \setcounter becomes a fragile command, and this it needs protection (from premature expansion) when being written to a file.

So here the fix is to add this to your preamble (don't change the cls file, rather send those maintaining it a message and ask them to fix it).

\makeatletter
\renewcommand{\appendix}{ %
 % Move to new page.
 \clearpage %
 % Renew the counters.
 \renewcommand*{\thechapter}{\Alph{chapter}} %
 % Start over the chapter counter.
 \setcounter{chapter}{0} %
 % Add a pdf anchor.
 \phantomsection %
 % Add blank space to table of contents
 \addtocontents{toc}{\vspace{3.9ex}}
 % Add the page to the table of contents.
 \addcontentsline{toc}{backchapter}{Appendices}
 % Stop adding sections to the table of contents.
 \addtocontents{toc}{\protect\setcounter{tocdepth}{1}} % <-----
 % Header for appendices.
 \renewcommand{\@chapapp}{APPENDIX} %
 % Renew the chapter and section labels.
 \let\@chapter\@chapter@appendix %
}
\makeatother
daleif
  • 54,450
  • we should fix mathtools not to make setcounter fragile – David Carlisle May 04 '22 at 08:56
  • @DavidCarlisle it's not mathtools that does that its calc – daleif May 04 '22 at 09:18
  • @DavidCarlisle you should probably fix mathtools so it does not use calc, but that does not make the calc problems go away. – daleif May 04 '22 at 09:18
  • yes not using calc would be good here. But as you say we could make the calc version robust anyway. Add it to the list of jobs to do:-) – David Carlisle May 04 '22 at 09:20
  • latex2e defines \setcounter by simple \def hence it's not robust, but just by coincidence the fully expansion of \setcounter{<cnt>}{<num>} is \global \c@<cnt> <num>\relax when counter <cnt> is defined. – muzimuzhi Z May 04 '22 at 09:30
  • @DavidCarlisle I actually don't think mathtools currently uses calc (quickly looked for the 4 macros). But empheq uses it a lot. So for now, the loading of calc could probably be moved to empheq (and I can have a look at what it uses, and probably rewrite using \dimexpr and friends. – daleif May 04 '22 at 10:02
  • Thank you very much for your help! I have some generic questions. 1. I know it is good to have a minimal example, but the template is not putting everything in a single "main" file. So how shall I construct a minimal example when I propose questions on Stack Exchange? 2. How can I locate the error? Overleaf suggests some errors in "./output.toc", but when I clicked it, it did not direct me anywhere. Some others are in main.tex, which can be directed to. – Ypbor May 04 '22 at 12:33
  • The problem is in the .toc that is where that \setcounter written to. But the error message is important. If the error is in .toc or say .aux then it is something written to that file in some incompatible way. As for examples, you should always reduce to a single file, this is an important debugging tool. What I did here was to download your example, and then I messed with the .cls file and having the compiler ignore parts of it. – daleif May 04 '22 at 13:01