我的文档中需要几个自定义计数器,需要在整个文本中引用它们。我找到了如何创建可以标记和引用的自定义计数器的方法。示例:如果我显示位于第 2 节第 1 子节中的第 3 个定义,则\ref{bestdef}
应该显示“2.1.3”。现在我的问题是它只\ref{bestdef}
显示“2.1”,而忽略了定义的自定义计数器(但链接有效)。我遗漏了什么?请参阅我随附的最小示例。
\documentclass[a4paper,fleqn]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[font=small,labelfont=bf,format=plain,labelsep=period]{caption}
\usepackage{hyperref}
%%%%%%%%%%%%%%%%%%%%%%%%%% Custom Counter: Definition %%%%%%%%%%%%%%%%%
\newcounter{mydefcounter}\setcounter{mydefcounter}{1}
\newcommand{\mydef}[1]{\arabic{section}.\arabic{subsection}.\arabic{mydefcounter}\textbf{#1}\refstepcounter{mydefcounter}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\section{First Section}
\subsection{First Subsection}
Definition \mydef{\label{nicedef}}: This is my first definition.
\subsection{Second Subsection}
Definition \mydef{\label{verynicedef}}: This is my second definition.
\section{Second Section}
\subsection{Third Subsection}
In Definition \ref{nicedef} we had seen that...
In Definition \ref{verynicedef} we had seen that...
Definition \mydef{\label{bestdef}}: This is my third definition.
In Definition \ref{bestdef} we see that...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
答案1
\newtheorem{definition}{Definition}[subsection]
在我看来,只要结合使用适当的标签和包装,根本不需要使用这样的柜台cleveref
。
首先解释一下为什么 OP 方式失败:
\label
需要先前的\refstepcounter
调用,它定义了,在此示例中\@currentlabel
明确使用了。\themydefcounter
\themydefcounter
只不过,\arabic{mydefcounter}
这在某个时候会变得不合适。
请参阅最后的示例:
\documentclass{article}
\newtheorem{definition}{Definition}[subsection]
\usepackage{hyperref}
\usepackage{cleveref}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\section{First Section}
\subsection{First Subsection}
\begin{definition} \label{nicedef}
This is my first definition.
\end{definition}
\subsection{Second Subsection}
\begin{definition} \label{verynicedef}
This is my second definition.
\end{definition}
\section{Second Section}
\subsection{Third Subsection}
In \Cref{nicedef} we had seen that...
In \Cref{verynicedef} we had seen that...
\begin{definition}\label{bestdef}
This is my third definition.
\end{definition}
In \Cref{bestdef} we see that...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
现在说说‘错误’和‘正确’的用法:
\documentclass{article}
\newcounter{foo}
\setcounter{foo}{1}
\newcommand{\mydef}[1]{\arabic{section}.\arabic{subsection}.\arabic{foo}\refstepcounter{foo}\textbf{#1}}
\begin{document}
\section{foo}
\subsection{foosubsection}
\makeatletter
A def: \mydef{\label{firstfoo}}
But this is the connected \verb!\@currentlabel!: \@currentlabel
\ref{firstfoo}
Another def: \mydef{\label{secondfoo}}
But this is the connected \verb!\@currentlabel!: \@currentlabel
\ref{secondfoo}
Now do it better:
\renewcommand{\thefoo}{\thesubsection.\arabic{foo}}
\renewcommand{\mydef}[1]{\refstepcounter{foo}#1\textbf{\thefoo}}
A def: \mydef{\label{thirdfoo}}
But this is the connected \verb!\@currentlabel!: \@currentlabel
\ref{thirdfoo}
Another def: \mydef{\label{fourthfoo}}
But this is the connected \verb!\@currentlabel!: \@currentlabel
\ref{fourthfoo}
\makeatother
\end{document}
答案2
你对\label
and的使用\ref
相当奇怪,我没有试图理解哪里出了问题。不过,重新安排你的一些定义,可以让一切按预期工作:
\documentclass[a4paper,fleqn]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[font=small,labelfont=bf,format=plain,labelsep=period]{caption}
\usepackage{hyperref}
%%%%%%%%%%%%%%%%%%%%%%%%%% Custom Counter: Definition %%%%%%%%%%%%%%%%%
\newcounter{mydefcounter}
\renewcommand{\themydefcounter}{\arabic{section}.\arabic{subsection}.\arabic{mydefcounter}}
\newcommand{\mydef}[1]{\refstepcounter{mydefcounter}\label{#1}\themydefcounter}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\section{First Section}
\subsection{First Subsection}
Definition \mydef{nicedef}: This is my first definition.
\subsection{Second Subsection}
Definition \mydef{verynicedef}: This is my second definition.
\section{Second Section}
\subsection{Third Subsection}
In Definition \ref{nicedef} we had seen that...
In Definition \ref{verynicedef} we had seen that...
Definition \mydef{bestdef}: This is my third definition.
In Definition \ref{bestdef} we see that...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}