Compilation errors when babel and cleveref are included. and other errors when \frontmatter is present

clevereferrorsfront-matterhyperref

Overview

I'm currently re-writing some of my lecture notes in LaTeX. My first language is Spanish, so I'm using the babel package. I also wanted to add some utility to the resulting document, like bookmarks and clickable refs. However, just yesterday I stumbled across a compilation error that's halted my progress, and I'm struggling to understand what the problem is.

Details and MWE

I wrote a test document to check if there were any problems with the packages, and after a lot of testing I ended up with the following code, which I named main.tex:

\documentclass[10pt, spanish, openany]{memoir}
\usepackage{babel}
\usepackage[T1]{fontenc}
\usepackage{blindtext}
\usepackage{amsmath, amsthm}
\usepackage[backend=biber]{biblatex}
\usepackage{csquotes}

\usepackage[pdfencoding=auto, psdextra]{hyperref}
\usepackage{cleveref} % this package breaks everything for some reason
\usepackage[numbered]{bookmark}

\begin{document}
    \frontmatter
    {
        \hypersetup{hidelinks}
        \tableofcontents
    }
    \chapter*{Sample introduction}
    \addcontentsline{toc}{chapter}{Sample introduction} % So this section gets added to ToC
    \blindtext
    \begin{align}
        \label{sample:eq1}
        x + y = 2
    \end{align}
    \blindtext
    \mainmatter
    \chapter{Sample chapter}
    \blindtext
    \section{Sample section}
    \blindtext
\end{document}

Some of the packages I included in this test document are not used; however, my original project does make use of them, so I thought it'd be best if I kept them while testing. Now, after messing around, I think I've found the culprit(s): the babel (with the spanish parameter) and the cleveref packages. When these two are included, and the \frontmatter command is used, I get a compilation error: c:/TeX Projects/Hyperref Frontmatter Test/main.tex:24: Missing } inserted. <inserted text>.

I've also found that removing \frontmatter (while keeping both babel and cleveref) results in a successful compilation. Similarly, when removing either babel or cleveref (while keeping the \frontmatter command), the MWE can be compiled successfully. If possible, I'd like to compile my document while babel, cleveref and \frontmatter are all present. Another thing that's puzzling me is that I'm pretty sure that I was able to compile my original project just fine (with the three now problematic items included) a few weeks ago.

Best Answer

Update: Solution below


A bit too long for comment, on a fully up to date TeXLive 2021, I can bould the MWE down to

\documentclass{book}
\usepackage[spanish]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{cleveref}
\begin{document}
\frontmatter
\begin{align}
  \label{sample:eq1}
  x + y = 2
\end{align}
\end{document}

English works fine. So it seems to be an interaction between \frontmatter, spanish babel, amsmath (as there is no issue with equation) and cleveref.

What the problem is I don't know. The error I get is

! Missing } inserted.
<inserted text> 
                }
l.11 \end{align}

As Ulrike mentions in the comments, the problem is well known as spanish babel messes with Roman numerals.

In the manual for babel-spanish it is mentioned that also loading the babel option es-lcroman leaves the lower case (which \frontmatter uses) alone.

Thus this works

\documentclass{book}
\usepackage[spanish,es-lcroman]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{cleveref}
\begin{document}
\frontmatter
\begin{align}
   \label{sampleEq1}
  x + y = 2
\end{align}
\end{document}
Related Question