有什么方法可以引用标签的特定组件吗?

有什么方法可以引用标签的特定组件吗?

我使用numberwithin对图的编号来对它们所在部分进行编号。例如,我在第 1 部分中有图 1.1、1.2 和 1.3,在第 2 部分中有图 2.1 和 2.2。我想要做的是只引用图号,而不是引用部分。例如,我想要引用“图 1.1-3”。有什么方法可以做到这一点吗?

以下是我认为与我的文档序言相关的部分:

\documentclass[runningheads]{llncs}

\let\proof\relax
\let\endproof\relax

\usepackage{amsthm}
\usepackage{amssymb}
\setcounter{tocdepth}{3}
\usepackage{graphicx}
\usepackage{mathtools}
\usepackage{listings}
\usepackage{verbatim}
\usepackage{epsfig}  
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{todonotes}
\usepackage[normalem]{ulem}
\usepackage{proof}
\usepackage{ragged2e}
\usepackage{enumitem}
\usepackage{syntax}
\usepackage{mathpartir}

\numberwithin{figure}{section}

以下是针对上述序言的一个小例子:

\begin{document}

\title{Example}
\titlerunning{Example}

\author{Me}

\maketitle

\section{First Section}

\begin{figure}[htbp]
\caption{First Figure}
\label{fig:1.1}
This is a figure.
\end{figure}

\begin{figure}[htbp]
\caption{Second Figure}
\label{fig:1.2}
This is another figure.
\end{figure}

\begin{figure}[htbp]
\caption{Third Figure}
\label{fig:1.3}
This is yet another figure.
\end{figure}

\section{Second Section}

\begin{figure}[htbp]
\caption{Fourth Figure}
\label{fig:2.1}
This is a figure in the second section.
\end{figure}

\begin{figure}[htbp]
\caption{Fifth Figure}
\label{fig:2.2}
This is another figure in the second section.
\end{figure}

\section{Conclusion}
As you can see in figures \ref{fig:1.1}-\ref{fig:1.3}, the numbering is not what I'd like. I want it to read "figures 1.1-3".

\end{document}

答案1

cleveref包提供了一些机制,可以在创建对以下对象的交叉引用时从对象的编号中“剥离”前缀组件:范围对象,例如figure环境。

(在某些情况下,例如罗马数字,cleveref\crefstripprefix机制可能不够用,您可能需要xstring's \StrCut。)

有关此方法的更多信息,请参阅该包的用户指南第 20 页cleveref以及以下帖子:引用具有特定格式的一系列方程式 (A.3-7),而不是 (A.3)-(A.7)在参考范围 (1a-1e) 至 (1a-e) 中交叉引用, 和参考一系列子示例

在此处输入图片描述

\documentclass[runningheads]{llncs}
\usepackage{amsmath}   % for '\numberwithin' macro
\numberwithin{figure}{section}

\usepackage[noabbrev]{cleveref}  % see https://ctan.org/pkg/cleveref
\crefrangelabelformat{figure}{#3#1#4--#5\crefstripprefix{#1}{#2}#6}

\begin{document}
%% The following is _minimalist_ code
\setcounter{section}{1}
\refstepcounter{figure}\label{fig:aa}
\refstepcounter{figure}\label{fig:bb}
\refstepcounter{figure}\label{fig:cc}

A cross-reference to \cref{fig:aa,fig:cc,fig:bb}. % arguments of '\cref' needen't be sorted by the user
\end {document}

附录:这里提出的解决方法允许作者\cref创建多个交叉引用标注范围的数字,每个数字范围的数字前缀都被删除。例如,

在此处输入图片描述

\documentclass[runningheads]{llncs}
\counterwithin{figure}{section}

\usepackage[noabbrev]{cleveref}  % see https://ctan.org/pkg/cleveref
\crefrangelabelformat{figure}{#3#1#4--#5\crefstripprefix{#1}{#2}#6}

\begin{document}
%% The following is _minimalist_ code; it's not how one would normally
%% go about creating 'figure' environments, captions, and labels.
\setcounter{section}{1}
\refstepcounter{figure}\label{fig:aa} % 1.1
\refstepcounter{figure}\label{fig:bb} % 1.2
\refstepcounter{figure}\label{fig:cc} % 1.3
\stepcounter{section}
\refstepcounter{figure}\label{fig:dd} % 2.1
\refstepcounter{figure}\label{fig:ee} % 2.2
\refstepcounter{figure}\label{fig:ff} % 2.3
\refstepcounter{figure}\label{fig:gg} % 2.4

%% Now the call to \cref. Note that the arguments of \cref
%% need not be sorted in any order.
A cross-reference to \cref{fig:aa,fig:cc,fig:gg,fig:ee,fig:ff,fig:bb}. 
\end {document}

答案2

您可以加载包引用计数并定义一个宏\RefOnlyStuffBehindLastDot ,该宏使用 refcount\getrefbykeydefault从引用中提取数字,然后应用尾部递归宏,该宏从提取的数字中删除点分隔的参数,直到没有点为止:

\documentclass[runningheads]{llncs}

\usepackage{refcount}
\makeatletter
\@ifdefinable\gobbletodot{\long\def\gobbletodot#1.{}}%
\newcommand\KeepOnlyStuffBehindLastDot[1]{%
  \ifcat$\detokenize\expandafter{\gobbletodot#1.}$%
  \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {#1}{\expandafter\KeepOnlyStuffBehindLastDot\expandafter{\gobbletodot#1}}%
}%
\@ifdefinable\RefOnlyStuffBehindLastDot{%
  \DeclareRobustCommand\RefOnlyStuffBehindLastDot[1]{%
    \IfRefUndefinedBabel{#1}{\refused{#1}\nfss@text{\reset@font\bfseries??}}%
                        {%
                          \@ifundefined{hyperref}{\@firstofone}{\hyperref[{#1}]}%
                          {%
                            \expandafter\expandafter\expandafter\KeepOnlyStuffBehindLastDot
                            \expandafter\expandafter\expandafter{\getrefbykeydefault{#1}{}{??}}%
                          }%
                        }%
  }%
}%
\makeatother

% \usepackage{hyperref}

\let\proof\relax
\let\endproof\relax

\usepackage{amsthm}
\usepackage{amssymb}
\setcounter{tocdepth}{3}
\usepackage{graphicx}
\usepackage{mathtools}
\usepackage{listings}
\usepackage{verbatim}
\usepackage{epsfig}  
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{todonotes}
\usepackage[normalem]{ulem}
\usepackage{proof}
\usepackage{ragged2e}
\usepackage{enumitem}
\usepackage{syntax}
\usepackage{mathpartir}

\numberwithin{figure}{section}

\begin{document}

\title{Example}
\titlerunning{Example}

\author{Me}

\maketitle

\section{First Section}

\begin{figure}[htbp]
\caption{First Figure}
\label{fig:1.1}
This is a figure.
\end{figure}

\begin{figure}[htbp]
\caption{Second Figure}
\label{fig:1.2}
This is another figure.
\end{figure}

\begin{figure}[htbp]
\caption{Third Figure}
\label{fig:1.3}
This is yet another figure.
\end{figure}

\section{Second Section}

\begin{figure}[htbp]
\caption{Fourth Figure}
\label{fig:2.1}
This is a figure in the second section.
\end{figure}

\begin{figure}[htbp]
\caption{Fifth Figure}
\label{fig:2.2}
This is another figure in the second section.
\end{figure}

\section{Conclusion}
As you can see in figures \ref{fig:1.1}-\ref{fig:1.3}, the numbering is not what I'd like. I want it to read "figures 1.1-3".

As you can see in figures \ref{fig:1.1}-\RefOnlyStuffBehindLastDot{fig:1.3}, the numbering what I like. I want it to read "figures 1.1-3".

\end{document}

在此处输入图片描述

答案3

\label如果你没有使用任何花哨的交叉引用包,你可以在设置之前通过更改将引用设置为你喜欢的任何内容\@currentlabel

在此处输入图片描述

\documentclass[runningheads]{llncs}

\usepackage{mathtools}

\numberwithin{figure}{section}

\begin{document}

\section{A section}

See Figures \ref{fig:first}--\ref{fig:final}, 
or Figures~\ref{fig:first}--\ref{fig:final-final}.

\begin{figure}
  \caption{First figure}\label{fig:first}% 1.1
  \caption{Second figure}\label{fig:second}% 1.2
  \caption{Third figure}\label{fig:third}% 1.3
  \caption{Final figure}\label{fig:final}% 1.4
  \makeatletter
  \renewcommand{\@currentlabel}{\arabic{figure}}\label{fig:final-final}% 4
  \makeatother
\end{figure}

\end{document}

相关内容