0

I am using the answer given here to customize tags for amsmath environments. However, with the cleveref package, types other than equation generate warnings:

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

and the equation reference is preceded by ?? corresponding to an undefined type.

I can specify the type as equation in the label tag to avoid this warning. Is there a way to do this in the preamble without having to modify each label? Setting \crefalias{align}{equation} did not work.

The example I used for testing is

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

I would also like to remove the excess space after the matrix m environment.

1 Answer 1

1

I believe what you want is the following:

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

Regarding the “excess space after the matrix m environment,” you can see the same with several consecutive align environments separated by blanks lines (\par). I believe this is just something to avoid. The space can be reduced by replacing such blank lines with lines containing only a %, though—see below.

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

enter image description here

1
  • Thanks, that fixed it. Apr 9, 2020 at 10:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .