用於編寫約束的 smartef 類型引用

用於編寫約束的 smartef 類型引用

\cref{eq15,eq16,eq17,eq18}產生Eqs. (15) to (18).

我想根據參考的位置和參考的數量將一些方程式寫為Constraint/// 。與/中發生的情況類似,它會自動偵測引用​​的數量並相應地產生數量。另外,我不想改變方程式。constraintConstraintsconstraints\cref\Cref\cref

可能的?有什麼幫助/想法嗎?

微量元素:

\documentclass{article}
\usepackage{amsmath}
\usepackage[colorlinks,allcolors=red]{hyperref}
\usepackage{cleveref}

\begin{document}
\begin{equation}\label{eq1}
    a+b<2
\end{equation}
\begin{equation}\label{eq4}
    a+b<2
\end{equation}
\begin{equation}\label{eq3}
    a+b<2
\end{equation}
\begin{equation}\label{eq2}
    a+b<2
\end{equation}

\Cref{eq1} should display Equation and \cref{eq1} should display Eq. i.e., normal behaviour of cref. I don't want to change this.

Need some kind of newcommand/macro/package so that I can write constraints (2) to (4). 

%Ex:
%\newref{eq2} = constraint (4)
%\Newref{eq2} = Constraint (4)
%\newref{eq3,eq2,eq4} = constraints (2) to (4)
%\Newref{eq3,eq2,eq4} = Constraints (2) to (4)
%\newref{eq2,eq3} = constraints (3) and (4)
%\Newref{eq2,eq3} = constraints (3) and (4)

\end{document}

答案1

constraint您可以透過為計數器定義一個名為 的「別名」來實現格式化目標equation。觀察環境\label指令的形式: ,其中是。使用可選參數告訴使用預設標籤名稱以外的標籤名稱。equation\label[constraint]{eq|x|}x{2,3,4}cleveref

有關 的“別名”功能的更多信息cleveref,請參閱第 6 節包的用戶指南,「覆蓋交叉引用類型」。

在此輸入影像描述

\documentclass{article}
\usepackage{amsmath}
\usepackage[colorlinks,allcolors=red]{hyperref}

\usepackage{cleveref}
\crefalias{constraint}{equation}
\crefname{constraint}{constraint}{constraints}
\Crefname{constraint}{Constraint}{Constraints}
\creflabelformat{constraint}{#2{\upshape(#1)}#3} 

\begin{document}
\begin{equation}\label{eq1} a+b=2 \end{equation}
\begin{equation}\label[constraint]{eq2} a+b<2 \end{equation}
\begin{equation}\label[constraint]{eq3} a+b>2 \end{equation}
\begin{equation}\label[constraint]{eq4} a+b\ne2 \end{equation}

\Cref{eq1}; \cref{eq1}.

\Cref{eq2,eq4,eq3}; \cref{eq2,eq4,eq3}.
\end{document}

答案2

該解決方案取決於定義一個新命令來使用\cref/\Cref暫時的當交叉引用方程式時改變它們的定義。這樣,在任何時候,方程式都可以透過其初始標籤定義或附加標籤進行交叉引用(無需刪除初始/原始標籤)。兩個標籤可以隨時用於任意順序的相同方程式。

新指令可以\ccref定義如下

\newcommand*\ccref[1]{\crefname{equation}{constraint}{constraints}\Crefname{equation}{Constraint}{Constraints}\cref{#1}\crefname{equation}{eq.}{eqs.} \Crefname{equation}{Eq.}{Eqs.}}

類似地定義大寫字母\CCref

以下是使用兩個標籤時所有可能情況的eq./eqs./Eq./Eqs.範例constraint/constraints/Constraint/Constraints

在此輸入影像描述

完整程式碼

\documentclass{article}
\usepackage{amsmath}
\usepackage[colorlinks,allcolors=red]{hyperref}
\usepackage{cleveref}
\crefname{equation}{eq.}{eqs.} % Initial definition of \cref
\Crefname{equation}{Eq.}{Eqs.} % Initial definition of \Cref
%
\newcommand*\ccref[1]{\crefname{equation}{constraint}{constraints}\Crefname{equation}{Constraint}{Constraints}\cref{#1}\crefname{equation}{eq.}{eqs.} \Crefname{equation}{Eq.}{Eqs.}}
\newcommand*\CCref[1]{\crefname{equation}{constraint}{constraints}\Crefname{equation}{Constraint}{Constraints}\Cref{#1}\crefname{equation}{eq.}{eqs.} \Crefname{equation}{Eq.}{Eqs.}}
%
\begin{document}
%
\section{Equations}
    \begin{equation}\label{eq1}
        a+b<2
    \end{equation}
    \begin{equation}\label{eq2}
        a+b<2
    \end{equation}
    \begin{equation}\label{eq3}
        a+b<2
    \end{equation}
    \begin{equation}\label{eq4}
        a+b<2
    \end{equation}

\section{Test single equation}
    \begin{tabular}{l l }
        Command & Output \\
        \hline
    \verb|\cref{eq1}| &  \cref{eq1} \\
    \verb|\Cref{eq1}| &  \Cref{eq1} \\
    \verb|\ccref{eq1}| & \ccref{eq1} \\
    \verb|\CCref{eq1}| &  \CCref{eq1}  \\
    \end{tabular}

\section{Test many equation}

\begin{tabular}{l l }
    Command & Output \\
    \hline
    \verb|\cref{eq1,eq2}| &  \cref{eq1,eq2} \\
    \verb|\Cref{eq1,eq2}| &  \Cref{eq1,eq2} \\
    \verb|\Cref{eq1,eq2,eq4}| &  \Cref{eq1,eq2,eq4} \\
    \verb|\ccref{eq1,eq2}| & \ccref{eq1,eq2} \\
    \verb|\CCref{eq1,eq2}| &  \CCref{eq1,eq2}  \\
    \verb|\CCref{eq1,eq2,eq4}| &  \CCref{eq1,eq2,eq4}  \\
\end{tabular}

\section{Test range of equations}
    \begin{tabular}{l l }
    Command & Output \\
    \hline
    \verb|\cref{eq1,eq2,eq3,eq4}| &  \cref{eq1,eq2,eq3,eq4} \\
    \verb|\Cref{eq1,eq2,eq3,eq4}| &  \Cref{eq1,eq2,eq3,eq4} \\
    \verb|\ccref{eq1,eq2,eq3,eq4}| & \ccref{eq1,eq2,eq3,eq4} \\
    \verb|\CCref{eq1,eq2,eq3,eq4}| &  \CCref{eq1,eq2,eq3,eq4}  \\
\end{tabular}
\end{document}

相關內容