Processing the following memoir document, which contains a custom command \subsec for unnumbered subsections, causes the error:
.[docname]:64: Use of \cref doesn't match its definition.
l.64 Here we are in \cref{
sec:first}.
The error does not occur if I use, instead, a conventional \subsection or \subsection* command.
\documentclass{memoir}
\usepackage{amsmath}
\setsecnumdepth{subsection}
% Handle refs to subsections, which are unnumbered
\newcounter{subsec}
\newcommand{\subsec}[1]{%
% 2 lines moved to end per Mico
%% \subsection*{#1}%
%% \addcontentsline{toc}{subsection}{#1}%
\refstepcounter{subsec}%{\ss}
\def\cref@currentlabel{[subsec][\arabic{subsec}][]#1}%
\def\@currentlabelname{#1}%
\subsection*{#1}%
\addcontentsline{toc}{subsection}{#1}%
}
\usepackage[colorlinks,linkcolor=red]{hyperref}
%% Allow ref to section n, not section m.n
%% From 'third installment' of answer by Christian Hupfer
%% https://tex.stackexchange.com/a/383608
\usepackage{xparse,xassoccnt}
\usepackage[user,hyperref]{zref}
\RegisterPostLabelHook{\zlabel}
% Replace \arabic{section} with \Roman{section} etc. as needed:
\makeatletter
\zref@newprop{section}{\arabic{section}}
\zref@addprop{main}{section}
\newcommand{\secref}[1]{%
\zref@ifrefundefined{#1}{%
\ref{#1}}{%
\hyperlink{\zref@extract{#1}{anchor}}%
{\zref@extract{#1}{section}}}}
\makeatother
%% End of code from Christian Hupfer's answer
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}
\crefname{subsec}{subsection}{subsections}
\Crefname{subsec}{Subsection}{Subsections}
\crefformat{subsec}{#2subsection~``#1''#3} % for unnumbered subsections
\Crefformat{subsec}{#2Subsection~``#1''#3} % ditto, caps
\crefname{page}{page}{page}
\begin{document}
\chapter{The chapter}\label{chap:the}
\section{A first section}\label{sec:first}
% Cleveref is OK with \subsection instead of \subsec
%\subsection{A numbered subsection}\label{subsec:A}
% This causes produces the error:
\subsec{An unnumbered subsection}\label{subsec:A}
\begin{table}[h]
\begin{gather*}
2+2 = 4
\end{gather*}
\caption{Some math}
\label{tbl:math}
\end{table}
Here we are in \cref{sec:first}.
In \cref{tbl:math} we see some math.
\end{document}
What is wrong and how can it be fixed?
Note: In my "real" book-length document, I encountered a presumably related error, instead: \cref did not issue an error but instead, in place of the expected output "Table 1.1" produced a reference of the form "subsection [the subsection name]"!
Related:
Cross-reference to unnumbered SUBsections
Plural \cref with custom unnumbered SUBsections
Update:
Here's an even simpler MWE — without any table environment` or math, and without the code to refer to section n instead of section m.n — that causes the same error:
\documentclass{memoir}
\setsecnumdepth{subsection}
% Handle refs to subsections, which are unnumbered
\newcounter{subsec}
\newcommand{\subsec}[1]{%
% 2 lines moved to end per Mico
%% \subsection*{#1}%
%% \addcontentsline{toc}{subsection}{#1}%
\refstepcounter{subsec}%{\ss}
\def\cref@currentlabel{[subsec][\arabic{subsec}][]#1}%
\def\@currentlabelname{#1}%
\subsection*{#1}%
\addcontentsline{toc}{subsection}{#1}%
}
\usepackage[colorlinks,linkcolor=red]{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}
\begin{document}
\chapter{The chapter}\label{chap:the}
\section{A first section}\label{sec:first}
Nothing
\section{Section two}\label{sec:2nd}
% Cleveref is OK with \subsection instead of \subsec
%\subsection{A numbered subsection}\label{subsec:A}
% This causes produces the error:
\subsec{An unnumbered subsection}\label{subsec:A}
Here we are in \cref{sec:2nd}.
\end{document}
\makeatletterand\makeatotheraround the newcommand (the definition contains an@character). – Marijn May 06 '20 at 20:42\cref{table:xxxx}giving me the current\subsecname instead of the expected "Table m.n" reference. – murray May 06 '20 at 21:22\makeatletter...\makeatother. – murray May 14 '20 at 22:07