我可以将 \ref 和 \nameref 合并为一个命令吗?

我可以将 \ref 和 \nameref 合并为一个命令吗?

笔记这里有一个标题几乎相同的问题,但那里的人要求的却完全不同。

我目前正在写一篇经常引用章节名称和定理编号的文档。下面是 MWE:

\documentclass{article}
\usepackage{nameref}

\newtheorem{theorem}{Theorem}

\begin{document}
\section{Section Name}
\label{sec1}

\begin{theorem}
\label{thm1}
    Every section has a name.
\end{theorem}

By Theorem \ref{thm1}, this section has a name, which is \nameref{sec1}.

\end{document}

这段代码运行完美,但我不想与 区分开\ref\nameref。也就是说,我想改为这样写:

By Theorem \ref{thm1}, this section has a name, which is \ref{sec1}.

或者:

By \ref{thm1}, this section has a name, which is \ref{sec1}.

文本将输出为:

根据定理1,这个section有一个名字,就是Section Name。

这可能吗?是的,我希望将其保留为\ref,而不是类似的东西\myref

答案1

(我目前无法发表评论,否则我想问您cleveref在发布此文之前是否试图避免发表评论。如果是这样,请告诉我。)

如果您的目标只是获取输出,而不是具体弄清楚如何组合这样的命令,那么我建议使用该cleveref包,因为您所描述的本质上是重新发明\cref命令。

此外,除非其功能以某种方式修改,否则使用\nameref{thm1}将为您提供包含定理的部分的名称(我不认为这是目标)。

下面是您的代码示例\cref

\documentclass{article}
\usepackage{nameref}
\usepackage{cleveref}

% defines how the name for references to a single, and multiple
% items of the given type should be referenced
\crefname{theorem}{Theorem}{Theorems}

\newtheorem{theorem}{Theorem}

\begin{document}
\section{Section Name}
\label{sec1}

\begin{theorem}
\label{thm1}
    Every section has a name.
\end{theorem}

By \cref{thm1}, this section has a name, which is \nameref{sec1}.

\end{document}

其中有一行用于指定包应如何引用定理。

该包还允许您指定参考文献的格式:

% make references bold
\crefformat{theorem}{\B{Theorem~#2#1#3}}

以及指定当指定两个或更多项时要做什么:

\newcommand{\crefrangeconjunction}{--}  % placed between numbers in a range
\newcommand{\crefpairconjunction}{ and }  % placed between two items
\newcommand{\crefmiddleconjunction}{ , }  % placed between items in a list
\newcommand{\creflastconjunction}{, and }  % placed before last item in a list

% specify format for two items
\crefrangeformat{theorem}{\B{Theorems~#3#1#4\crefrangeconjunction#5#2#6}}

% specify format for more than two items
\crefmultiformat{theorem}{\B{Theorems~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}

定义内部引用

这是我论文中的文件示例,它定义了我需要的所有内容的参考样式。(和 之间有一些区别,\Crefname\crefname现在想不起来。)每个块包含相同的命令,唯一的区别在于它们所针对的类型:

\Crefname{equation}{Equation}{Equations}
\Crefname{figure}{Figure}{Figures}
\Crefname{tabular}{Table}{Tables}
\Crefname{section}{Section}{Sections}
\Crefname{chapter}{Chapter}{Chapters}
\Crefname{appendix}{Appendix}{Appendices}

\crefname{equation}{Equation}{Equations}
\crefname{figure}{Figure}{Figures}
\crefname{tabular}{Table}{Tables}
\crefname{section}{Section}{Sections}
\crefname{chapter}{Chapter}{Chapters}
\crefname{appendix}{Appendix}{Appendices}

\crefdefaultlabelformat{#2\B{#1}#3}
\crefformat{equation}{\B{Equation~#2#1#3}}
\crefformat{table}{\B{Table~#2#1#3}}
\crefformat{figure}{\B{Figure~#2#1#3}}
\crefformat{section}{\B{Section~#2#1#3}}
\crefformat{chapter}{\B{Chapter~#2#1#3}}
\crefformat{appendix}{\B{Appendix~#2#1#3}}

\newcommand{\crefrangeconjunction}{--}
\newcommand{\crefpairconjunction}{ and }
\newcommand{\crefmiddleconjunction}{ , }
\newcommand{\creflastconjunction}{, and }


\crefrangeformat{table}{\B{Tables~#3#1#4\crefrangeconjunction#5#2#6}}
\crefrangeformat{figure}{\B{Figures~#3#1#4\crefrangeconjunction#5#2#6}}
\crefrangeformat{equation}{\B{Equations~#3#1#4\crefrangeconjunction#5#2#6}}
\crefrangeformat{chapter}{\B{Chapters~#3#1#4\crefrangeconjunction#5#2#6}}
\crefrangeformat{section}{\B{Sections~#3#1#4\crefrangeconjunction#5#2#6}}
\crefrangeformat{appendix}{\B{Appendices~#3#1#4\crefrangeconjunction#5#2#6}}

\crefmultiformat{table}{\B{Tables~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}
\crefmultiformat{figure}{\B{Figures~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}
\crefmultiformat{equation}{\B{Equations~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}
\crefmultiformat{chapter}{\B{Chapters~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}
\crefmultiformat{section}{\B{Sections~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}
\crefmultiformat{appendix}{\B{Appendices~#2#1#3}}%
       {\B{ and~#2#1#3}}{\B{, #2#1#3}}{\B{, and~#2#1#3}}

答案2

这使用特殊\label命令,而不是特殊\ref命令。

\documentclass{article}
\usepackage{nameref}

\newtheorem{theorem}{Theorem}

\makeatletter
\newcommand{\sectionlabel}[1]{\let\@currentlabel=\@currentlabelname
  \edef\@currentlabelname{section}%
  \label{#1}}
\makeatother

\begin{document}
\section{Section Name}
\sectionlabel{sec1}

\begin{theorem}
\label{thm1}
    Every section has a name.
\end{theorem}

By Theorem \ref{thm1}, this section has a name, which is \ref{sec1}.

\end{document}

这将修改,它由、等\@startsection使用。\section\subsection

\documentclass{article}
\usepackage{etoolbox}

\newtheorem{theorem}{Theorem}

\makeatletter
\patchcmd{\@sect}{\refstepcounter{#1}}%
  {\refstepcounter{#1}\edef\@currentlabel{#7}}% #7=short title, #8=long title
  {}{FAILED}
\makeatother

\begin{document}
\section{Section Name}
\label{sec1}

\begin{theorem}
\label{thm1}
    Every section has a name.
\end{theorem}

By Theorem \ref{thm1}, this section has a name, which is \ref{sec1}.

\end{document}

相关内容