标题说的差不多。我只想\ref
在我的 latex 文档中使用,但是当我写时,\ref{eq: ...}
我希望\ref
将其排版为\eqref
,而当我使用\ref{sec: ...}
或\ref{fig: ...}
或其他任何内容时,我希望将其排版为\cref
。
答案1
\ref
我不会重新定义诸如 之类的低级命令,而是重新定义\cref
它,以便当其参数是与计数器 关联的某个项目时,它不会输出标签(通常是“方程式”或“方程式”)equation
。您需要做的就是运行指令
\crefname{equation}{}{}
加载cleveref
包后。(可选)如果您恰好加载了hyperref
包,并且不是希望将交叉引用方程编号周围的括号作为超链接的一部分,您可能还需要运行指令
\creflabelformat{equation}{\textup{(#2#1#3)}}
默认值为\creflabelformat{equation}{\textup{#2(#1)#3}}
。请注意左括号和右括号位置的不同。
在以下 MWE(最小工作示例)中,hyperref
包中加载了选项nameinlink
,并且cleveref
包中加载了nameinlink
突出显示正在生成的内容的选项\cref
选项。文本模式故意设置为斜体说明和都\eqref
将以\cref
直立方式(即“罗马”模式)排版交叉引用的方程编号(及其相关括号)。这是标准的排版惯例。
\documentclass{article}
\usepackage{amsmath} % for "\eqref" command
\usepackage[colorlinks]{hyperref} % just for this example
\usepackage[nameinlink,noabbrev]{cleveref}
\crefname{equation}{}{}
\creflabelformat{equation}{\textup{(#2#1#3)}} % optional
\begin{document}
\section{First}\label{sec:first}
\begin{equation}\label{eq:pyth}a^2+b^2=c^2\end{equation}
\begin{equation}\label{eq:triv}1+1=2\end{equation}
\begin{figure}\caption{A figure}\label{fig:test}\end{figure}
\section{Second}
\itshape % Just to verify that equation numbers will be typeset
% in upright ("Roman") mode by both \eqref and \cref.
%% An example with "\eqref"
Cross-references to equations \eqref{eq:pyth} and \eqref{eq:triv}.
\noindent%% An example with "\cref"
Cross-references to equations \cref{eq:pyth,eq:triv,sec:first,fig:test}.
\end{document}
答案2
我不建议重新定义\ref
-macro,因为修改该宏的 hyperref 包可能依赖于其定义。
如何定义一个尚未使用的名称的宏,例如,\MyRefChooseCommand
检查其参数是否具有前导短语“eq:”或“fig:”或“sec:”或其他什么,然后对该参数应用适当的引用命令?
\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}
\begingroup
\makeatletter
\@firstofone{%
\endgroup
%%----------------------------------------------------------------------
%% Paraphernalia:
%%......................................................................
\newcommand\UD@Exchange[2]{#2#1}%
%%----------------------------------------------------------------------
%% Check whether argument is empty:
%%......................................................................
%% \UD@CheckWhetherNull{<argument which is to be checked>}%
%% {<tokens to be delivered in case that argument
%% which is to be checked is empty>}%
%% {<tokens to be delivered in case that argument
%% which is to be checked is not empty>}%
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral0\expandafter\@secondoftwo\string{\expandafter
\@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\@secondoftwo\string}\expandafter\@firstoftwo\expandafter{\expandafter
\@secondoftwo\string}\expandafter\expandafter\@firstoftwo{ }{}%
\@secondoftwo}{\expandafter\expandafter\@firstoftwo{ }{}\@firstoftwo}%
}%
%%----------------------------------------------------------------------
%% Check whether argument's leading tokens form a specific token-sequence:
%%......................................................................
%% \UD@CheckWhetherLeadingTokens{<token sequence>}%
%% {<a single non space token> that does
%% _not_ occur in <token sequence>>}%
%% {<internal token-check-macro>}%
%% {<argument which is to be checked>}%
%% {<tokens to be delivered in case <argument
%% which is to be checked> has <token sequence>
%% as leading tokens>}%
%% {<tokens to be delivered in case <argument
%% which is to be checked> does not have
%% <token sequence> as leading tokens>}%
\newcommand\UD@CheckWhetherLeadingTokens[4]{%
\romannumeral0\UD@CheckWhetherNull{#4}%
{\expandafter\expandafter\@firstoftwo{ }{}\@secondoftwo}%
{\expandafter\@secondoftwo\string{#3#2#4#1}{}}%
}%
\newcommand\UD@@CheckWhetherLeadingTokens[1]{%
\expandafter\UD@CheckWhetherNull\expandafter{\@firstoftwo{}#1}%
{\UD@Exchange{\@firstoftwo}}{\UD@Exchange{\@secondoftwo}}%
{\UD@Exchange{ }{\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter}\expandafter\expandafter
\expandafter}\expandafter\@secondoftwo\expandafter{\string}%
}%
%%----------------------------------------------------------------------
%% \UD@internaltokencheckdefiner{<internal token-check-macro>}%
%% {<token sequence>}%
%% Defines <internal token-check-macro> to snap everything
%% until reaching <token sequence>-sequence and pass that to
%% \UD@@CheckWhetherLeadingTokens.
%%......................................................................
\newcommand\UD@internaltokencheckdefiner[2]{%
\newcommand#1{}\long\def#1##1#2{\UD@@CheckWhetherLeadingTokens{##1}}%
}%
\UD@internaltokencheckdefiner{\@CheckEq}{eq:}%
\UD@internaltokencheckdefiner{\@CheckSec}{sec:}%
\UD@internaltokencheckdefiner{\@CheckFig}{fig:}%
%%----------------------------------------------------------------------
%% \MyRefChooseCommand{<label>}
%%
%% Examines the <label>-argument.
%% Yields \eqref{<label>} in case <label> begins with 'eq:'
%% Yields \cref{<label>} in case <label> begins with 'sec:'
%% Yields \cref{<label>} in case <label> begins with 'fig:'
%% Yields \cref{<label>} in case <label> begins with 'something else.
%%
%% The branches for 'sec:' and 'fig:' are somewhat pointless.
%% They are just there for exhibiting how forther forking
%% can be implemented.
%%......................................................................
\newcommand\MyRefChooseCommand[1]{%
\UD@CheckWhetherLeadingTokens{eq:}{.}{\@CheckEq}{#1}{%
\eqref %<- name of label starts with 'eq:'
}{%
\UD@CheckWhetherLeadingTokens{sec:}{.}{\@CheckSec}{#1}{%
\cref %<- name of label starts with 'sec:'
}{%
\UD@CheckWhetherLeadingTokens{fig:}{.}{\@CheckFig}{#1}{%
\cref %<- name of label starts with 'fig:'
}{%
\cref %<- name of label starts with something else.
}%
}%
}{#1}%
}%
}%
\begin{document}
\section{A nice section}\label{sec:nice}
\begin{equation}
a^2+b^2=c^2\label{eq:1}
\end{equation}
Referencing via \verb|\ref|-command yields:
\verb|\ref{sec:nice}| \(\to\) \ref{sec:nice}
\verb|\ref{eq:1}| \(\to\) \ref{eq:1}
Referencing via \verb|\MyRefChooseCommand|-command yields:
\verb|\MyRefChooseCommand{sec:nice}| \(\to\) \MyRefChooseCommand{sec:nice}
\verb|\MyRefChooseCommand{eq:1}| \(\to\) \MyRefChooseCommand{eq:1}
\end{document}