8

I am using numberwithin to number my figures within their section. For example, I have figures 1.1, 1.2, and 1.3 in section 1 and figures 2.1 and 2.2 in section 2. What I want to do is reference just the figure number, and not the section. For example, I want a reference to read "figures 1.1-3". Is there any way to do this?

Here is what I believe to be the relevant part of the preamble in my document:

\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}

The following is a small example with respect to the above preamble:

\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}
4
  • Please tell us which package(s), if any, you load at present to create cross-references.
    – Mico
    Jan 3, 2021 at 22:28
  • I don't think I am loading any packages for cross-references. Please see my edit including the preamble to my document.
    – alan
    Jan 3, 2021 at 22:35
  • 1
    Off-topic: Since you load the graphicx package, there's no need for the epsfig package. None.
    – Mico
    Jan 3, 2021 at 22:38
  • Fair enough. It ended up working. I'll accept other answers if they work, too.
    – alan
    Jan 3, 2021 at 22:57

3 Answers 3

11

The cleveref package provides some machinery to "strip" the prefix component from an object's number when creating cross-references to ranges of objects, such as figure environments.

(In some cases such as Roman numbering, cleveref's \crefstripprefix machinery may not be enough, though, and you might need xstring's \StrCut.)

For more information on this method, see page 20 of the user guide of the cleveref package as well as postings such as Referencing a range of equations with a particular format (A.3-7) instead of (A.3)-(A.7), Crossreference in Ref Range (1a-1e) to (1a-e), and Refer to a range of subsubexamples.

enter image description here

\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}

Addendum: The solution method proposed here lets writers use \cref to create multiple cross-referencing call-outs to ranges of numbers, with the number prefix being stripped off for each range of numbers. For instance,

enter image description here

\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}
5

You can load the package refcount and define a macro \RefOnlyStuffBehindLastDot which uses refcount's \getrefbykeydefault for extracting the number from the reference before applying a tail-recursive macro which from the extracted number removes dot-delimited arguments until no dots are left:

\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}

enter image description here

6
  • But what if there is no dot/delimiter ? Can you use \getrefbykeydefault to call the sublabel ? May 11 at 17:00
  • 1
    @VincentKrebs The macro \RefOnlyStuffBehindLastDot iteratively removes dot-delimited arguments until there is no more dot. If in the beginning there is no dot, nothing gets removed. What do you mean when using the phrase "sublabel"? May 12 at 10:26
  • Makes sense. But I was talking about \getrefbykeydefault precisely. Was a shortcut for "sublevel label" (call it "alone", skipping the main level label). May 12 at 10:56
  • 1
    @VincentKrebs You can use \getrefbykeydefault with any cross-referencing-label for obtaining the tokens that form the textual phrase of the cross-reference. This is useful when the package hyperref is loaded where commands like \ref/\pageref do not just deliver tokens for textual phrases but wrap tokens for textual phrases between additional tokens for turning things into hyperlinks. May 12 at 11:01
  • Wonderful, I played with it somehow and that's what I thought, I'll have to find some documentation. I think what I am talking about here is a wrong problem, i.e. I do not need a delimiter to skip tokens tex.stackexchange.com/a/643863/262813 May 12 at 11:20
3

If you're not using any fancy cross-referencing packages, you can just set the reference to whatever you like before setting a \label by changing \@currentlabel:

enter image description here

\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}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.