使用 cleveref 包和自定义 amsmath 标签

使用 cleveref 包和自定义 amsmath 标签

我正在使用给出的答案这里amsmath环境自定义标签。但是,使用此cleveref包,除了equation生成警告之外,其他类型还会生成警告:

LaTeX Warning: cref reference format for label type `matrix' undefined on input 58 
LaTeX Warning: cref reference format for label type `matrix' undefined on input 68

并且方程式引用前面对应??于未定义的类型。

我可以在标签标记中指定类型equation以避免出现此警告。有没有办法在序言中执行此操作而不必修改每个标签?设置\crefalias{align}{equation}无效。

我用于测试的例子是

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage{cleveref}
\crefname{equation}{Eq.}{Eqs.}
\Crefname{equation}{Equation}{Equations}

\newcounter{matrix}
\renewcommand*{\thematrix}{M\arabic{matrix}}

\makeatletter
\def\@equationname{equation}
\newenvironment{m}[1]{%
    \def\mymathenvironmenttouse{#1}%
    \ifx\mymathenvironmenttouse\@equationname%
        \refstepcounter{matrix}%
    \else
        \patchcmd{\@arrayparboxrestore}{equation}{matrix}{}{}%          doesn't change output?
        \patchcmd{\print@eqnum}{equation}{matrix}{}{}%
        \patchcmd{\incr@eqnum}{equation}{matrix}{}{}%
%       \def\print@eqnum{\tagform@\thematrix}%                          instead of etoolbox' \pathcmd
%       \def\incr@eqnum{\refstepcounter{matrix}\let\incr@eqnum\@empty}% instead of etoolbox' \pathcmd
    \fi
    \csname\mymathenvironmenttouse\endcsname%
}{%
    \ifx\mymathenvironmenttouse\@equationname%
        \tag{\thematrix}%
    \fi
    \csname end\mymathenvironmenttouse\endcsname%
}
\makeatother

\begin{document}

\begin{equation} \label{eq:2}
 y = x
\end{equation}

The next matrix equation is \cref{eq:m1}.
\begin{m}{equation}
 y = \sqrt x \label{eq:m1}
\end{m}

\begin{align}
  \alpha &= \beta \label{eq:1} \\
  \delta &= \gamma \label{eq:4}
\end{align}

The previous standard equation is \cref{eq:2} and align is \cref{eq:1,eq:4}.

The next matrix equations (align) are \cref{eq:m2,eq:m3}.
\begin{m}{align}
 y &= \sqrt x \label{eq:m2} \\
 z &= y^2 \label[equation]{eq:m3}
\end{m}

\begin{m}{alignat}{3}
 y & = \sqrt x & \quad y^2 &= x & \quad z &= c  \label[equation]{eq:m4} \\
 p & = q/r & & & & \label{eq:m5}
\end{m}
The previous matrix equations (alignat) are \cref{eq:m4,eq:m5}.

\end{document}

我还想删除matrix m环境后的多余空间。

答案1

我相信你想要的是以下内容:

\crefname{matrix}{Eq.}{Eqs.}
\Crefname{matrix}{Equation}{Equations}
\creflabelformat{matrix}{#2(#1)#3}

关于“矩阵环境后的多余空间m”,您可以看到几个连续的align环境被空白行 ( \par) 分隔开,这也是同样的情况。我认为这只是为了避免。不过,可以通过将这些空白行替换为仅包含 的行来减少空间%——见下文。

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}
% \usepackage{hyperref} % load it before cleveref, but not earlier than that
\usepackage{cleveref}

\crefname{equation}{Eq.}{Eqs.}
\Crefname{equation}{Equation}{Equations}

\crefname{matrix}{Eq.}{Eqs.}
\Crefname{matrix}{Equation}{Equations}
\creflabelformat{matrix}{#2(#1)#3}

\newcounter{matrix}
\renewcommand*{\thematrix}{M\arabic{matrix}}

\makeatletter
\def\@equationname{equation}
\newenvironment{m}[1]{%
    \def\mymathenvironmenttouse{#1}%
    \ifx\mymathenvironmenttouse\@equationname%
        \refstepcounter{matrix}%
    \else
        \patchcmd{\@arrayparboxrestore}{equation}{matrix}{}{}%          doesn't change output?
        \patchcmd{\print@eqnum}{equation}{matrix}{}{}%
        \patchcmd{\incr@eqnum}{equation}{matrix}{}{}%
%       \def\print@eqnum{\tagform@\thematrix}%                          instead of etoolbox' \pathcmd
%       \def\incr@eqnum{\refstepcounter{matrix}\let\incr@eqnum\@empty}% instead of etoolbox' \pathcmd
    \fi
    \csname\mymathenvironmenttouse\endcsname%
}{%
    \ifx\mymathenvironmenttouse\@equationname%
        \tag{\thematrix}%
    \fi
    \csname end\mymathenvironmenttouse\endcsname%
}
\makeatother

\begin{document}

\begin{equation} \label{eq:2}
 y = x
\end{equation}
%
The next matrix equation is \cref{eq:m1}.
\begin{m}{equation}
 y = \sqrt x \label{eq:m1}
\end{m}
%
\begin{align}
  \alpha &= \beta \label{eq:1} \\
  \delta &= \gamma \label{eq:4}
\end{align}
%
The previous standard equation is \cref{eq:2} and align is \cref{eq:1,eq:4}.
The next matrix equations (align) are \cref{eq:m2,eq:m3}.
\begin{m}{align}
 y &= \sqrt x \label{eq:m2} \\
 z &= y^2 \label[equation]{eq:m3}
\end{m}
%
\begin{m}{alignat}{3}
 y & = \sqrt x & \quad y^2 &= x & \quad z &= c  \label[equation]{eq:m4} \\
 p & = q/r & & & & \label{eq:m5}
\end{m}
The previous matrix equations (alignat) are \cref{eq:m4,eq:m5}.

\end{document}

在此处输入图片描述

相关内容