\cref 的使用与未编号子节内的定义不匹配

\cref 的使用与未编号子节内的定义不匹配

处理以下memoir文档(其中包含未编号小节的自定义命令\subsec)会导致错误:

.[docname]:64: Use of \cref doesn't match its definition.
l.64 Here we are in \cref{
                          sec:first}.

错误确实不是如果我使用常规\subsection\subsection*命令,则会发生这种情况。

\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}

哪里出了问题?如何修复?

笔记:在我的“真实”书长文档中,我遇到了一个大概相关的错误,但\cref事实是:没有发出错误,而是在预期的输出“表 1.1”的位置产生了形式为“子节 [子节名称]”的引用!

有关的:

交叉引用未编号的子部分

带有自定义未编号 SUBsections 的复数 \cref

更新:

这是一个更简单的 MWE — 没有任何table环境或数学,也没有引用第 n 节而不是第 mn 节的代码 — 但会导致相同的错误:

\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}

相关内容