0

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}
6
  • 3
    In the MWE you are missing \makeatletter and \makeatother around the newcommand (the definition contains an @ character).
    – Marijn
    Commented May 6, 2020 at 20:42
  • @Marijn: ouch! sorry, inadvertent omissions from when I was simplifying my orginal issue, which was about \cref{table:xxxx} giving me the current \subsec name instead of the expected "Table m.n" reference.
    – murray
    Commented May 6, 2020 at 21:22
  • @Marjin you should make that an answer (possibly community wiki, at your discression) Commented May 9, 2020 at 10:31
  • @Marijn: tex.stackexchange says I MUST award the bonus to an answer, but you didn't post an actual answer! (I'd appreciate it if you decide you don't want to, given my obviously stupid failure to catch the missing \makeatletter...\makeatother.
    – murray
    Commented May 14, 2020 at 22:07
  • @ murray I didn't post an answer because it was only a simpe comment, and I thought you might change this question into a question about your actual problem not to waste the bounty on an @ sign (changing a question to be about something else, or a 'chameleon question' as it is called, is normally not really appreciated, but maybe for a bounty it is more acceptable, given also that there were no answers yet). I didn't see the comment of @AndrewSwann because he misspelled my name so I didn't get notified by the system. I guess the bounty is expired now?
    – Marijn
    Commented May 15, 2020 at 5:48

0

You must log in to answer this question.

Browse other questions tagged .