Using cleveref with mdpi template

cleverefhyperrefmdpioverleaf

I would like to use the \cref command provided by the cleveref package in a project using the MDPI template. However, I can't seem to do so without getting an error.

Because I'm working from the template an MWI would be really long, so in lieu of that here are precise steps to reproduce the issue:

  1. start a new project using the MDPI template. If using Overleaf, this can be done by selecting "New Project" then "Journal Template", then selecting the mdpi one.

  2. Insert the code \cref{fig1} somewhere in the document body. (fig1 is a label that is already defined by the template.) A comment in the template says that cleveref is loaded by default, so in theory this should work, but it doesn't – it says \cref is an undefined command.

  3. In an attempt to fix that, insert \usepackage{cleveref} just before the \begin{document} line.

Unfortunately step 3 doesn't fix the issue. Instead I get an error, Package cleveref Error: cleveref must be loaded after hyperref!. I don't know how to fix this because I'm not loading hyperref manually at all – it's loaded automatically by the template.

In an attempt to work around the issue I tried

\AfterPackage{hyperref}{\usepackage{cleveref}}

just before \begin{document}. This doesn't give an error, but it doesn't work properly either – the reference is given as "??" and I get a warning about the undefined reference, no matter how many times I recompile.

What would be the correct way to load cleveref when using the MDPI template, such that the \cref command can be used?

Best Answer

I had the same problem. It seems that cleveref is not included in the class file (mdpi.cls), although it is claimed in the template. The class file is set in such a way that the hyperref package is loaded last by using \AddToHook. However, this is against the usage rules for cleveref, which must be loaded after hyperref.

The workaround is to add cleveref in the same way as hyperref by adding the follwing code before \begin{document}:

\AddToHook{begindocument}{ 
\usepackage[capitalise,nameinlink,sort&compress]{cleveref}}